如何在使用Nginx重写的Zend Framework路由中避免index.php

我正试图从默认的Zend框架路线摆脱index.php。 我认为应该在服务器级而不是在应用程序上进行纠正。 (纠正我,如果我错了,但我认为这样做在服务器端更有效)。

我运行Nginx 0.7.1和php-fpm 5.3.3

这是我的nginxconfiguration

server { listen *:80; server_name domain; root /path/to/http; index index.php; client_max_body_size 30m; location / { try_files $uri $uri/ /index.php?$args; } location /min { try_files $uri $uri/ /min/index.php?q=; } location /blog { try_files $uri $uri/ /blog/index.php; } location /apc { try_files $uri $uri/ /apc.php$args; } location ~ \.php { include /usr/local/etc/nginx/conf.d/params/fastcgi_params_local; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_script_name; fastcgi_param SERVER_NAME $http_host; fastcgi_pass 127.0.0.1:9000; } location ~* ^.+\.(ht|svn)$ { deny all; } # Static files location location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js)$ { expires max; } } 

基本上www.domain.com/index.php/path/to/url和www.domain.com/path/to/url提供相同的内容。

我想用nginx重写来解决这个问题。

您需要在每个位置都有类似的位置 。 我只是写一个/blog位置的例子:

 location /blog/ { try_files $uri $uri/ @blog; } location @blog { rewrite ^/blog/(.*)$ /blog/index.php/$1 last; } 

在您的configuration中也发现:您可能想要在您的PHP位置使用fastcgi_split_path_info

 location ~ ^(.+\.php)(.*)$ { include fastcgi_params; fastcgi_split_path_info ^(.+\.php)(.*)$; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_pass localhost:9000; } 

详细信息请参阅文档 。

理想的解决scheme确实涉及到应用程序的支持,因为它应该了解它正在运行的path。 这允许它select正确的资源来服务,并返回正确的链接和redirect。 在这种情况下,nginxconfiguration看起来像这样:

 location / { include fastcgi_params; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_script_name; fastcgi_pass 127.0.0.1:9000; } 

即只需将所有内容[0]传递给特定的PHP脚本。 这可能实际上与Zend(我没有使用它自己)。

如果不能修改应用程序来理解这一点,那么事情就会因为重写path和修改内容而变得混乱。 重写的方法是确保重写的path不会被重写。 以下将重写/index.phppath并将它们传递给Zend。

 location ~ \.php(/|$) { include fastcgi_params; fastcgi_split_path_info ^(.+\.php)(.*)$; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_pass 127.0.0.1:9000; } location / { rewrite ^/(.*)$ /index.php/$1; } 

如果这不起作用,这可能是因为Zend没有正确处理请求 – 检查日志,看看它试图find什么path。

这不会导致在HTML中返回的链接或由Zend发送的redirect使用没有“index.php”的path,而nginx似乎没有修改这些的机制。 检查Zend是否有办法为链接和redirectconfiguration根path。

[0]你显然想要直接提供静态内容,我省略了这一点。

尝试类似的东西:

 rewrite ^/(.*)$ /index.php/$1; 
 # Rewrite rule adapted for Zend Framework location / { index index.php; if (!-f $request_filename) { rewrite ^(.*)$ /index.php last; } }