Rootop 服务器运维与web架构

html和php的url美化处理

# 访问地址
http://voice/launchGame/23?playerId=123&playMode=real_play&currency=USD&operatorCode=Cordish&jurisdiction=PA&skin=Instance001&loginToken=token123&language=bg&contestRef=321

当浏览器访问时,会提示404错误,因为网站根目录下不存在launchGame目录及23文件
实际这是前段美化url后的访问地址,需要在nginx中将其重定向到 index.html 中,js会获取参数进行处理。
nginx中重定向有2种指令配置
1、rewrite
2、try_files

这里采用try_files

location / 
{
	index index.php index.html;
	try_files $uri $uri/ /index.html;
}

这样html中的js代码会将 /launchGame/23?playerId=123&playMode=real_play&currency=USD&operatorCode=Cordish&jurisdiction=PA&skin=Instance001&loginToken=token123&language=bg&contestRef=321
每一部分进行处理。

php的url美化处理
# 访问地址
http://voice/?user=1&pass=2
可以看到并没有具体到某个资源文件,例如index.php
但是配置文件中index指令指定了默认页为 index.php 所以url中的参数在index.php中是可以$_GET获取的

如果想通过try_files交给别的文件处理,则需要下面的配置

server
{
	listen        80;
	server_name  voice;
	root   "D:/phpstudy_pro/WWW/pt";
	index index.php index.html;
	
	try_files $uri $uri/ /a.php$is_args$args;

	location ~ \.php(.*)$
	{
		fastcgi_pass   127.0.0.1:9001;
		fastcgi_index  index.php;
		fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
		fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
		fastcgi_param  PATH_INFO  $fastcgi_path_info;
		fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
		include        fastcgi_params;
	}
}

http://voice/?user=1&pass=2 通过此链接来分析

# 情况1
$uri = /
匹配不到任何文件,找默认页 index.php或者index.html(取决于前后顺序,如果index.php文件不存在,则找index.html)。

# 情况2
$uri/,由于$uri = / 所以$uri/ = //
如果没有index.php或者index.html文件,则会匹配到网站根目录,会将网站根目录文件列出来,但是没加 autoindex on; 参数的话会继续访问 /a.php 且响应403状态码 但是不会传递参数。
如果加上了 autoindex on; 配置,则列出来网站目录列表,不会继续访问下一个匹配(/a.php)。

# 情况3
去掉 $uri/ 配置部分,也就是配置为 try_files $uri /a.php$is_args$args;
如果没有index.php或者index.html文件,会继续匹配/a.php$is_args$args,此时会访问到a.php并且参数也传递过去。

原创文章,转载请注明。本文链接地址: https://www.rootop.org/pages/5317.html

作者:Venus

服务器运维与性能优化

评论已关闭。