解决ThinkPHP路由404问题的 nginx 配置文件

星期一, 2016-11-21 | Author: Lee | php | 9,121 views

完美支持ThinkPHP的四种url模式, 静态文件不会再跑一次fastcgi浪费资源

ThinkPHP的几种url模式,如果nginx 不做任何配置,代理到php都是返回404错误,要解决也很简单

做好nginx的配置 做到动静分离和路由重写即可,配置如下:

其他日志配置等就不再贴出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
server {
    listen       80;
    server_name  i5a6.com;
    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;
    }
}

Tags: ,

文章作者: Lee

本文地址: https://www.pomelolee.com/1637.html

除非注明,Pomelo Lee文章均为原创,转载请以链接形式标明本文地址

No comments yet.

Leave a comment

Search

文章分类

Links

Meta