Solved

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

Posted on 2010-08-26
3
2,004 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Ruby, Call or invoke a API DLL library is easily via Win32API class, win32-api gem or other gems. For general DLL API call, there are quite a few references, some good tips list below: http://www.rubytips.org/2008/05/13/accessing-windows-api-fro…
Recently I spent hours debugging an issue in a Rails project where ActiveRecord was causing MySQL errors trying to create a User object of a class at the top level of a Single Table Inheritance model structure.  It turns out `.create` behaves differ…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

863 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

21 Experts available now in Live!

Get 1:1 Help Now