Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Zend Framework 2 work with 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?

Here are the .htaccess rules:

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]
like image 573
如果有时间 Avatar asked Nov 01 '13 17:11

如果有时间


Video Answer


2 Answers

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 visit http://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

Edit 1 - Added fastcgi_params config

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;
like image 148
Diemuzi Avatar answered Sep 20 '22 13:09

Diemuzi


To follow up on Diemuzi's answer.

Please remember, that if you want query parameters to be working in your application, you have to pass them along with try_files

Best way i found is to use both $is_args and $args. $is_args sets only a question mark if there is arguments, and $args pass the query arguments.

Replace

try_files $uri $uri/ /index.php;

with

try_files $uri $uri/ /index.php$is_args$args;

Now you can use query params as normally in your ZF2 application for example in a default controller

$query_params = $this->params()->fromQuery();

like image 20
ksvendsen Avatar answered Sep 19 '22 13:09

ksvendsen