【问题标题】:Rails app served via Passenger standalone behind an nginx proxyRails 应用程序通过 nginx 代理后面的Passenger 独立服务
【发布时间】:2015-06-09 21:35:20
【问题描述】:

我有两个应用程序:/foo/bar。在每个文件夹中,我都启动了乘客。对于 foo:

passenger start -d -e production -p 4000

对于酒吧:

passenger start -d -e production -p 4001

然后我将 nginx 配置如下:

server {
  listen 80 default_server;
  server_name www.server.com;
  root /var/www/html;

  location /foo/ {
      proxy_pass http://0.0.0.0:4000/;
      proxy_set_header Host $host;
  }

  location /bar/ {
      proxy_pass http://0.0.0.0:4001/;
      proxy_set_header Host $host;
  }
}

应用程序正在提供服务,但所有链接均无效。 users#index 操作的链接返回为 '/users' 而不是 '/foo/users'

  • 我在两个应用程序中都设置了 config.relative_url_root,这有助于资产而不是链接。
  • _url_path 方法我都试过了,都不管用。
  • 这个 answer 很接近,但 passenger_base_uri 不是股票 nginx 的有效指令。
  • 然后我按照 advanced configuration instructions 为乘客的 nginx 配置添加了 passenger_base_uri = '/foo'; 到我的自定义 conf 文件并像这样加载它:

    passenger start -d -e production -p 4000 --nginx-config-template nginx.conf.erb

仍然没有爱,我没有想法。有没有人这样做过?这似乎是在生产中部署多个应用的​​常用方法。


更多想法 (2015-06-05)

passenger_base_uri = '/foo' 添加到我的 nginx.conf.erb 文件中将应用程序托管在两个位置(这对我来说很奇怪,但无论如何):

  • localhost:4000/
  • localhost:4000/foo/

第一个没有正确的资源链接(即它只是 '/users')但可以访问其资产。

第二个有正确的资源链接(例如'/foo/users')但没有它的资产(这是因为它在其public文件夹中寻找/foo/assets/*,而不仅仅是@987654340 @)。我相信这是要走的路,因为我可以像这样更改我的代理以获取应用程序:

location /foo/ {
    proxy_pass http://0.0.0.0:4000/foo/;
    proxy_set_header Host $host;
}

还有其他人有什么想法吗?如果我这样做,则意味着我必须将我的资产转入 public/foo 才能正常工作。不是世界末日,但它仍然看起来很奇怪。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 nginx passenger


    【解决方案1】:

    对于其他想要做同样事情的人来说,最后是这样的:

    1. 按照Advanced Configuration 获取项目特定的nginx.conf.erb 文件。
    2. 为您的应用向该文件添加 passenger_base_uri 指令(例如passenger_base_uri = '/foo';
    3. 在您的 config/environments/production.rb 文件中移动资产的位置:config.assets.prefix = '/foo/assets'
    4. 开始乘客passenger start -d -e production -p SOME_PORT --nginx-config-template nginx.conf.erb
    5. 在你的 nginx 代理配置中为你的应用添加一个 location 指令:

      location /foo/ {
          proxy_pass http://0.0.0.0:SOME_PORT/foo/;
      
          proxy_buffering off;
          proxy_http_version 1.1;
      
          proxy_set_header Connection $connection_upgrade;
          proxy_set_header Host $host; # more robust than http_host
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # this ensures your app's env is correct
          proxy_set_header X-Forwarded-Host $host;
          # proxy_set_header X-Forwarded-Proto https; # add this if you always want the redirects to go to HTTPS
      }
      

    之后,(重新)启动你的 nginx 代理,你应该擅长http://your_proxy/foo/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-21
      • 2020-04-30
      • 1970-01-01
      • 2013-10-31
      • 1970-01-01
      • 2015-10-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多