当前位置: 首页 > 后端技术 > PHP

nginx配置php-pathinfo模式-php多版本服务

时间:2023-03-29 14:04:25 PHP

结构/home/wwwroot/demo/public/index.php/index.php/index/index?p=1&ps=2SCRIPT_FILENAME:/home/wwwroot/demo/public/index.phpPHP_SELF:/index.php/index/indexSCRIPT_NAME:/index.phpPATHI_INFO:/index/indexQUERY_STRING:p=1&ps=2URI:/index.php/index/index?p=1&ps=2fastcgi_split_path_info指令该指令将覆盖nginx的$fastcgi_script_name/$fastcgi_path_info将请求路径赋值给$fastcgi_path_info,也就是我们要的path_info。index.php/news/1$fastcgi_script_name:index.php$fastcgi_path_info:news/11.通用配置以下配置文件与nginx.conf和pathinfo.conf同级。解析并设置PATH_INFOfastcgi_split_path_info^(.+?\.php)(/.*)$;set$path_info$fastcgi_path_info;fastcgi_paramPATH_INFO$path_info;fastcgi_paramSCRIPT_NAME$fastcgi_script_name;fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;fastcgi.conf环境变量,open_basedir是加载到php路径时需要注意的环境变量。siteApp/public/index.php下有很多应用程序入口文件的框架可能会受到限制。fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;fastcgi_paramQUERY_STRING$query_string;fastcgi_paramREQUEST_METHOD$request_method;fastcgi_paramCONTENT_TYPE$content_type;fastcgi_paramCONTENT_LENGTH$content_length;fastcgi_paramSCRIPT_NAME$fastcgi_script_name;fastcgi_paramREQUEST_URI$request_uri;fastcgi_paramDOCUMENT_URI$document_uri;fastcgi_paramDOCUMENT_ROOT$document_root;fastcgi_paramSERVER_PROTOCOL$server_protocol;fastcgi_paramREQUEST_SCHEME$scheme;fastcgi_paramHTTPS$httpsif_not_empty;fastcgi_paramGATEWAY_INTERFACECGI/1.1;fastcgi_paramSERVER_SOFTWAREnginx/$nginx_version;fastcgi_paramREMOTE_ADDR$remote_addr;fastcgi_paramREMOTE_PORT$remote_port;fastcgi_paramSERVER_ADDR$server_addr;fastcgi_paramSERVER_PORT$server_port;fastcgi_paramSERVER_NAME$server_name;#仅限PHP,需要如果PHP是使用--enable-force-cgi-redirectfastcgi_paramREDIRECT_STATUS200;fastcgi_paramPHP_ADMIN_VALUE"open_basedir=$document_root/:/tmp/:/proc/";enable-php.conf只启用php但不启用pathinfolocation~[^/]\.php(/|$){#try_files$uri=404;#fastcgi_passunix:/tmp/php-cgi.sock;fastcgi_pass127.0.0.0:9000;fastcgi_indexindex.php;includefastcgi.conf;}enable-php-pathinfo.conf启用php并打开pathinfolocation~[^/]\.php(/|$){#try_files$uri=404;Pathinfo肯定是404#fastcgi_passunix:/tmp/php-cgi.sock;fastcgi_pass127.0.0.0:9000;fastcgi_indexindex.php;包括路径信息.conf;includefastcgi.conf;}2.站点配置示例通配符/根请求,使用try_files指令分离静态/动态请求并隐藏index.php如果不想让别人知道你用的是什么服务器语言,很简单,把index.php改成youwantkonwlanginmyserverhahahnoway.php,把try_files指令更新为:location/{#dispatchstatic/dynamicrequestandhiddenindex.phptry_files$uri$uri//youwantkonwlanginmyserverhahahnoway.php?$query_string=404;}站点配置vhosts/site.confserver{listen80default_serverreuseport;#listen[::]:80default_serveripv6only=on;服务器名称_;索引index.html索引。htm索引.php;根/home/wwwroot/默认;location/{#thinkphp兼容模式#最后使用重写的url再次进入location进行解析#break直接返回重写的url对应的资源#if(!-e$request_filename){#rewrite^(.*)$/index.php?s=$1最后;#}#调度静态/动态请求和隐藏的index.php#hideindex.php/try_files$uri$uri//index.php$is_args$query_string=404;#隐藏index.php并打开pathinfotry_files$uri$uri//index.php$uri?$query_string;}位置~.*\.(gif|jpg|jpeg|png|bmp|swf)${过期30天;}location~.*\.(js|css)?${过期12h;}location~/.well-known{允许所有;}位置~/\.{全部拒绝;}#防止上传web-shell你的documentRoot的目录位置~/(wp-content|uploads|wp-includes|images)/.*\.php${denyall;}#选择启用模式#includeenable-php.confincludeenable-php-pathinfo.confaccess_log/home/wwwlogs/access.log;error_log/home/wwwlogs/error.log;}三、php多版本服务如果本地可以有多个站点需要使用不同的php版本,可以为不同版本的php-fpm服务创建不同的监听端口,然后创建对应的fpm服务转发配置文件enable-php74-pathinfo.conflocation~[^/]\.php(/|$){#try_files$uri=404;fastcgi_pass127.0.0.0:9074;fastcgi_indexindex.php;包括路径信息.conf;includefastcgi.conf;}enable-php56-pathinfo.conflocation~[^/]\.php(/|$){#try_files$uri=404;fastcgi_pass127.0.0.0:9056;fastcgi_indexindex.php;包括路径信息.conf;includefastcgi.conf;}enable-php53-pathinfo.conflocation~[^/]\.php(/|$){#try_files$uri=404;fastcgi_pass127.0.0.0:9053;fastcgi_indexindex.php;包括路径信息.conf;包括fastcgi.conf;}