今天开发公司官网:http://www.zstime.com/,遇到一个问题,如何在nginx下设置pathInfo以及如何隐藏index.php
这里分别来讲解一下:
一、隐藏index.php
隐藏index.php需要修改nginx的配置文件,如果你是使用vhost的,需要修改如conf/vhost/你的文件名.conf
文件,整个文件如下
server {
listen 80;
server_name www.zstime.com;
index index index.html index.htm index.php;
root /alidata/www/ZStimeBro/;
error_page 404 /error.html;
location / {
if (!-e $request_filename){
rewrite ^/(.*)$ /index.php/$1 last;
}
}
location ~ \.php($|/)
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~.*\.(gif|jpg|jpeg|png|bmp|swf)$ {
expires 30d;
}
location ~.*\.(js/css)?$ {
expires 1h;
}
#α??̬????
include /alidata/server/nginx/conf/rewrite/default.conf;
access_log /alidata/log/nginx/access/default.log;
}
其中添加了:
location / {
if (!-e $request_filename){
rewrite ^/(.*)$ /index.php/$1 last;
}
}
将请求链接重定向,加上一个index.php,从而实现了去除index.php的效果
二、添加pathInfo支持
nginx本身不支持pathInfo,所有我们需要自己解析URL,将index.php
location ~ \.php($|/)
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
注意:添加pathInfo方法不止一种,但是这种方法相对简单。
其中
~ \.php($|/)
是匹配请求路径中包含.php的url。
三、添加404
404页面的添加折腾了一天,首先需要在nginx.conf中开启
fastcgi_intercept_errors on;
切记要开启这一项,不然不能自定义404页面。
之后修改vhost对应的配置文件,添加:
error_page 404 /error.html;
切记不能加“=”号。
如此理论上来说是结束了,但是浏览器中试了一下发现还是不行,始终显示 状态吗200!一天的时间就花在这里!!最后查看了很久,才发现不是配置文件的问题了,而是自己使用的框架的问题,我使用的时broPHP,这个小框架居然没有做页面404处理,好吧,参考了http://www.csdn123.com/html/exception/693/693838_693845_693847.htm,搞定了!
总结:有时候,你觉得出问题的地方可能是安全的,你觉得很安全的地方往往出问题!
转载请注明出处:郭胜龙博客:http://blog.csdn.net/guoshenglong11/article/details/39324747 谢谢!