Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2031
  • Last Modified:

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

0
binele
Asked:
binele
  • 2
1 Solution
 
Cliff GaliherCommented:
"some error?"
0
 
bineleAuthor Commented:
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
 
Cliff GaliherCommented:
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now