如何使 Zend Framework 2 与 nginx 一起工作? [英] How to make Zend Framework 2 work with nginx?

查看:23
本文介绍了如何使 Zend Framework 2 与 nginx 一起工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让 Zend Framework 2 应用程序在 Nginx 服务器中运行.我可以访问主页,但我无法访问任何其他模块.我发现Nginx需要重写规则来访问域/相册等URL.问题是如何将 Zend Framework 2 的默认 .htaccess 规则转换为 Nginx 重写规则?

I want to make Zend Framework 2 application to run within Nginx server. I could access the homepage, but I could not access any other modules. I found that Nginx needs a rewrite rule to access the URL such as domain/album. The question is how to convert the default .htaccess rules of Zend Framework 2 to Nginx rewrite rule?

这里是 .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]

推荐答案

你真的不需要任何重写规则来让 Nginx 和 ZF2 一起玩得很好.这是我使用的一个非常简单的 Nginx 配置:

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;
    }
}

当然可以更改当前设置的路径.由于您没有提到您使用的是哪种类型的 PHP 安装,我无法为您提供帮助,因为我目前正在使用 PHP-FPM.

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.

使用这个非常简单的设置,我的所有模块都按预期工作.例如,我可以访问 http://example.com/some/urlhttp://example.com/index.php/some/url

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

Nginx 也有一个简单的 ZF 配置 http://wiki.nginx.org/Zend_Framework#Time_for_nginx

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

编辑 1 - 添加了 fastcgi_params 配置

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  HTTPS              $https if_not_empty;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;

这篇关于如何使 Zend Framework 2 与 nginx 一起工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆