Nginx + Passenger to serve rails apps in different

2019-04-15 12:15发布

I'm running a rails app in a Debian server (ip 192.168.1.193) with passenger as a standalone

$ cd /home/hector/webapps/first
$ passenger start -a 127.0.0.1 -p 3000

And I want to serve this app throw Nginx with reverse proxy in a different sub folder as:

http://192.168.1.193/first

My nginx.conf server:

...
server {
    listen 80;
    server_name 127.0.0.1;
    root /home/hector/webapps/first/public;
    passenger_base_uri /first/;
    location /first/ {
        proxy_pass http://127.0.0.1:3000;
        proxy_set_header Host $host;
    }
}
...

Then I run the Nginx server

$ /opt/nginx/sbin/nginx

With one rails app running with this configuration everything seems to work ok.

But when I try to add my second app

$ cd /home/hector/webapps/second
$ passenger start -a 127.0.0.1 -p 3001

with this nginx.conf file:

...
server {
    listen 80;
    server_name 127.0.0.1;
    root /home/hector/webapps/first/public;
    passenger_base_uri /first/;
    location /first/ {
        proxy_pass http://127.0.0.1:3000;
        proxy_set_header Host $host;
    }
}

server {
    listen 80;
    server_name 127.0.0.1;
    root /home/hector/webapps/second/public;
    passenger_base_uri /second/;
    location /second/ {
        proxy_pass http://127.0.0.1:3001;
        proxy_set_header Host $host;
    }
}
…

and I reload the Nginx server configuration

$ /opt/nginx/sbin/nginx -s reload
nginx: [warn] conflicting server name "127.0.0.1" on 0.0.0.0:80, ignored

I get a warning and I cannot access the second app from

http://192.168.1.193/second/ 

The server returns 404 for the second app and the first app is still running.

1条回答
【Aperson】
2楼-- · 2019-04-15 12:31

I think you just have to put both locations into the same server:

server {
  listen 80;
  server_name 127.0.0.1;

  location /first/ {
    root /home/hector/webapps/first/public;
    passenger_base_uri /first/;

    proxy_pass http://127.0.0.1:3000/;
    proxy_set_header Host $host;
  }
  location /second/ {
    root /home/hector/webapps/second/public;
    passenger_base_uri /second/;

    proxy_pass http://127.0.0.1:3001/;
    proxy_set_header Host $host;
  } 

}
查看更多
登录 后发表回答