配置nginx支援pathinfo功能
nginx模式不支援pathinfo模式,類似info.php/hello形式的url會被提示找不到頁面。下面的通過正則找出實際檔案路徑和pathinfo部分的方法,讓nginx支援pathinfo。
location ~ .php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
##通過設定模擬出pathinfo
set $path_info “”;
set $real_script_name $fastcgi_script_name;
if ($fastcgi_script_name ~ “^(.+?.php)(/.+)$”) {
set $real_script_name $1;
set $path_info $2;
}
fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
fastcgi_param SCRIPT_NAME $real_script_name;
fastcgi_param PATH_INFO $path_info;
include fastcgi_params;
}
要點:
1.~ .php 後面不能有$ 以便能匹配所有 *.php/* 形式的url
2. 通過設定更改 SCRIPT_FILENAME
我在實際使用張將這段程式碼融合到了fastcgi_params中。下面是我的nginx配置檔案示例:
配置虛擬主機部分,支援pathinfo的nginx程式碼如下:
## 在nginx.conf的server部分:
server {
listen 8080;
server_name localhost;
location ~ .php {
include fastcgi.conf;
}
}
##要點: .php 後面沒有$,以便匹配所有 *.php/* 形式
##重點程式碼見 fastcgi.conf 開頭部分
fastcgi.conf 程式碼如下:
fastcgi_pass 127.0.0.1:9000;
##fastcgi_index index.php;
set $path_info “”;
set $real_script_name $fastcgi_script_name;
if ($fastcgi_script_name ~ “^(.+?.php)(/.+)$”) {
set $real_script_name $1;
set $path_info $2;
}
fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
fastcgi_param SCRIPT_NAME $real_script_name;
fastcgi_param PATH_INFO $path_info;
## 以上是支援pathinfo的重點部分
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
#fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#fastcgi_param SCRIPT_NAME $fastcgi_script_name; 這兩行是需要註釋掉的。請注意
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
# PHP only, required if PHP was built with –enable-force-cgi-redirect
#fastcgi_param REDIRECT_STATUS 200;
location ~ .php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
##通過設定模擬出pathinfo
set $path_info “”;
set $real_script_name $fastcgi_script_name;
if ($fastcgi_script_name ~ “^(.+?.php)(/.+)$”) {
set $real_script_name $1;
set $path_info $2;
}
fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
fastcgi_param SCRIPT_NAME $real_script_name;
fastcgi_param PATH_INFO $path_info;
include fastcgi_params;
}
要點:
1.~ .php 後面不能有$ 以便能匹配所有 *.php/* 形式的url
2. 通過設定更改 SCRIPT_FILENAME
我在實際使用張將這段程式碼融合到了fastcgi_params中。下面是我的nginx配置檔案示例:
配置虛擬主機部分,支援pathinfo的nginx程式碼如下:
## 在nginx.conf的server部分:
server {
listen 8080;
server_name localhost;
location ~ .php {
include fastcgi.conf;
}
}
##要點: .php 後面沒有$,以便匹配所有 *.php/* 形式
##重點程式碼見 fastcgi.conf 開頭部分
fastcgi.conf 程式碼如下:
fastcgi_pass 127.0.0.1:9000;
##fastcgi_index index.php;
set $path_info “”;
set $real_script_name $fastcgi_script_name;
if ($fastcgi_script_name ~ “^(.+?.php)(/.+)$”) {
set $real_script_name $1;
set $path_info $2;
}
fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
fastcgi_param SCRIPT_NAME $real_script_name;
fastcgi_param PATH_INFO $path_info;
## 以上是支援pathinfo的重點部分
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
#fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#fastcgi_param SCRIPT_NAME $fastcgi_script_name; 這兩行是需要註釋掉的。請注意
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
# PHP only, required if PHP was built with –enable-force-cgi-redirect
#fastcgi_param REDIRECT_STATUS 200;
本文轉自 fenghao.cn 51CTO部落格,原文連結:http://blog.51cto.com/linuxguest/694319,如需轉載請自行聯絡原作者
相關文章
- nginx+thinkphp下解決不支援pathinfo模式NginxPHP模式
- 配置nginx支援Nginx
- NGINX配置SSL支援Nginx
- nginx配置 laravel 支援NginxLaravel
- Nginx 支援websocket的配置NginxWeb
- Yii2配置pathinfo形式的url
- 介紹Nginx配置支援PHPNginxPHP
- nginx配置支援websocket協議wssNginxWeb協議
- Nginx 伺服器配置支援SignalR (WebSocket)Nginx伺服器SignalRWeb
- 配置Nginx以支援RTMP直播及HLS流播放Nginx
- Nginx配置正向代理支援HTTP和HTTPS轉發NginxHTTP
- ubuntu20安裝nginx支援多站點及代理配置UbuntuNginx
- 配置Django-TinyMCE元件支援上傳圖片功能Django元件
- nginx配置Nginx
- 配置nginxNginx
- Nginx 配置Nginx
- nginx之 nginx限流配置Nginx
- 【Nginx】Referer配置Nginx
- Nginx部署配置Nginx
- Nginx - 最小配置Nginx
- nginx常用配置Nginx
- Docker配置nginxDockerNginx
- NGINX基本配置Nginx
- Nginx 配置域名Nginx
- Nginx Location 配置Nginx
- nginx 配置使用Nginx
- Nginx配置HTTPSNginxHTTP
- nginx代理配置Nginx
- Nginx的Gzip功能Nginx
- Linux系統伺服器下Nginx支援ipv6配置的方法Linux伺服器Nginx
- nginx配置詳解Nginx
- Nginx 配置檔案Nginx
- Nginx負載配置Nginx負載
- nginx常用配置教程。Nginx
- Nginx全配置解析Nginx
- nginx配置檔案Nginx
- Nginx日誌配置Nginx
- Nginx安全配置研究Nginx
- 二、Nginx 配置 httpsNginxHTTP