nginx和公共子目录

时间:2013-02-21 00:59:54

标签: ruby-on-rails nginx

我是nginx服务器配置的新手。我在公共目录下的子目录下提供文件时遇到问题。

示例,文件在;不提供rails_app / public / uploads / client / 2 / image / 7 /目录或rails_app / public / picture /目录 但我没有遇到任何问题:rails_app / public / webcam.swf

就像某个子目录不服务。

我的production.rb文件

  # Disable Rails's static asset server (Apache or nginx will already do this)
  config.serve_static_assets = false

只有当我将serve_static_assets设置为true时我才没有问题

我的nginx配置:

upstream thin {
  server '127.0.0.1:3000';
  server '127.0.0.1:3001';
  server '127.0.0.1:3002';
  server '127.0.0.1:3003';
  server '127.0.0.1:3004';
}

server {
  listen 80 default deferred;
  # server_name example.com;
  root /home/user/appname/public;

  location ^~ /assets/ {
    root /home/user/appname/public;
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  location / {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://thin;
  }
  error_page 500 502 503 504 /500.html;
  client_max_body_size 4G;
  keepalive_timeout 10;
}

你可以帮我配置这个配置吗?

提前致谢。

1 个答案:

答案 0 :(得分:1)

我认为问题在于您的location /阻止了您的动态请求以及您的非资产静态请求。试试这个配置(注意try_files行):

server {
  listen 80 default deferred;
  # server_name example.com;
  root /home/user/appname/public;
  try_files $uri @app;

  location ^~ /assets/ {
    root /home/user/appname/public;
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  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://thin;
  }
  error_page 500 502 503 504 /500.html;
  client_max_body_size 4G;
  keepalive_timeout 10;
}