Zend Framework 2在Nginx服务器下无法路径重写的解决办法

今天我在我的pcDuino上添加Zend Framework 2应用程序,在Nginx中添加了Server的配置之后打开网站,发现首页没有任何问题,但是其他的Module都无法进入,我想想感觉问题应该出在路径重写的问题上。我想要不我把.htaccess的规则改写成Nginx的rewrite规则吧!然后我打开.htaccess,内容如下:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule ^(.*)$ %{ENV:BASE}index.php [NC,L]


You really do not need any rewrite rules to make Nginx and ZF2 to play nice together. Here is a very simple Nginx configuration which I use:

server {
    listen *:80;
    server_name DOMAIN;

    # Character Set
    charset utf-8;

    # Logs
    access_log /vhosts/DOMAIN/logs/access_log main;
    error_log /vhosts/DOMAIN/logs/error_log;

    # Directory Indexes
    index index.php;

    # Document Root
    root /vhosts/DOMAIN/public;

    # Location
    location / {
        try_files $uri $uri/ /index.php;

    # Error Pages
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;

    # PHP-FPM Support
    location ~ \.php$ {
        fastcgi_pass unix:/usr/local/etc/php-fpm/DOMAIN.sock;
        include fastcgi.conf;

    # Block access to .htaccess
    location ~ \.htaccess {
        deny all;

Of course change the paths to your current setup. Since you did not mention what type of PHP installation you are using I can’t help you there because I am currently using PHP-FPM.

Using this very simple setup all my modules are working as expected. For example I could visithttp://example.com/some/url OR http://example.com/index.php/some/url

Nginx also has a simple configuration for ZF http://wiki.nginx.org/Zend_Framework#Time_for_nginx



location / {
    try_files $uri $uri/ /index.php;


location ~ \.htaccess {
    deny all;


Tonitech版权所有 | 转载请注明出处: http://www.tonitech.com/2151.html

《Zend Framework 2在Nginx服务器下无法路径重写的解决办法》上有1条评论