与其他与nginx不执行PHP文件相关的问题不同,问题出在我使用SEO而不是PHP脚本时,然后redirect到PHP脚本,而不是执行代码,它将脚本作为纯文本发送给浏览器。
基本上我想要的是每个请求发送到没有实际文件的HTTP服务器,index.php脚本将被执行,并且其输出返回给浏览器。 但是,发生什么事是我得到的index.php内的代码,而不是。
这是redirect完成的configuration文件的一部分。 它实际上是从以前的Apache的.htaccess文件中翻译过来的:
location / { if (!-e $request_filename){ rewrite ^(.*)$ /index.php break; } }
这些是PHP相关的指令,实际上这些指令在configuration文件中的前一个之前:
location ~ \.php$ { try_files /dd05cf208ebd3d4559f3af75016a1e3d.htm @php; } location @php { try_files $uri =404; include /etc/nginx/fastcgi_params; fastcgi_pass unix:/var/lib/php7.0-fpm/web4.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_intercept_errors on; }
我已经使用了mod_php几十年的Apache,等价的指令刚开始工作,但我是新的Nginx,我不知道从哪里开始debugging。
在nginx中实现前端控制器模式的正确方法如下:
location / { try_files $uri $uri/ /index.php; }
你的PHP位置块看起来有点奇怪。 我想你的configuration目标是让nginx在/dd05cf208ebd3d4559f3af75016a1e3d.htm显示维护通知,如果文件存在。
这个configuration对我来说看起来是正确的,除了你的@php块中的try_files使得请求失败,因为它使得nginx寻找名称在URI中指定的文件,而友好的URL则不存在这些文件。
没有理由包含它,所以你的PHPconfiguration应该如下所示。
location ~ \.php$ { try_files /dd05cf208ebd3d4559f3af75016a1e3d.htm @php; } location @php { include /etc/nginx/fastcgi_params; fastcgi_pass unix:/var/lib/php7.0-fpm/web4.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_intercept_errors on; }
您的search引擎优化解决scheme是使用不推荐的if指令内的location 如果是邪恶的 :
指令
if在location上下文中使用时有问题,在某些情况下,它不会做你期望的,而是完全不同的东西。 在某些情况下,甚至是段错误。 如果可能,避免它通常是一个好主意。
实现我相信你想要的正确方法是try_files ,例如:
location / { try_files $uri $uri/ @missing; } location @missing { rewrite ^(.*)$ /index.php break; }
进入服务器位置
error_page 404 =200 /index.php;
将所有404(未find)页面redirect到您的index.php
如果你只是想抓住所有缺失的页面,你不需要所有的重写和位置的东西。