Link to home
Start Free TrialLog in
Avatar of binele
bineleFlag for Australia

asked on

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

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

Avatar of Cliff Galiher
Cliff Galiher
Flag of United States of America image

"some error?"
Avatar of binele

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of Cliff Galiher
Cliff Galiher
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial