nginx 配置说明 rewrite
时间:2016-12-07 22:45 来源:linux.it.net.cn 作者:IT
项目配置:
server {
listen 807;
server_name localhost;
root /data/web/myblog/web;
client_max_body_size 501m;
error_log /data/server/nginx/symfony2.error.log;
access_log /data/server/nginx/symfony2.access.log;
# strip app.PHP/ prefix if it is present
# 如果出现192.168.1.53:807/app.php/blog/index 那么直接重定向到 192.168.1.53:807/blog/index
rewrite ^/app\.php/?(.*)$ /$1 permanent;
# 上面重定向后 符合到这个location 那么又进行try_files 找这个uri 如果找到直接返回uri 找不到重定向到 rewriteapp 这个loacation 进行重写 又再次变成
#192.168.1.53:807/app.php/blog/index
location / {
index index.php;
try_files $uri @rewriteapp;
}
location @rewriteapp {
rewrite ^(.*)$ /app.php/$1 last;
}
# pass the PHP scripts to FastCGI server from upstream phpfcgi
location ~ ^/(app|app_dev|config)\.php(/|$) {
fastcgi_pass unix:/tmp/php.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi.conf;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
}
location ~ \.php$ {
fastcgi_pass unix:/tmp/php.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi.conf;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
}
解析
fastcgi_split_path_info
目的:让php能够解析类似这样的url http://www.it.net.cn/index.php/abc/def
在默认情况下我们打开这个url时会出现无法找到该页。这就需要在nginx做path_info设置了。
原理:把index.php做为php执行的脚本,把/abc/def做为参数传给php-cgi执行。
实现:nginx版本0.7.31以上支持fastcgi_split_path_info,这个指令可以设置SCRIPT_FILENAME和PATH_INFO的变量,用正则表达式将这两部分分开.
例子:我们在nginx配置文件"local"区块中加入以下代码。
-
location ~ ^.+\.php
-
{
-
fastcgi_pass 127.0.0.1:9000;
-
fastcgi_split_path_info ^(.+\.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;
-
(...)
-
}
重启nginx服务
-
/usr/local/nginx/sbin/nginx -s reload
当nginx处理http://www.it.net.cn/index.php/abc/def请求时,将会把"index.php"做为php的脚本,/abc/def做为index.php脚本的参数提交给php-cgi执行
(责任编辑:IT)
项目配置:
server { listen 807;server_name localhost; root /data/web/myblog/web; client_max_body_size 501m; error_log /data/server/nginx/symfony2.error.log; access_log /data/server/nginx/symfony2.access.log; # strip app.PHP/ prefix if it is present # 如果出现192.168.1.53:807/app.php/blog/index 那么直接重定向到 192.168.1.53:807/blog/index rewrite ^/app\.php/?(.*)$ /$1 permanent;
# 上面重定向后 符合到这个location 那么又进行try_files 找这个uri 如果找到直接返回uri 找不到重定向到 rewriteapp 这个loacation 进行重写 又再次变成 #192.168.1.53:807/app.php/blog/index location / { index index.php; try_files $uri @rewriteapp; } location @rewriteapp { rewrite ^(.*)$ /app.php/$1 last; } # pass the PHP scripts to FastCGI server from upstream phpfcgi location ~ ^/(app|app_dev|config)\.php(/|$) { fastcgi_pass unix:/tmp/php.sock; fastcgi_split_path_info ^(.+\.php)(/.*)$; include fastcgi.conf; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param HTTPS off; } location ~ \.php$ { fastcgi_pass unix:/tmp/php.sock; fastcgi_split_path_info ^(.+\.php)(/.*)$; include fastcgi.conf; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param HTTPS off; }
解析 fastcgi_split_path_info
目的:让php能够解析类似这样的url http://www.it.net.cn/index.php/abc/def
重启nginx服务
当nginx处理http://www.it.net.cn/index.php/abc/def请求时,将会把"index.php"做为php的脚本,/abc/def做为index.php脚本的参数提交给php-cgi执行 |