Nginx似乎忽略了try_files命令

时间:2015-05-07 13:43:33

标签: ruby-on-rails nginx puma

我不确定我是否误解了try_files的工作原理,或者我的配置是否有问题。我在Nginx后面的Puma上运行Rails。我试图在我的/公共文件夹中提供项目,之后它应该将请求发送给Puma。

所以,如果我去 https://domain.com/sitemap.xml 它应该服务/var/www/live/current/public/sitemap.xml。然后,https://domain.com/rails_index_page应该来自Puma。

后者运行正常,我可以访问https://domain.com/public/sitemap.xml,但我想通过https://domain.com/sitemap.xml

访问它

我的配置文件如下所示:

upstream live {
  server unix:///var/www/live/shared/puma.sock;
}

server {
  client_body_buffer_size 10K;
  client_header_buffer_size 1k;
  client_max_body_size 8m;
  large_client_header_buffers 4 16k;
  client_body_timeout 12;
  client_header_timeout 12;
  keepalive_timeout 15;
  send_timeout 10;
  server_tokens off;

  listen 443;
  ssl on;
  ssl_certificate /etc/nginx/ssl/ssl-bundle.crt;
  ssl_certificate_key /etc/nginx/ssl/server.key;
  ssl_session_cache shared:SSL:20m;
  ssl_session_timeout 10m;
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  ssl_ciphers ECDH+AESGCM:ECDH+AES256:ECDH+AES128:DH+3DES:!ADH:!AECDH:!MD5;
  ssl_prefer_server_ciphers on;

  server_name domain.com; # change to match your URL
  root /var/www/live/current/public; # I assume your app is located at this location
  try_files $uri @live;

  location @live {
    add_header Strict-Transport-Security "max-age=31536000";
    gzip on;
    gzip_static on;
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 8;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    proxy_pass http://live; # match the name of upstream directive which is defined above
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }

  location ~ ^/assets/ {
    expires 1y;
    add_header Cache-Control public;
    root /var/www/live/current/public;
    gzip on;
    gzip_static on;
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 8;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
    add_header Last-Modified "";
    add_header ETag "";
  }

  set $rootUrl "/var/www/live/current";

  location ~* \.(?:ico)$ {
    root $rootUrl;
    expires 30d;
    add_header Cache-Control public;
    access_log off;
  }

  # css and js are tokenized
  location ~* \.(?:css|js) {
    root $rootUrl;
    expires max;
    add_header Cache-Control public;
    access_log off;
  }

  # nginx gzip_static does not add Vary header for fonts. 
  location ~* \.(?:eot|ttf|svg)$ {
    root $rootUrl;
    expires max;
    add_header Access-Control-Allow-Origin *;
    add_header Vary Accept-Encoding;
    add_header Cache-Control public;
    access_log off;
  }

  # woff fonts should not be zipped.
  location ~* \.(?:woff)$ {
    root $rootUrl;
    add_header Access-Control-Allow-Origin *;
    expires max;
    add_header Cache-Control public;
    access_log off;
  }

  # tokenized images can be cached forever 
  location ~* "\.([a-z0-9]{8})\.(?:gif|png|jpe?g)$" {
    root $rootUrl;
    expires max;
    add_header Cache-Control public;
    access_log off;
    add_header Access-Control-Allow-Origin *;
  }

  # non tokenized images only cache for 1 week as they are in my context subject to change.
  location ~* \.(?:gif|png|jpe?g)$ {
    add_header Access-Control-Allow-Origin *;
    root $rootUrl;
    expires 1w;
    add_header Cache-Control public;
    access_log off;
  }
}

我确定配置文件可以进行相当多的更新/优化,所以请不要对此大声喊叫。 :)

1 个答案:

答案 0 :(得分:0)

添加默认位置块有帮助吗?像

这样的东西
location / {
  try_files $uri @live;
  root ... ;
}

尝试文件在服务器上下文中有效,但我不知道哪些订单位置指令匹配(并且落到服务器默认值)

没有格式化的道歉,我在移动设备上,如果它有帮助我会修复它。