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?
我想让Zend Framework 2应用程序在Nginx服务器中运行。我可以访问主页,但我无法访问任何其他模块。我发现Nginx需要一个重写规则来访问域名/专辑等URL。问题是如何将Zend Framework 2的默认.htaccess规则转换为Nginx重写规则?
Here are the .htaccess
rules:
这是.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]
2 个解决方案
#1
19
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:
你真的不需要任何重写规则来让Nginx和ZF2一起玩得很好。这是一个非常简单的Nginx配置,我用它:
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.
当然,请更改当前设置的路径。由于您没有提到您正在使用的PHP安装类型,因此我无法帮助您,因为我目前正在使用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
使用这个非常简单的设置,我的所有模块都按预期工作。例如,我可以访问http://example.com/some/url或http://example.com/index.php/some/url
Nginx also has a simple configuration for ZF http://wiki.nginx.org/Zend_Framework#Time_for_nginx
Nginx还有一个简单的ZF配置http://wiki.nginx.org/Zend_Framework#Time_for_nginx
Edit 1 - Added fastcgi_params config
编辑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;
#2
9
To follow up on Diemuzi's answer.
跟进Diemuzi的回答。
Please remember, that if you want query parameters to be working in your application, you have to pass them along with try_files
请记住,如果您希望查询参数在您的应用程序中运行,则必须将它们与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.
我发现的最佳方法是同时使用$ is_args和$ args。如果存在参数,$ is_args仅设置问号,$ args传递查询参数。
Replace
更换
try_files $uri $uri/ /index.php;
try_files $ uri $ uri / /index.php;
with
同
try_files $uri $uri/ /index.php$is_args$args;
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
现在,您可以在ZF2应用程序中正常使用查询参数,例如在默认控制器中
$query_params = $this->params()->fromQuery();
$ query_params = $ this-> params() - > fromQuery();
#1
19
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:
你真的不需要任何重写规则来让Nginx和ZF2一起玩得很好。这是一个非常简单的Nginx配置,我用它:
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.
当然,请更改当前设置的路径。由于您没有提到您正在使用的PHP安装类型,因此我无法帮助您,因为我目前正在使用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
使用这个非常简单的设置,我的所有模块都按预期工作。例如,我可以访问http://example.com/some/url或http://example.com/index.php/some/url
Nginx also has a simple configuration for ZF http://wiki.nginx.org/Zend_Framework#Time_for_nginx
Nginx还有一个简单的ZF配置http://wiki.nginx.org/Zend_Framework#Time_for_nginx
Edit 1 - Added fastcgi_params config
编辑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;
#2
9
To follow up on Diemuzi's answer.
跟进Diemuzi的回答。
Please remember, that if you want query parameters to be working in your application, you have to pass them along with try_files
请记住,如果您希望查询参数在您的应用程序中运行,则必须将它们与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.
我发现的最佳方法是同时使用$ is_args和$ args。如果存在参数,$ is_args仅设置问号,$ args传递查询参数。
Replace
更换
try_files $uri $uri/ /index.php;
try_files $ uri $ uri / /index.php;
with
同
try_files $uri $uri/ /index.php$is_args$args;
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
现在,您可以在ZF2应用程序中正常使用查询参数,例如在默认控制器中
$query_params = $this->params()->fromQuery();
$ query_params = $ this-> params() - > fromQuery();