Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.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上将所有请求重写为PHP脚本_Php_Apache_.htaccess_Mod Rewrite_Nginx - Fatal编程技术网

在Nginx上将所有请求重写为PHP脚本

在Nginx上将所有请求重写为PHP脚本,php,apache,.htaccess,mod-rewrite,nginx,Php,Apache,.htaccess,Mod Rewrite,Nginx,对于我的项目,我需要将所有请求重写到index.php,这是请求处理程序。 我用Apache中的.htaccess文件完成了这项工作,但现在我想让它也与Nginx兼容。我知道Nginx上不存在.htaccess文件,因此我必须在/etc/Nginx/sites available/default中编辑虚拟主机文件,但我不知道哪个是与我的旧Apache规则等效的Nginx。 以下是我的旧.htaccess文件的内容: RewriteEngine On RewriteBase / RewriteCo

对于我的项目,我需要将所有请求重写到index.php,这是请求处理程序。 我用Apache中的.htaccess文件完成了这项工作,但现在我想让它也与Nginx兼容。我知道Nginx上不存在.htaccess文件,因此我必须在/etc/Nginx/sites available/default中编辑虚拟主机文件,但我不知道哪个是与我的旧Apache规则等效的Nginx。 以下是我的旧.htaccess文件的内容:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !/index\.php
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^([^?]*)$ /index.php [QSA,NS,L]

在Apache上,所有请求都被重写为index.php,无论这些请求是php脚本、静态文件还是不存在的文件(在我的项目中还有一些虚拟路径,如/::API/或/::ADMIN/和根shorcut/~/),我如何使用Nginx做到这一点?

Nginx文档列出了重写指令:

简单的答案如下:

location / {
    rewrite ^(.*)$ /index.php?$1 last;
}

编辑以添加?$1位。谢谢,@bob0t

您忘记了QSA标志:这更像是
/index.php$query\u string
我的文件中有这样一个:
server{location/{rewrite^(.*)$/index.php$query\u string last;}location~\.php${try\u files$uri=404;fastcgi\u split\u path\u info^(.+.php)(/.+)$;fastcgi_pass unix:/var/run/php7.0-fpm.sock;fastcgi_index index.php;include fastcgi_params;}}
但是我在php脚本上得到了502:Bad Gateway,在虚拟路径(不存在文件)上得到了404,我搞错了什么?检查我的编辑。Bob0t说的是结果,而不是要做的更改。我已经完成了编辑,所以现在块是:
location/{rewrite^(.*)$/index.php?$1 last;}location~\.php${try\u files$uri=404;fastcgi\u split\u path\u info^(.+\.php)(/.+)$;fastcgi_pass unix:/var/run/php7.0-fpm.sock;fastcgi_index index.php;include fastcgi_params;}
但仍然是相同的错误谢谢,我已经检查了问题并理解了错误,这里是更正的配置块,可能对其他人也有用:
location/{rewrite^(.*)$/index.php?$1 last;}location~\.php${fastcgi\u pass unix:/var/run/php/php7.0-fpm.sock;fastcgi\u index.php;包括fastcgi\u参数;fastcgi\u参数脚本文件名/[网站路径]/index.php;}