【问题标题】:Nginx seem to be ignoring try_files orderNginx 似乎忽略了 try_files 顺序
【发布时间】:2015-05-08 20:51:31
【问题描述】:

不确定我是否误解了 try_files 的工作方式,或者我的配置是否有问题。我在 Nginx 后面的 Puma 上运行 Rails。我正在尝试在我的 /public 文件夹中提供项目,之后它应该将请求发送给 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;
  }
}

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

【问题讨论】:

  • 你说你想服务/var/www/live/public/sitemap.xml,然后你的root指向一个完全不同的文件夹/var/www/live/current/public。是您的问题有误,还是您的配置有误?
  • 啊,这是从示例中删除标识信息的错字。我现在已经更正了。谢谢!

标签: ruby-on-rails nginx puma


【解决方案1】:

添加默认位置块有帮助吗?类似的东西

location / {
  try_files $uri @live;
  root ... ;
}

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

抱歉没有格式化,我在手机上,如果有帮助我会修复它。

【讨论】:

  • 我已经在服务器块中添加了位置 /,但它看起来不起作用。 :(
猜你喜欢
  • 2022-01-23
  • 2021-10-28
  • 2016-09-30
  • 2016-03-16
  • 2014-02-03
  • 2012-03-11
  • 2013-09-07
  • 2016-08-12
  • 1970-01-01
相关资源
最近更新 更多