2017-07-28 8 views
2

我们有一个自定义PHP应用程序,我们使用.htaccess文件编写并运行在Apache上,以处理url重写。我们正试图将其转换为使用Plesk Onyx下的FPM在NGINX下工作。如何让NGINX在自定义PHP应用程序上正确地重写和执行?

应用程序生成类似链接:

https://somedomain.com/mypage (same as index/mypage) 
https://somedomain.com/index/sitemap 
https://somedomain.com/blog/some-article-name 

这些网址的映射到采取REQUEST_URI,并用它来渲染页面响应的index.php文件。

的应用程序的结构被嵌套如下:

docroot (/) 

./index.php //handler for the request in/

./blog/index.php //handler for any request to /blog 

每个的index.php期望接收的路径= {REQUEST_URI},以便它可以将请求映射到控制器和动作?。

我已经尝试了多种方式让NGINX使用tryfiles来重写,但没有运气。使用重写我可以/可以工作,但它不会渲染/ mypage或/ index/sitemap。

如果我尝试点击/ index/sitemap它下载index.php而不是执行它,如果我尝试博客发生同样的事情。实际上唯一可行的路径是/,所有其他路径都只是下载index.php文件。

这是我的配置,因为它是现在,我哪里出错了?

location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ { 
    expires 30d; 
    add_header Pragma public; 
    add_header Cache-Control “public”; 
    try_files $uri @fallback; 
} 

location/{ 
    #index index.php index.html index.html; 
    rewrite ^/([^?]*) /index.php?path=$1 break; 
    rewrite ^blog/([^?]*) /blog/index.php?path=$1 break; 
    #try_files $uri @fallback; 

}  

回答

1

您的配置有多个问题。我会忽略第一个location区块,因为它似乎与您的问题无关。

第一个rewrite将永远匹配,所以第二个rewrite将永远不会被咨询。第二个rewrite将永远不会匹配,因为nginx URI总是以/开头。 [^?]是没有意义的,因为rewrite使用规范化的URI,它不包含?或查询字符串。使用rewrite...break意味着重写的URI在相同的位置处理,这是一个错误,因为这个位置没有配备处理PHP文件。有关更多信息,请参阅this document

使用try_files可能看起来像这样的解决方案:

location/{ 
    try_files $uri $uri/ /index.php?path=$uri&$args; 
} 
location /blog { 
    try_files $uri $uri/ /blog/index.php?path=$uri&$args; 
} 
location ~ \.php$ { ... } 

更多见this document

相关问题