【问题标题】:Nginx + Passenger to serve rails apps in different sub URIsNginx + Passenger 在不同的子 URI 中为 Rails 应用程序提供服务
【发布时间】:2013-03-29 20:38:59
【问题描述】:

我在 Debian 服务器 (ip 192.168.1.193) 中运行 rails 应用程序,乘客作为独立的

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

我想在不同的子文件夹中为这个应用程序抛出带有反向代理的 Nginx:

http://192.168.1.193/first

我的 nginx.conf 服务器:

...
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;
    }
}
...

然后我运行 Nginx 服务器

$ /opt/nginx/sbin/nginx

使用此配置运行一个 Rails 应用程序似乎一切正常。

但是当我尝试添加我的第二个应用时

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

使用这个 nginx.conf 文件:

...
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;
    }
}
…

然后我重新加载 Nginx 服务器配置

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

我收到警告,我无法访问第二个应用程序

http://192.168.1.193/second/ 

服务器为第二个应用返回 404,而第一个应用仍在运行。

【问题讨论】:

    标签: ruby-on-rails nginx passenger


    【解决方案1】:

    我认为您只需将两个位置放入同一服务器即可:

    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;
      } 
    
    }
    

    【讨论】:

    • 是的,使用此配置,两个应用程序都可以正常工作!谢谢萨沙。
    • 我刚刚意识到 proxy_pass 指令必须以斜杠结尾:proxy_pass http://127.0.0.1:3000/; 否则,您将收到路由错误。
    猜你喜欢
    • 2013-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-12
    • 2015-08-15
    • 1970-01-01
    • 2014-03-11
    • 1970-01-01
    相关资源
    最近更新 更多