Solved

Nginx configuration - one url, multiple sub folders and rails app

Posted on 2010-08-26
3
1,999 Views
Last Modified: 2013-11-13
Hi, I have a nginx, thin set up for my rails apps. I've got 1 domain name which I can use and need to handle 20 different rails app. ...

So ... need to do this way ...

say the domain name is blah.blahblah.com

for rails app "abc", it will be accessed via a subfolder like

http://blahblah.blah.com/abc

then for rails app "def". same thing

http://blahblah.blah.com/def

I've done it this way (see code). It works if it's just for one sub folder but if I do it for say 2, there's some error. I'm not a guru in nginx, I copied this off somewhere on the web.

Please advise.

Question is how do I set this up in nginx?
upstream abc {

        server 127.0.0.1:5090;

    }



upstream def {

        server 127.0.0.1:6000;

    }



server {

            listen   80;

            server_name blah.blahblah.com;

            

            access_log /opt/x/access.log;

            error_log /opt/x/error.log;



            root   /opt/x;

            index  index.html;



            location /abc/ {

                          proxy_set_header  X-Real-IP  $remote_addr;

                          proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;

                          proxy_set_header Host $http_host;

                          proxy_redirect off;



                          if (-f $request_filename/index.html) {

                                           rewrite (.*) $1/index.html break;

                          }



                          if (-f $request_filename.html) {

                                           rewrite (.*) $1.html break;

                          }



                          if (!-f $request_filename) {

                                           proxy_pass http://abc;

                                           break;

                          }

            }



            location /def/ {

                          proxy_set_header  X-Real-IP  $remote_addr;

                          proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;

                          proxy_set_header Host $http_host;

                          proxy_redirect off;



                          if (-f $request_filename/index.html) {

                                           rewrite (.*) $1/index.html break;

                          }



                          if (-f $request_filename.html) {

                                           rewrite (.*) $1.html break;

                          }



                          if (!-f $request_filename) {

                                           proxy_pass http://def;

                                           break;

                          }

            }







}

Open in new window

0
Comment
Question by:binele
  • 2
3 Comments
 
LVL 56

Expert Comment

by:Cliff Galiher
ID: 33533117
"some error?"
0
 

Author Comment

by:binele
ID: 33533306
sorry, was trying to recreate the error but it seems nginx is happy with the config now....

then started to test it but here's what's in the logs.


 [error] 4311#0: *18 open() "/opt/x/abc" failed (2: No such file or directory), client: x.x.x.x, server: blah.blahblah.com, request: "GET /abc HTTP/1.1", host: "blahblah.blah.com"


 [error] 4311#0: *18 open() "/opt/x/def" failed (2: No such file or directory), client: x.x.x.x, server: blah.blahblah.com, request: "GET /abc HTTP/1.1", host: "blahblah.blah.com"


Looks like it's not reverse proxying the request...

I've got my upstream defined

upstream abc {
        server 127.0.0.1:5060;
    }


and location defined

 location /abc/ {
                          proxy_set_header  X-Real-IP  $remote_addr;
                          proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
                          proxy_set_header Host $http_host;
                          proxy_redirect off;

                          if (-f $request_filename/index.html) {
                                           rewrite (.*) $1/index.html break;
                          }

                          if (-f $request_filename.html) {
                                           rewrite (.*) $1.html break;
                          }

                          if (!-f $request_filename) {
                                           proxy_pass http://abc;
                                           break;
                          }
            }

And am doing the same for "def"

Is the config correct or am I not getting this right? please advise.
0
 
LVL 56

Accepted Solution

by:
Cliff Galiher earned 500 total points
ID: 33533694
Well, the thing that leaps out at me is that the error you listed is complaining about finding /opt/.../def
/def and /def/ are very different when it comes to pattern matching so the "missing" trailing slash is not matching your location thus is falling through and not getting proxied.
-Cliff
 
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Have you ever sent email via ColdFusion and thought of tracking this mail to capture the exact date and time when the message was opened ?  If yes, then this article is for you ! First we need a table user_email with columns user_id , email , sub…
Most ColdFusion developers get confused between the CFSet, Duplicate, and Structcopy methods of copying a Structure, especially which one to use when. This Article will explain the differences in the approaches with examples; therefore, after readin…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now