這份配置檔案能解決什麼問題?
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。