nginx支持rewrite

两种方式, 测试都可以

1: tryfiles的方式, 有版本限制


1location / {
2                try_files $uri $uri/ /index.php?q=$uri&$args;
3}


2: 直接正则替换

关键代码如下

01            if (!-e $request_filename) {
02                rewrite ^/(.*)$ /index.php/$1 last;
03            }
04             
05             
06                set  $path_info "";
07                location /index.php {
08                    if ($uri ~ "^/index.php(/.+)$") {
09                        set  $path_info  $1;
10                    }
11                    fastcgi_pass   127.0.0.1:9001;
12                    fastcgi_index  index.php;
13                    fastcgi_param  SCRIPT_FILENAME  $document_root/index.php;
14                    fastcgi_param  PATH_INFO $path_info;
15                    include  fastcgi_params;
16                }


实际开发中展示

01server {
02        listen        80;
03        server_name  bkdemo.local bkdemo.local;
04        root   "/local/var/nginx/www.w.com/db_bscore/public";
05        location / {
06            if (!-e $request_filename) {
07                rewrite ^/(.*)$ /index.php/$1 last;
08            }
09            index index.php index.html;
10            error_page 400 /error/400.html;
11            error_page 403 /error/403.html;
12            error_page 404 /error/404.html;
13            error_page 500 /error/500.html;
14            error_page 501 /error/501.html;
15            error_page 502 /error/502.html;
16            error_page 503 /error/503.html;
17            error_page 504 /error/504.html;
18            error_page 505 /error/505.html;
19            error_page 506 /error/506.html;
20            error_page 507 /error/507.html;
21            error_page 509 /error/509.html;
22            error_page 510 /error/510.html;
23            autoindex  off;
24        }
25        location ~ \.php(.*)$ {
26            fastcgi_pass   127.0.0.1:9001;
27            fastcgi_index  index.php;
28            fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
29            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
30            fastcgi_param  PATH_INFO  $fastcgi_path_info;
31            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
32            include        fastcgi_params;
33        }
34         
35 
36```
37    set  $path_info "";
38    location /index.php {
39        if ($uri ~ "^/index.php(/.+)$") {
40            set  $path_info  $1;
41        }
42        fastcgi_pass   127.0.0.1:9001;
43        fastcgi_index  index.php;
44        fastcgi_param  SCRIPT_FILENAME  $document_root/index.php;
45        fastcgi_param  PATH_INFO $path_info;
46        include  fastcgi_params;
47    }
48```
49 
50}




原文链接: nginx支持rewrite 版权所有,转载时请注明出处,违者必究。
注明出处格式:流沙团 ( http://www.gyarmy.com/?post=553 )

发表评论

0则评论给“nginx支持rewrite”