问题原因:
Apache支持伪静态规则在项目的入口目录有个.htaccess文件,Apache默认识别此文件内容,
但是Nginx不识别.htaccess文件,导致伪静态规则失效,从而无法解析url地址,导致404错误。解决办法:
Convert apache htaccess rewrite rules to nginx rewrite rules
打开网址 http://www.anilcetin.com/convert-apache-htaccess-to-nginx/
将项目入口目录的.htaccess内容复制到左侧框中进行转换
将转换的结果保存到nginx.htaccess,并在vhosts.conf中引入nginx.htaccess 如下图
重启nginx完成
下面给出laravel的.htaccess转换后的nginx.htaccess:
if (!-d $request_filename){ set $rule_0 1$rule_0; } if ($uri ~ "(.+)/$"){ set $rule_0 2$rule_0; set $bref_1 $1; } if ($rule_0 = "21"){ rewrite ^/ /$bref_1 permanent; } if (!-d $request_filename){ set $rule_1 1$rule_1; } if (!-f $request_filename){ set $rule_1 2$rule_1; } if ($rule_1 = "21"){ rewrite ^/ /index.php last; } if ($http_authorization ~ "."){ set $rule_2 1$rule_2; } if ($rule_2 = "1"){ ###setenv HTTP_AUTHORIZATION:$http_authorization; ###该行注释掉不然nginx启动失败 #ignored: "-" thing used or unknown variable in regex/rew }