我有这个nginx vhost配置:
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?
答案 0 :(得分:1)
我已经解决了问题。
首先,您应该注释掉这一行或从snippets/fastcgi-php.conf
文件中删除此行
try_files $fastcgi_script_name =404;
然后在您的virtualhost配置上,将try_files $uri $uri /index.php;
放在include snippets/fastcgi-php.conf;
块的location ~\.php$
之前。
location ~\.php$
块应如下所示:
location ~ \.php$ {
try_files $uri $uri /index.php;
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
应该可以解决问题的