1. 程式人生 > 實用技巧 >關於THINKPHP 的 NGINX 配置,那些年才過的坑

關於THINKPHP 的 NGINX 配置,那些年才過的坑

THINKPHP 的 NGINX 配置踩坑

今天在用一個以 tp 為基礎的快速開發框架時遇到一些問題:

nginx 報錯截圖

為了方便說明進行手動換行

// 處理時重寫或內部重定向迴圈
2019/11/11 11:16:06 [error] 15164#15432: *1 rewrite or internal redirection cycle while processing 
    "/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index/user/index.html", 
    client: 127.0.0.1, 
    server: xxxxx, 
    request: "GET /index/user/index.html HTTP/1.1", 
    host: "xxxxx", 
    referrer: "xxxxx"

錯誤配置

參考 larvael 配置

server {
    .
    .
    .

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    .
    .
    .
    location ~ \.php$ {
        fastcgi_pass127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }
    .
    .
    .

}

發現所有路徑都一樣,都是首頁效果

初步判斷 nginx 重寫規則有問題

# 路徑 / 開頭之後都走這個匹配
# 如 /index /index/user 
location / {
    # $uri 本地有就返回,或者$uri/ 本地有目錄就返回,或者走後面的重寫
    # 本地是指在網站根目錄下,如 當 $uri=index 就是看根目錄下面有 index 檔案或者 index/ 目錄
    try_files $uri $uri/ /index.php?$query_string;
}

開始報錯

解決問題

網上查詢後 tp5 的配置應為

location / {
    try_files $uri $uri/ /index.php$uri?$query_string;
}

改後,發現問題沒解決;對比配置發現

  # location ~ \.php$ 改成  location ~ \.php(.*)$
    location ~ \.php(.*)$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param  PATH_INFO  $fastcgi_path_info;
        fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
        include fastcgi_params;
    }

解決,完整配置

server {
    listen       80;
    server_name  xxxxxxx ;
    root  www;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    charset utf-8;

    index index.html index.htm index.php;
    location / {
        try_files $uri $uri/ /index.php$uri?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;


    location ~ \.php(.*)$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param  PATH_INFO  $fastcgi_path_info;
        fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}

一些說明

在偽靜態配置用 try_files 而非 rewrite,是因為 rewrite 會對全部匹配(這裡是 /)請求都做一次匹配,如:host.com/a.png,這會會浪費一些資源(伺服器的算力等);同時也參考了 laravel 的官方配置加入一些安全設定。

如果在用到上線專案時最好加入靜態資源快取配置。

location ~* ^.+\.(css|js|ico|gif|jpg|jpeg|png)$ {
    expires 30d;
}

修復

2020-08-20 發現 get 沒有引數,原因是沒有 QUERY_STRING 為空,解決:

location / {
    # 帶上 query_string
    try_files $uri $uri/ /index.php$uri?$query_string;
}

點關注,不迷路

好了各位,以上就是這篇文章的全部內容了,能看到這裡的人呀,都是人才。之前說過,PHP方面的技術點很多,也是因為太多了,實在是寫不過來,寫過來了大家也不會看的太多,所以我這裡把它整理成了PDF和文件,如果有需要的可以

點選進入暗號: PHP+「平臺」


更多學習內容可以訪問【對標大廠】精品PHP架構師教程目錄大全,只要你能看完保證薪資上升一個臺階(持續更新)

以上內容希望幫助到大家,很多PHPer在進階的時候總會遇到一些問題和瓶頸,業務程式碼寫多了沒有方向感,不知道該從那裡入手去提升,對此我整理了一些資料,包括但不限於:分散式架構、高可擴充套件、高效能、高併發、伺服器效能調優、TP6,laravel,YII2,Redis,Swoole、Swoft、Kafka、Mysql優化、shell指令碼、Docker、微服務、Nginx等多個知識點高階進階乾貨需要的可以免費分享給大家,需要的可以加入我的 PHP技術交流群