Nginx 实现单入口和 PATHINFO 总结

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

server {
 
  listen 80;
  server_name renyiwei.com www.renyiwei.com;
  root html;
  index index.html index.php index.htm;
 
  location /{
    if (!-e $request_filename){
      rewrite ^/(.*)\.php/(.*)$ /$1.php/$2 last; #例如:test.php/111这种不应该当作文件不存在,不交给index.php
      rewrite ^/(.*)$ /index.php/$1;
    }
  }
 
  location ~ \.php(/.*)?$ { #这里只匹匹配*.php和*.php/*这种
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    include fastcgi_params;
  }
}

###########fastcgi_params############
set $path_info "";
set $real_script_name $fastcgi_script_name;
if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$"){
  set $real_script_name $1;
  set $path_info $2;
 
}
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
fastcgi_param SCRIPT_NAME $real_script_name;
fastcgi_param PATH_INFO $path_info;
#fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param HTTPS $https if_not_empty;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;

本文出自 全栈笔记 版权所有,你可以在保留原文地址:Nginx 实现单入口和 PATHINFO 总结 及作者的情况下到你的网站或博客。

【上一篇】
【下一篇】

您可能感兴趣的文章:

发表评论

您必须 登录 后才能发表评论。