【发布时间】:2022-05-06 07:57:40
【问题描述】:
我有这个 nginx 虚拟主机配置:
server {
listen 8081;
server_name blocked_server;
root /home/gian/blocked_server;
access_log off;
error_log off;
index index.php index.html index.htm index.nginx-debian.html;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location ~ / {
try_files $uri $uri /index.php;
}
}
它将所有 url 重定向到 index.php,除非 url 以 .php 扩展名结尾。
例如这有效: http://localhost/somethingthatdontexist
但这会返回 404 并且不会重定向到 index.php http://localhost/somethingthatdontexist.php
如何将不存在的带有 .php 扩展名的 url 重定向到 index.php?
【问题讨论】:
-
尝试将
try_files $uri /index.php;添加到您的location ~ \.php$块中。 -
我尝试将其添加到
location ~\.php$块中,但由于此错误,我无法重新加载 nginx:nginx: [emerg] "try_files" directive is duplicate in /etc/nginx/snippets/fastcgi-php.conf:5 -
检查
snippets/fastcgi-php.conf文件的内容。您可以更改其中的try_files语句,或者将文件的内容粘贴到location块中,以便在那里进行编辑。
标签: nginx