Nginx 实现单入口和 PATHINFO 总结 - 全栈笔记

Nginx 实现单入口和 PATHINFO 总结

任亦伟 Linux, PHP 2014-03-21 279 次浏览 没有评论

查找了不少资料,自己研究不少,为了尽可能地减少错误和最大程度地兼容,规则暂时如下,后续发现问题继续修正!

01
02
server {
03
 
04
  listen 80;
05
  server_name renyiwei.com www.renyiwei.com;
06
  root html;
07
  index index.html index.php index.htm;
08
 
09
  location /{
10
    if (!-e $request_filename){
11
      rewrite ^/(.*)\.php/(.*)$ /$1.php/$2 last; #例如:test.php/111这种不应该当作文件不存在,不交给index.php
12
      rewrite ^/(.*)$ /index.php/$1;
13
    }
14
  }
15
 
16
  location ~ \.php(/.*)?$ { #这里只匹匹配*.php和*.php/*这种
17
    fastcgi_pass 127.0.0.1:9000;
18
    fastcgi_index index.php;
19
    include fastcgi_params;
20
  }
21
}
22

###########fastcgi_params############
01
02
set $path_info "";
03
set $real_script_name $fastcgi_script_name;
04
if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$"){
05
  set $real_script_name $1;
06
  set $path_info $2;
07
 
08
}
09
fastcgi_param QUERY_STRING $query_string;
10
fastcgi_param REQUEST_METHOD $request_method;
11
fastcgi_param CONTENT_TYPE $content_type;
12
fastcgi_param CONTENT_LENGTH $content_length;
13
fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
14
fastcgi_param SCRIPT_NAME $real_script_name;
15
fastcgi_param PATH_INFO $path_info;
16
#fastcgi_param SCRIPT_NAME $fastcgi_script_name;
17
fastcgi_param REQUEST_URI $request_uri;
18
fastcgi_param DOCUMENT_URI $document_uri;
19
fastcgi_param DOCUMENT_ROOT $document_root;
20
fastcgi_param SERVER_PROTOCOL $server_protocol;
21
fastcgi_param HTTPS $https if_not_empty;
22
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
23
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
24
fastcgi_param REMOTE_ADDR $remote_addr;
25
fastcgi_param REMOTE_PORT $remote_port;
26
fastcgi_param SERVER_ADDR $server_addr;
27
fastcgi_param SERVER_PORT $server_port;
28
fastcgi_param SERVER_NAME $server_name;
29
# PHP only, required if PHP was built with --enable-force-cgi-redirect
30
fastcgi_param REDIRECT_STATUS 200;
31

发表评论

回顶部