原创 

nginx v1.4.6 部署ThinkPHP 页面访问404 -- 酱油小君搬砖记

分类:    1137人阅读    IT小君  2016-12-03 16:17

2016-12-03处理:

首先,是盯着屏幕的你最需要的正解:

server {  
 ...  
    location / {  
        index  index.htm index.html index.php;  
        #如果文件不存在则尝试TP解析  
        try_files  $uri  /index.php$uri;  
    }  
    location ~ .+.php($|/) {  
        root        /var/www/html/website;  
        fastcgi_pass   127.0.0.1:9000;  
        fastcgi_index  index.php;  
          
        #设置PATH_INFO,注意fastcgi_split_path_info已经自动改写了fastcgi_script_name变量,  
        #后面不需要再改写SCRIPT_FILENAME,SCRIPT_NAME环境变量,所以必须在加载fastcgi.conf之前设置  
        fastcgi_split_path_info  ^(.+.php)(/.*)$;  
        fastcgi_param  PATH_INFO $fastcgi_path_info;  
          
        #加载Nginx默认"服务器环境变量"配置  
        include fastcgi_params;#原来是:include        fastcgi.conf; 但是nginx v1.4.6  将 fastcgi.conf改为fastcgi_params
    }  
}  


然后是故事的开始:
问题来了,SO百度一下你会看到一个ThinkPHP牛逼哄哄的帖子:老朱亲自写的,最完美ThinkPHP Nginx 配置文件

他提供的解决方案如下:


server {
    listen       80;
    server_name  thinkphp.lo;
    root /var/www;
    index  index.html index.htm index.php;

    error_page  404              /404.html;
    location = /404.html {
        return 404 'Sorry, File not Found!';
    }
    error_page  500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html; # windows用户替换这个目录
    }

    location / {
        try_files $uri @rewrite;
    }

    location @rewrite {
        set $static 0;
        if  ($uri ~ .(css|js|jpg|jpeg|png|gif|ico|woff|eot|svg|css.map|min.map)$) {
            set $static 1;
        }

        if ($static = 0) {
            rewrite ^/(.*)$ /index.php?s=/$1;
        }

    }

    location ~ /Uploads/.*.php$ {
        deny all;
    }

    location ~ .php/ {
       if ($request_uri ~ ^(.+.php)(/.+?)($|?)) { }
       fastcgi_pass 127.0.0.1:9000;
       include fastcgi_params;
       fastcgi_param SCRIPT_NAME     $1;
       fastcgi_param PATH_INFO       $2;
       fastcgi_param SCRIPT_FILENAME $document_root$1;
    }

    location ~ .php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /.ht {
        deny  all;
    }
}
这个确实可以解决404的问题,但是也存在几个问题:


1、当你的站点放在二级目录时,无法访问(没试过不带php的URL)

2、页面404时,nginx好像进入了死循环,页面一直再转...

于是乎Google,得到了正解!...好故事就这样结束了,no,

最终我没有选择nginx+thinkphp因为速度比apache+thinkphp慢,不知为毛,就大神指点..

点击广告,支持我们为你提供更好的服务

HTML5数字产品服务公司网站模板

jQuery右端悬浮带返回顶部特效

现代时尚家具公司网站模板

html5 canvas进度条圆环图表统计动画特效

HTML5 Canvas竖直流动线条背景动画特效

css+js实现的颜色渐变数字时钟动画特效

html5 canvas彩色碎片组合球形旋转动画特效

有机水果蔬菜HTML5网站模板

网页设计开发公司网站模板

响应式时尚单品在线商城网站模板

HTML5现代家居装潢公司网站模板

中小型创意设计服务公司网站模板

响应式太阳能能源公司网站模板

小众时尚单品在线电子商务网站模板

js+css3抽奖转盘旋转点餐代码

html5图标下拉搜索框自动匹配代码

响应式咖啡饮品宣传网站模板

canvas炫酷鼠标移动文字粒子特效

css鼠标跟随文字模糊特效

html5 svg夜空中星星流星动画场景特效

点击广告,支持我们为你提供更好的服务
 工具推荐 更多»
点击广告,支持我们为你提供更好的服务