这份配置文件能解决什么问题?
1)php项目pathinfo模式的支持。
pathinfo模式URL比如:tpshop.com/index.php/home/Index。
如果URL没有index.php,例如:tpshop.com/home/Index/index.html。配置文件会重写(rewrite)url。
2)解决访问CSS,JS,图片等静态资源返回404的问题。
当前环境
windows+nginx1.14+php5.6
参考文章
https://blog.csdn.net/jo_andy/article/details/52598097
https://blog.jjonline.cn/linux/218.html(赞)
server {
listen 80;
server_name tpshop.com;
root D:/wamp/AppServ/www/v56/tpshop;
# 某些目录不允许访问
location ~* ^/(application|template|runtime)/.*\.(php|php5)$ {
deny all;
}
location / {
# location里也可以定义root目录
# root D:/wamp/AppServ/www/v56/tpshop;
# 把index.php添加到默认首页,就是输入/时自动打开/index.php
index index.html index.htm index.php;
# 因为pathinfo模式URL没有index.php
# 所以如果请求的文件不存在,则进行 URI 重写
# 在原有的基础上添加入口文件index.php
if (!-e $request_filename){
# 地址作为将参数rewrite到index.php上
rewrite ^/(.*)$ /index.php/$1 last;
break;
}
}
# css,js,img等静态资源访问
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
# img缓存30day
expires 30d;
break;
}
location ~ .*\.(js|css)?$ {
# js,css缓存12hour
expires 12h;
break;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php/?.*$ {
root D:/wamp/AppServ/www/v56/tpshop;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
# 配置pathinfo
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
## 在将这个请求的URI匹配完毕后,检查这个绝对地址的PHP脚本文件是否存在
## 如果这个PHP脚本文件不存在就不用交给php-fpm来执行了
## 否则页面将出现由php-fpm返回的:`File not found.`的提示
if (!-e $document_root$fastcgi_script_name) {
## 此处直接返回404错误
## 你也可以rewrite 到新地址去,然后break;
return 404;
}
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
注意:
有一个地方容易忽略,着重提醒一下。这里是这样的,
location ~ \.php/?.*$ {}
而不是
location ~ \.php${} #这是nginx默认配置的写法
下面的匹配规则并不匹配:https://blog.jjonline.cn/admin/index.php/Index/index 这样的url。