Nginx 位置规则不适用

Nginx location rules not applying

我想 运行 在一个由 NGINX 服务器块配置的域(不是默认站点)上同时使用 WordPress 和 YOURLS。由于两者都需要以不同方式处理 URL,因此它们需要不同的 try_files 指令。 WordPress 位于域的根目录 (domain.tld),而 YOURLS 被安装到 /g/ 目录。尽管有两个位置规则,但我在 YOURLS 生成的任何链接上都会收到 404s(例如域。tld/g/linkname,所有这些都是重定向到外部 URL),尽管我可以访问管理后端。 据我所知,声明位置规则(一个用于 /g/,一个用于 /)应该足以让 NGINX 以不同的方式处理直接和 /g/ URL——我的想法有什么问题吗? try_files 规则是正确的,并且在其他单一应用程序服务器块上运行良好(WordPress 以及 YOURLS 在单独的服务器块上安装)。

服务器块定义配置如下所示:

server {
 listen [::]:80;
 listen 80;

 server_name domain.tld www.domain.tld;

 return 301 https://domain.tld$request_uri;

}

server {
 listen [::]:443 ssl;
 listen 443 ssl;

    ssl_certificate /etc/letsencrypt/live/domain.tld/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/domain.tld/privkey.pem;
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains";


 root /var/www/html/domain.tld;

 # Add index.php to the list if you are using PHP
 index index.html index.htm index.nginx-debian.html index.php;

 server_name domain.tld www.domain.tld;

 location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.2-fpm.sock;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors off;
 }

 location /g/ {
     try_files $uri $uri/ /yourls-loader.php$is_args$args;
        expires 14d;
        add_header Cache-Control 'public';
 }

 location / {
  try_files  $uri $uri/ /index.php?$args;
 }

 # deny access to .htaccess files, if Apache's document root
 # concurs with nginx's one
 #
 location ~ /\.ht {
  deny all;
 }
}

location /g/ try_files 指令的问题是 YOURLS 加载程序的路径不正确。如果 URL 处理程序 (yourls-loader.php) 位于 /g 目录中,则必须更改它的路径以包含 /g 目录:

try_files $uri $uri/ /g/yourls-loader.php$is_args$args;

位置规则并不意味着每个路径也从该位置处理,而是从上面给出的根路径处理。