記錄 nginx和php安裝完后的URL重寫,訪問空白和隱藏index.php文件的操作方法


1 sudo cd /etc/nginx/;
2 sudo vi fastcgi_params;

 

1.URL重寫

如果你的url參數不是用?xxx傳遞,而是自定義的,比如用/xx/xx/xx的方式傳遞,那么在nginx下就要開啟pathinfo

典型配置:

1 location ~ \.php$ {
2     root           html;
3     fastcgi_pass   127.0.0.1:9000;
4     fastcgi_index  index.php;
5     fastcgi_param  SCRIPT_FILENAME  $DOCUMENT_ROOT$fastcgi_script_name;
6     include        fastcgi_params;
7 }

修改1,6兩行,支持pathinfo

1 location ~ \.php(.*)$ { # 正則匹配.php后的pathinfo部分
2     root html;
3     fastcgi_pass   127.0.0.1:9000;
4     fastcgi_index  index.php;
5     fastcgi_param  SCRIPT_FILENAME  $DOCUMENT_ROOT$fastcgi_script_name;
6     fastcgi_param PATH_INFO $1; # 把pathinfo部分賦給PATH_INFO變量
7     include        fastcgi_params;
8 }

2.當URL重寫后,比如訪問xxx/index.php/xxx/xxx 會空白,而用?方式訪問則正常

  a.修改fastcgi_params文件

sudo cd /etc/nginx/;
sudo vi fastcgi_params;

 

配置文件內容:

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; #這行沒有就添加,有了就改成這樣子

 

  b.修改nginx配置文件

  

location ~ .php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name; # 新增

       include fastcgi_params; # 新增
}

 

 

3.隱藏入口文件

  a.如果代碼就在域名指向的根目錄下,則

location / { // …..省略部分代碼
   if (!-e $request_filename) {
   rewrite  ^(.*)$  /index.php?s=$1  last;
   break;
    }
}

 

  b.如果代碼在域名指向的下級目錄下,則

  

   #youdomain 是所在目錄的名稱。比如域名根目錄為/var/www/html  代碼所在目錄為/var/www/html/wave/project,則此處youdomain均替換為wave/projetc
  location /youdomain/ { if (!-e $request_filename){ rewrite ^/youdomain/(.*)$ /youdomain/index.php?s=$1 last; } }

 

  ===

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM