解決tp5在linux服務器nginx環境404問題

1. 首先在nginx.conf里加上幾行代碼

             location ~ \.php$ {
                 root           /var/www/website/public;
                 fastcgi_pass   127.0.0.1:9000;
                 fastcgi_index  index.php;

                 fastcgi_split_path_info ^(.+\.php)(.*)$;     # 這句1
                 fastcgi_param PATH_INFO $fastcgi_path_info;    # 這句2

                 #  fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

                 fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; # 這句3
                 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; # 這句4

                 include        fastcgi_params;
             }

 2. 然後在location / 下增加 這四行代碼

  注: 此段開啓nginx的重寫模塊 訪問時不需要添加 index.php

                 if (!-e $request_filename) {            # 開啓nginx的重寫模塊
                     rewrite  ^(.*)$  /index.php?s=/$1  last;
                     break;
                 }

3. 最終配置

    server {
 54          listen       80;
 55          server_name tp.sunshihao.cn;
 56          location ~ \.php$ {
 57              root           /var/www/website/public;
 58              fastcgi_pass   127.0.0.1:9000;
 59              fastcgi_index  index.php;
 60              fastcgi_split_path_info ^(.+\.php)(.*)$;     #增加這一句  讓nginx 支持pathinfo
 61              fastcgi_param PATH_INFO $fastcgi_path_info;    #增加這一句
 62             #  fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
 63              fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; # 09-20+
 64              fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; # 09-20+
 65              include        fastcgi_params;
 66          }
 67          location / {
 68              root   /var/www/website/public;
 69              if (!-e $request_filename) {            # 開啓nginx的重寫模塊
 70                  rewrite  ^(.*)$  /index.php?s=/$1  last;
 71                  break;
 72              }
 73              index  index.php index.html index.htm;
 74          }
 75 
 76      }

然後就直接訪問 http://tp.sunshihao.cn/index/index/test (域名根據自己配置填寫)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章