ruby-on-rails - 在为 unicorn rails 服务器提供服务的同时,如何让 nginx 从两个位置提供静态文件?

标签 ruby-on-rails nginx unicorn

好的,所以我有相当多的标准 nginx 配置来为 unicorn rails 服务器提供服务(监听套接字文件并提供来自 rails_app/public 目录的静态文件)。

但是,我想执行以下操作:

  1. 提供静态文件 rails_app/public(目前是 完成)
  2. 从不同的根目录(如/mnt/files/)提供带有 url/reports/的静态文件

我尝试将以下内容添加到我的 nginx 配置中:

location /reports/ {
    root /mnt/matthew/web;
}

但是没用。

有什么想法可以实现吗?

(下面是我的整个 nginx.conf 文件:

worker_processes 1;

pid /tmp/nginx.pid;
error_log /tmp/nginx.error.log;

events {
  worker_connections 1024; # increase if you have lots of clients
  accept_mutex off; # "on" if nginx worker_processes > 1
  # use epoll; # enable for Linux 2.6+
  # use kqueue; # enable for FreeBSD, OSX
}

http {
  # nginx will find this file in the config directory set at nginx build time
  include mime.types;

  # fallback in case we can't determine a type
  default_type application/octet-stream;

  # click tracking!
  access_log /tmp/nginx.access.log combined;
  sendfile on;

  tcp_nopush on; # off may be better for *some* Comet/long-poll stuff
  tcp_nodelay off; # on may be better for some Comet/long-poll stuff
  gzip on;
  gzip_http_version 1.0;
  gzip_proxied any;
  gzip_min_length 500;
  gzip_disable "MSIE [1-6]\.";
  gzip_types text/plain text/html text/xml text/css
             text/comma-separated-values
             text/javascript application/x-javascript
             application/atom+xml;

  # this can be any application server, not just Unicorn/Rainbows!
  upstream app_server {
    server unix:/home/matthew/server/tmp/unicorn.sock fail_timeout=0;


  }

  server {
    # enable one of the following if you're on Linux or FreeBSD
    listen 80 default deferred; # for Linux
    # listen 80 default accept_filter=httpready; # for FreeBSD


    client_max_body_size 4G;
    server_name _;

    keepalive_timeout 5;

    location /reports/ {
        root /mnt/matthew/web;
    }
    # path for static files
    root /home/matthew/server/public;



    try_files $uri/index.html $uri.txt $uri.html $uri @app;

    location @app {
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect off;

      proxy_pass http://app_server;
    }

    # Rails error pages
    error_page 500 502 503 504 /500.html;
    location = /500.html {
      root public;
    }
  }
}

最佳答案

location @app 正在寻找 /home/matthew/server/public 中的文件,因为那是指定的父根目录。如果您的 try files 语句匹配 location/reports/ 中具有不同根目录的文件,则不会找到这些文件。你需要像这样设置:

location /reports/ {
    root /mnt/matthew/web;
    try_files $uri/index.html $uri.txt $uri.html $uri @foo;
}
root /home/matthew/server/public;
try_files $uri/index.html $uri.txt $uri.html $uri @app;

location @foo {
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header Host $http_host;
  proxy_redirect off;

  proxy_pass http://app_server;

  root /mnt/matthew/web
}

location @app {
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header Host $http_host;
  proxy_redirect off;

  proxy_pass http://app_server;
}

关于ruby-on-rails - 在为 unicorn rails 服务器提供服务的同时,如何让 nginx 从两个位置提供静态文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4854479/

相关文章:

python - 如何修复 "Error during WebSocket handshake: Unexpected response code: 400"错误

ruby-on-rails - unicorn 请求排队

bundler - 使用 bluepill 监控 bundle 执行 unicorn_rails

ruby-on-rails - PGError : ERROR: unrecognized time zone name: "UTC"

ruby-on-rails - 无法在 Ubuntu 13.04 上使用 RVM 安装 Ruby on Rails

Nginx 重写规则指南

apache - nginx 将所有域重定向到另一个端口,但为管理应用程序保留一个域

ruby-on-rails - 使用 nginx 和 unicorn : 403 forbidden error 进行 rails 部署

ruby-on-rails - rails 4 : Create new records through has_many association

ruby-on-rails - Rails 中的可选或条件模型关联