rg8*_*g88 16 php mod-rewrite zend-framework nginx
我一直在研究的基于Zend Framework的站点现在正在迁移到它的生产服务器.这个服务器原来是nginx(惊喜!).当然,该站点无法正常工作,因为它是在Apache上开发的,并且依赖于htaccess文件.
我的问题是......任何人都有这方面的经验吗?关于如何将htaccess文件转换为nginx.conf文件的任何想法?我正在研究这个,但希望有人已经有过这方面的经验.谢谢!
编辑:这是当前的htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ /index.php [NC,L]
Run Code Online (Sandbox Code Playgroud)
小智 29
server {
listen 80; ## listen for ipv4
listen [::]:80 default ipv6only=on; ## listen for ipv6
server_name localhost;
access_log /var/log/nginx/localhost.access.log;
error_log /var/log/nginx/localhost.error.log;
root /var/www/localhost/public;
try_files $uri @php_index;
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location @php_index {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME /var/www/localhost/index.php;
include fastcgi_params;
}
}
Run Code Online (Sandbox Code Playgroud)
建议尽可能使用try_files.
stu*_*nti 16
我知道这是一个相当古老的线索,但无论如何它可能会帮助一些人.
基本上它将任何404错误重定向到index.php,但如果文件存在(类型文件),它将设置正确的根.
我是从头顶做到的.它可能不会马上工作,你必须把正确的路径和fastcgi配置.我还把所有东西都放回到index.php,因为它应该像Zend_Framework那样工作
error_page 404 = /index.php;
location / {
if (-f $request_filename) {
root /var/www;
}
}
location ~ \.php$ {
fastcgi_pass unix:/tmp/php.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/index.php;
include /etc/nginx/fastcgi_params;
}
Run Code Online (Sandbox Code Playgroud)
moo*_*are 13
我不知道转换htaccess文件的任何自动/系统方式,你可能必须手动完成.该Nginx的维基是nginx的文档的最佳资源.
编辑: 我现在在Nginx上运行Zend Framework,配置如下:
server {
listen 80;
server_name servername.com;
root /var/www/zendapp/public;
location / {
index index.php;
}
# Deny access to sensitive files.
location ~ (\.inc\.php|\.tpl|\.sql|\.tpl\.php|\.db)$ {
deny all;
}
location ~ \.htaccess {
deny all;
}
# Rewrite rule adapted from zendapp/public/.htaccess
if (!-e $request_filename) {
rewrite ^.*$ /index.php last;
}
# PHP scripts will be forwarded to fastcgi processess.
# Remember that the `fastcgi_pass` directive must specify the same
# port on which `spawn-fcgi` runs.
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
}
location = /50x.html {
root /var/www/default;
}
}
Run Code Online (Sandbox Code Playgroud)
如您所见,重写规则本身非常简单.