Debian 9 - nginx 忽略位置块

Debian 9 - nginx ignores location block

以下是我的nginxmydomain.conf文件内容

server {
    server_name  www.mywebsite.com;

#the below works
add_header "this" "works";

    root /var/www/yii2app/web;
    index index.php;

    charset utf-8;
    access_log  off;
    error_log   off;

    location ~* \.(txt|js|json|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar|svg)$ {
        try_files $uri =404;
    }

    location /img {
        add_header Cache-Control "public, no-transform";
        add_header "hello" "word";
        expires max;
    }

    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www/yii2app/web/$fastcgi_script_name;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        include        fastcgi_params;
    }

}

我正在尝试配置 cache-control,当我意识到我的位置块没有被 nginx 读取或接受时。

我通过添加一些额外的 header key-values 来测试它,例如:

add_header "this" "works";

add_header "hello" "word";

当我使用 curl 进行测试时,我可以看到 "this" "works",但在 /img 位置块中看不到 "hello" "world"...

curl -I http://www.mywebsite.com/img/test.gif

我做错了什么?上面的配置是我目前的确切配置,除了网站地址和根路径。我已经注释掉所有其他配置行。

正则表达式位置块按顺序计算并优先于普通前缀位置块。

URI /img/foo.gif 将优先匹配 location ~* \.(txt|js|json|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar|svg)$ 块而不是 location /img 块,与顺序无关。

您可以使用 ^~ 修饰符为前缀位置赋予更高的优先级。

例如:

location ^~ /img { ... }

详情见this document

或者,如果您不需要 add_header 部分,您可以使用 .