在項目發開過程中使用的服務器基本上都是提前配置好的,而且配置好的服務器環境也不能說改就改,隨意的更改環境會導致服務器其他的功能失效,可能會引發很嚴重的錯誤,最近的項目開發中,有一個項目在測試服務器開發和測試完成之后,轉移到正式服務器,在轉移完成之后,發現網站除了首頁可以正常訪問,其他的所有連接均無法打開,研究了一下發現,之前的開發環境使用的是Apache服務器,而正式的服務器使用的是Nginx服務器,看似都是服務器但是其中的小差別還是很大的,所以導致了項目無法正常訪問。
如何解決這個問題呢,直接修改Nginx 的配置文件vhost.conf,直接上的服務器 配置(包含 https):
server{
listen 443;
server_name demo5.thinkcmf.com;
root 你的ThinkCMF5目錄/public; # 該項要修改為你準備存放相關網頁的路徑
ssl on;
ssl_certificate cert/demo5.thinkcmf.com.pem;
ssl_certificate_key cert/demo5.thinkcmf.com.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
location / {
index index.php index.html index.htm;
#如果請求既不是一個文件,也不是一個目錄,則執行一下重寫規則 if (!-e $request_filename)
{
#地址作為將參數rewrite到index.php上。
rewrite ^/(.*)$ /index.php?s=$1;
#若是子目錄則使用下面這句,將subdir改成目錄名稱即可。
#rewrite ^/subdir/(.*)$ /subdir/index.php?s=$1;
}
}
location /api/ {
index index.php index.html index.htm;
#如果請求既不是一個文件,也不是一個目錄,則執行一下重寫規則 if (!-e $request_filename)
{
#地址作為將參數rewrite到index.php上。
#rewrite ^/(.*)$ /index.php?s=$1;
#若是子目錄則使用下面這句,將subdir改成目錄名稱即可。
rewrite ^/api/(.*)$ /api/index.php?s=$1;
}
}
location ~* \/upload\/.+\.(html|php)$ {
return 404;
}
location ~* ^\/plugins\/.+\.(html|php)$ {
return 404;
}
location ~* \/themes\/.+\.(html|php)$ {
return 404;
}
#proxy the php scripts to php-fpm
location ~ \.php {
include fastcgi_params;
##pathinfo支持start
#定義變量 $path_info ,用于存放pathinfo信息
set $path_info "";
#定義變量 $real_script_name,用于存放真實地址
set $real_script_name $fastcgi_script_name;
#如果地址與引號內的正則表達式匹配 if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
#將文件地址賦值給變量 $real_script_name
set $real_script_name $1;
#將文件地址后的參數賦值給變量 $path_info
set $path_info $2;
}
#配置fastcgi的一些參數
fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
fastcgi_param SCRIPT_NAME $real_script_name;
fastcgi_param PATH_INFO $path_info;
###pathinfo支持end
fastcgi_intercept_errors on;
fastcgi_pass 127.0.0.1:9000;
}
}
server {
listen 80;
server_name demo5.thinkcmf.com;
rewrite ^/(.*) https://$server_name/$1 permanent;}在配置好基本環境之后,還要解決在低版本的Nginx中不支持PATHINFO環境變量,通過在Nginx.conf中配置規則實現
location / {
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=/$1 last;
}
}如果你的應用安裝在二級目錄,例如使用TP框架,Nginx的偽靜態方法設置如下,其中public是所在的目錄名稱。
location / {
if (!-e $request_filename) {
rewrite ^/public/(.*)$ /youdomain/index.php?s=/$1 last;
}
}



