nginx 嵌套位置 try_files 优先级

nginx 嵌套位置 try_files 优先级

应用规则的实际顺序是什么:index、location、try_files?

配置:

server {
    listen 127.0.0.1:80;
    listen [::1]:80;
    server_name localhost;
    root /www/public;
    index index.html;

    location / {
        try_files $uri $uri/ /index.php;

        location ~ \.php$ {
            include /etc/nginx/fastcgi_params;
            fastcgi_pass unix:/var/run/php-fpm.sock;
            fastcgi_param SCRIPT_FILENAME /www/public$fastcgi_script_name;
        }
    }
}

预期行为:

  1. uri=http://localhost/test.php
  2. 匹配“位置/”
  3. try_files test.php 不
  4. try_files test.php/ 不
  5. try_files /index.php 匹配
  6. 匹配嵌套的“location ~ \.php$”

实际行为:

  1. uri=http://localhost/test.php
  2. 匹配嵌套的“location ~ \.php$”

我是否预料到了错误的行为?

編輯:

所以我必须在每个位置部分尝试一下_files?

server {
    ...
    location / {
        try_files $uri $uri/ /index.php;

        location ~ \.php$ {
            try_files $uri /index.php; # added line
            ...
        }
    }
}

相关内容