手上有一个网站部署在Apache环境下,现在要换服务器,新服务器是Nginx环境,只能修改规则了。
网上有不少apache转nginx的工具,转换后并不能使用。转换过后的规则如下。
if (!-f $request_filename){ set $rule_0 1$rule_0; } if (!-d $request_filename){ set $rule_0 2$rule_0; } if ($rule_0 = "21"){ rewrite ^(.*)$ /index.php/$1 last; }
这种规则,访问内页会报错。
分析代码,应该是Nginx没有开启pathinfo.
再次修改代码
location / { if (!-e $request_filename) { rewrite ^/(.*)$ /index.php/$1 last; break; } } location ~ \.php(.*)$ { include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $1; fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_index index.php; }
内页倒是可以访问了,但是在页面里的url也变了。原来的url是 /news/help_all,现在变成 /index.php//news/help_all。规则还是有问题。应该还是pathinfo设置的问题。
最终代码….
location / { if (!-e $request_filename) { rewrite ^/(.*)$ /index.php/$1 last; break; } } location ~ \.php(.*)$ { include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_split_path_info ^(.+?\.php)(/.*)$; set $path_info $fastcgi_path_info; fastcgi_param PATH_INFO $path_info; fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; fastcgi_pass unix:/tmp/php.sock; fastcgi_index index.php; }