配置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配置https(可支援thinkphp的pathinfo模式)NginxHTTPPHP模式
- nginx+thinkphp下解決不支援pathinfo模式NginxPHP模式
- 配置nginx支援Nginx
- nginx配置 laravel 支援NginxLaravel
- NGINX配置SSL支援Nginx
- Nginx配置支援phpNginxPHP
- Nginx 支援websocket的配置NginxWeb
- 介紹Nginx配置支援PHPNginxPHP
- 配置nginx對php的支援NginxPHP
- Debian6.04nginx+mysql+php5-fpmfastcgi+pathinfoNginxMySqlPHPAST
- nginx配置支援websocket協議wssNginxWeb協議
- tp5 url 線上訪問 在nginx 上 出現404錯誤,解決辦法(1.80nginx 配置 pathInfo)Nginx
- nginx使用ssl模組配置HTTPS支援NginxHTTP
- Nginx 伺服器配置支援SignalR (WebSocket)Nginx伺服器SignalRWeb
- 配置Nginx以支援RTMP直播及HLS流播放Nginx
- Nginx配置正向代理支援HTTP和HTTPS轉發NginxHTTP
- Nginx配置域名同時支援 https 和 http 訪問NginxHTTP
- 配置Django-TinyMCE元件支援上傳圖片功能Django元件
- ubuntu20安裝nginx支援多站點及代理配置UbuntuNginx
- 配置nginxNginx
- Nginx 配置Nginx
- nginx配置Nginx
- Nginx之(三)Nginx配置Nginx
- Fedora 20:新的網路配置功能、支援 ARM 裝置
- nginx之 nginx限流配置Nginx
- Nginx篇--解讀nginx配置Nginx
- Nginx的Gzip功能Nginx
- Docker配置nginxDockerNginx
- nginx常用配置Nginx
- nginx代理配置Nginx
- Nginx Location 配置Nginx
- NGINX基本配置Nginx
- Nginx 配置域名Nginx
- Nginx部署配置Nginx
- Nginx - 最小配置Nginx
- nginx location配置Nginx
- nginx 配置使用Nginx
- nginx的配置Nginx