简体   繁体   English

Nginx重写等效于Apache重写

[英]Nginx rewrite equivalent of apache rewrite

I am trying to change the configuration on my new Nginx server so it matches my current Apache settings. 我正在尝试在新的Nginx服务器上更改配置,使其与我当前的Apache设置匹配。 At the moment I am using this htaccess file: 目前,我正在使用此htaccess文件:

RewriteEngine on  
RewriteCond %{REQUEST_FILENAME} !-f  
RewriteCond %{REQUEST_FILENAME} !-d  
RewriteRule ^(.*) index.php?url=$1 [L,QSA]  

I have found a converter that could "translate" this into the Nginx equivalent. 我发现了一个可以将其“翻译”成Nginx等效文件的转换器。 I have tried the following: 我尝试了以下方法:

location / {
    if (!-e $request_filename){
     rewrite ^/(.*) /index.php?url=$1 break;
    }
    try_files $uri $uri/ =404;
}

But when I try to set a url query like this http://domain.tld/something my php file gets returned and downloaded and that ain't supposed to happen. 但是,当我尝试设置像这样的url查询http://domain.tld/something时,我的php文件就被返回并下载了,这不应该发生。

What I expect to happen is when a url like http://domain.tld/something is entered it gets treated like http://domain.tld/index.php?url=something 当像一个网址我希望发生的是//domain.tld/something:HTTP输入的它就会像对待HTTP://domain.tld/index.php URL =东西

Can someone tell me what I am doing wrong? 有人可以告诉我我在做什么错吗?

Unlike Apache, nginx doesn't come out-of-the-box ready to run PHP. 与Apache不同,nginx并非开箱即用即可运行PHP。 You need to setup a handler to deal with php files, otherwise nginx will serve them up just like any regular file. 您需要设置一个处理程序来处理php文件,否则nginx会像处理任何常规文件一样为它们提供服务。

See: Nginx downloads php instead of running it 请参阅: Nginx下载php而不是运行它

Also: review the NGINX documentation on using fast-cgi to run php . 另外:请阅读有关使用fast-cgi运行phpNGINX文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM