简体   繁体   English

nginx位置块理解,并使用带有命名位置块的乘客

[英]nginx location block comprehension and using passenger with named location blocks

I have a couple of questions regarding my nginx configuration as it pertains to serving webp files as well as using named locations with try_files . 关于我的Nginx配置,我有几个问题,因为它与服务Webp文件以及将try_files一起使用命名位置有关。

Current config: 当前配置:

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;
    }
}

As it stands, nginx is not serving the webp files. 就目前而言,nginx不提供webp文件。 If I were to place add_header X-Webp-Uri "$uri$webp_suffix"; 如果我要放置add_header X-Webp-Uri "$uri$webp_suffix"; inside the first location block, the header is added. 在第一个location块内,添加标题。 If I were to put that in the second png/jpeg matching location block the header doesn't get set. 如果我将其放在第二个png / jpeg匹配location块中,则不会设置标头。 It was my understanding that regular expression location blocks are sequential (ie, it doesn't stop matching at the 1st match). 据我了解,正则表达式位置块是顺序的(即,它不会在第一次匹配时停止匹配)。

1) I want to serve webp images if present (my attempt at this is the 2nd location block). 1)我想提供webp图像(如果存在)(我的尝试是在第二个location块)。 Would having a nested location block help in this circumstance? 在这种情况下,使用嵌套的location块会有所帮助吗? I want to set gzip_static, expires, etc. for ALL files in /assets/, but I only want to serve webp version if they exist for certain file extensions within /assets/. 我想为/ assets /中的所有文件设置gzip_static,到期等,但如果/ assets /中的某些文件扩展名存在,我只想提供webp版本。

2) On another topic, I want to serve static .html files if present. 2)关于另一个主题,我想提供静态.html文件(如果存在)。 To do this (after looking up tutorials on the web) I need a combination of try_files and a named location block that points to an upstream application (Rails). 为此(在网上查找了教程之后),我需要try_files和指向上游应用程序(Rails)的命名位置块的组合。 However I can't seem to find out how to declare the upstream block if I'm using Passenger (installed using passenger-install-nginx-module ). 但是,如果使用乘客(使用passenger-install-nginx-module ),我似乎似乎找不到如何声明上游块。 The only configurations I can find for a Passenger/Nginx setup is to use passenger_enabled on; 我可以为“乘客/ Nginx”设置找到的唯一配置是使用“ passenger_enabled on;


EDIT: I found a sample configuration; 编辑:我找到了一个示例配置; here's an example (this ignores problem #1): 这是一个示例(它忽略了问题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;
    }
}

EDIT 2: I've discovered that nginx completely wipes out any instruction outside of the if block! 编辑2:我发现nginx完全清除了if块之外的所有指令!

This will result in only the Vary Accept header being set: 这将导致仅设置Vary Accept标头:

server {
    location ~* ^/images/.+\.(png|jpe?g)$ {
        add_header X-Whatever "Yo";
        if ($webp_suffix != "") {
          add_header Vary Accept;
        }
    }
}

This will result in both headers being set: 这将导致设置两个标头:

server {
    location ~* ^/images/.+\.(png|jpe?g)$ {
        if ($webp_suffix != "") {
          add_header Vary Accept;
          add_header X-Whatever "Yo";
        }
    }
}

EDIT 3: So now it's even more befuddling. 编辑3:所以现在更加令人困惑。 It's like any prior add_header that isn't within the last code block (location or if statement) is completely ignored. 就像不在最后一个代码块(位置或if语句)中的所有先前的add_header将被完全忽略。 ie, with the following, only the X-Whatever header is set: 即,使用以下命令,仅设置X-Whatever标头:

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!
    }
}

I had to remove the Vary Accept header condition and simply always apply it. 我必须删除Vary Accept标头条件,并始终应用它。 I don't know if that's good or bad but I don't have any other choice considering it just removes every other header I've applied! 我不知道这是好是坏,但是我没有其他选择,因为它只会删除我应用的所有其他标头! I also had to move the webp location block above the assets block and duplicate code which sucks. 我还必须将webp位置块移动到资产块上方,并复制重复的代码。

Working configuration: 工作配置:

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 "";
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM