【发布时间】:2020-12-05 06:15:38
【问题描述】:
我在尝试进行站点配置时遇到了奇怪的 Nginx 行为。我需要 Nginx 使用 article.php 以防服务器上缺少文件。但它的工作原理很奇怪。如果查询是example.com/snaff,它将被重定向到article.php。但是如果查询是 example.com/snaff.php 它只会在浏览器中抛出 404 错误。
我使用 Ubuntu 20.04 + Nginx 1.18.0。早些时候,相同的配置在 Ubuntu 18.04 + Nginx 1.19.0 上运行良好。
不明白出了什么问题。
这是配置代码。
server {
listen 80;
server_name example.com www.example.com;
#return 404; # managed by Certbot
if ($host = www.example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
}
server {
listen 443 ssl http2;
server_name example.com www.example.com;
root /var/www/example.com/public_html/;
index index.php index.html index.htm;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
access_log /var/log/nginx/example_access.log;
error_log /var/log/nginx/example_error.log error;
etag on;
gzip on;
gzip_comp_level 5;
gzip_min_length 10;
gzip_proxied any;
gzip_types *;
gzip_disable "msie6";
client_max_body_size 15m;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
add_header Cache-Control "no-store, no-cache, must-revalidate";
}
location ~ /\.ht {
deny all;
}
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /article.php;
}
}
【问题讨论】: