如何使Zend框架2工作,nginx的? [英] How to make Zend Framework 2 work with nginx?
问题描述
我要让Zend框架2应用程序中的Nginx服务器上运行。我可以访问该网页,但我无法访问任何其他模块。我发现Nginx的需要重写规则来访问URL,如域/专辑。现在的问题是如何Zend框架2的默认的.htaccess规则转换为Nginx的重写规则? 这里是htaccess的规则:
RewriteEngine叙述在
的RewriteCond%{} REQUEST_FILENAME -s [OR]
的RewriteCond%{} REQUEST_FILENAME -l [OR]
的RewriteCond%{} REQUEST_FILENAME -d
重写规则^ * $ - [NC,L]
的RewriteCond%{REQUEST_URI} :: $ l ^(/.+)(+):: \ 2 $
重写规则^ - [E = BASE:%1](*)
重写规则^(。*)$%{ENV:BASE}的index.php [NC,L]
您真的不需要任何重写规则作出的Nginx和ZF2发挥好起来。这是一个非常简单的Nginx的配置,我用:
服务器{
听*:80;
服务器名域;
#字符集
字符集UTF-8;
#日志
访问日志/虚拟主机/域/日志/访问日志为主;
error_log中/虚拟主机/域/日志/ error_log中;
#目录索引
指数的index.php;
#文档根目录
根/虚拟主机/域/公;
# 位置
位置 / {
try_files $ URI $ URI /的index.php;
}
#错误页面
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
#PHP-FPM支持
位置〜\的.php $ {
fastcgi_pass UNIX:/usr/local/etc/php-fpm/DOMAIN.sock;
包括fastcgi.conf;
}
#阻止访问的.htaccess
位置〜\ {的.htaccess
拒绝所有;
}
}
当然,改变路径到你当前的设置。既然你不使用的是哪种类型的PHP安装的,我不能帮你那里,因为我目前使用PHP-FPM更不用说了。
使用这个非常简单的设置我所有的模块都工作正常。例如,我可以访问 http://example.com/some/url 或的 http://example.com/index.php/some/url
Nginx的也有ZF http://wiki.nginx.org/Zend_Framework#Time_for_nginx <一个简单的配置/ A>
修改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 $服务器名称;
#仅PHP,在PHP建成--enable-力的cgi-重定向要求
fastcgi_param REDIRECT_STATUS 200;
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 need rewrite rule to access the url such as domain/album. The question is how to convert the default .htaccess rule of Zend Framework 2 to Nginx rewrite rule? Here is the htaccess rule:
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 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;
这篇关于如何使Zend框架2工作,nginx的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!