对于我的nginx配置,我有几个问题,因为它涉及到服务webp文件以及在try_files中使用命名位置。
当前配置:
server {
listen 80;
server_name assets.manager manager;
passenger_app_env production;
passenger_ruby /home/web-server/.rvm/gems/ruby-2.2.1@manager/wrappers/ruby;
passenger_enabled on;
error_log /home/web-server/web-applications/manager/current/log/nginx-error.log;
root /home/web-server/web-applications/manager/current/public;
satisfy any;
allow 127.0.0.1;
allow 192.168.0.0/24;
deny all;
location ~ ^/assets/ {
gzip_static on;
expires max;
add_header Cache-Control public;
add_header Last-Modified "";
add_header ETag "";
}
location ~* .+\.(?:png|jpe?g|gif)$ {
if ($webp_suffix != "") {
add_header Vary Accept;
}
try_files $uri$webp_suffix $uri =404;
}
}目前,nginx并不为webp文件提供服务。如果我将add_header X-Webp-Uri "$uri$webp_suffix";放置在第一个location块中,则会添加标头。如果我将其放入第二个png/jpeg匹配的location块中,则不会设置标头。我的理解是正则表达式位置块是连续的(也就是说,在第一次匹配时它不会停止匹配)。
1)如果有的话,我想提供webp图片(我尝试的是第二个location块)。在这种情况下,使用嵌套的location块会有所帮助吗?我希望为/assets/中的所有文件设置webp、expires等,但我只希望在/assets/中某些文件扩展名存在的情况下提供webp版本。
2)在另一个主题上,如果存在静态.html文件,我希望提供服务。要做到这一点(在查找web上的教程之后),我需要一个try_files和一个指向上游应用程序(Rails)的命名位置块的组合。但是,如果我使用的是乘客(使用passenger-install-nginx-module安装),我似乎无法找到如何声明上游块。我能找到的乘客/Nginx设置的唯一配置是使用passenger_enabled on;
编辑:我找到了一个示例配置;下面是一个示例(这忽略了问题#1):
server {
listen 80;
server_name assets.staging.pos staging.pos;
passenger_app_env staging;
passenger_ruby /home/vagrant/.rvm/gems/ruby-2.2.1@pos/wrappers/ruby;
passenger_enabled on;
error_log /home/vagrant/rails/staging.pos/log/nginx-error.log;
root /home/vagrant/rails/staging.pos/public;
try_files $uri /cache/$uri /cache/$uri.html @app;
location @app {
proxy_set_header X-Forwarded-Proto http;
}
location ~* ^/images/.+\.(png|jpe?g)$ {
if ($webp_suffix != "") {
add_header Vary Accept;
}
try_files $uri$webp_suffix $uri =404;
}
}编辑2:我发现nginx完全清除了if块之外的任何指令!
这只会导致设置Vary Accept头:
server {
location ~* ^/images/.+\.(png|jpe?g)$ {
add_header X-Whatever "Yo";
if ($webp_suffix != "") {
add_header Vary Accept;
}
}
}这将导致设置两个标头:
server {
location ~* ^/images/.+\.(png|jpe?g)$ {
if ($webp_suffix != "") {
add_header Vary Accept;
add_header X-Whatever "Yo";
}
}
}编辑3:所以现在更让人迷惑了。这就像以前的任何不在最后一个代码块(location或if语句)中的add_header都被完全忽略了。例如,在以下情况下,只设置X-任何标题:
location ~ ^/assets/ {
gzip_static on;
expires max;
add_header Cache-Control public; # Ignored?!
add_header Last-Modified ""; # Ignored?!
add_header ETag ""; # Ignored?!
location ~* ^/assets/.+\.(?:png|gif|jpe?g)$ {
add_header X-Something "$uri$webp_suffix"; # Not ignored!
}
}发布于 2015-10-12 17:57:12
我必须删除Vary Accept标头条件,并始终应用它。我不知道这是好还是坏,但我没有其他选择,因为它只是删除了所有其他标题,我已经申请!我还必须移动webp位置块之上的资产块和重复的代码,这很糟糕。
工作配置:
server {
listen 80;
server_name assets.staging.pos staging.pos;
passenger_app_env staging;
passenger_ruby /home/vagrant/.rvm/gems/ruby-2.2.1@pos/wrappers/ruby;
passenger_enabled on;
error_log /home/vagrant/rails/staging.pos/log/nginx-error.log;
root /home/vagrant/rails/staging.pos/public;
# Try to return cached responses without hitting the app
try_files $uri /cache/$uri /cache/$uri.html @app;
location @app {
proxy_set_header X-Forwarded-Proto http;
}
# Return webp images when possible
location ~* ^/assets/.+\.(?:png|gif|jpe?g)$ {
expires max;
add_header Cache-Control public;
add_header Vary Accept;
add_header Last-Modified "";
add_header ETag "";
try_files $uri$webp_suffix $uri =404;
}
# Regular asset headers
location ~ ^/assets/ {
gzip_static on;
expires max;
add_header Cache-Control public;
add_header Last-Modified "";
add_header ETag "";
}
}https://stackoverflow.com/questions/33067091
复制相似问题