Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Nginx 如何将所有请求重定向到index.php,即使url中有.php_Nginx - Fatal编程技术网

Nginx 如何将所有请求重定向到index.php,即使url中有.php

Nginx 如何将所有请求重定向到index.php,即使url中有.php,nginx,Nginx,我有这个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;

我有这个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扩展名结尾

例如,这项工作:

但这会返回404,并且不会重定向到index.php


如何将不存在的扩展名为.php的url重定向到index.php?

我已经解决了这个问题。 首先,您应该注释掉这一行,或者从
snippets/fastcgi php.conf
文件中删除这一行

try_files $fastcgi_script_name =404;
然后在virtualhost配置中放入
try_files$uri$uri/index.phpinclude snippets/fastcgi-php.conf之前的code>
位置~\.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;
}

这应该可以做到。

尝试添加
Try\u文件$uri/index.php
到您的
location~\.php$
块。我尝试将其添加到
location~\.php$
块,但由于此错误,我无法重新加载nginx:
nginx:[emerg],/etc/nginx/snippets/fastcgi php.conf:5中的“try\u files”指令重复,请检查
snippets/fastcgi php.conf
文件的内容。您可以更改其中的
try\u files
语句,也可以将文件内容粘贴到
location
块中,以便在其中进行编辑。