公司的游戏快上线了,之前我们的服务器查询充值接口都是提供给运营商IP+端口+参数的。为了方便,要求要统一到一个api域名上
rewrite ^/v(\d)/(.+?)/(.+?)/(.+?)/(.+?)?(.*) /server_api.php?v=$1&pf=$2&game=$3&ly=$4&act=$5&$6 last;
最后的last我这里用break也可以,两者的区别大致就是break会停止继续匹配,直接打开请求这个页面。而使用last的话,会继续往下走,查看其他的location条件。
就是把
platform1: 123.123.123.123:7001/dosth?sid=1¶m1=value1¶m2=value2¶m3=value3
platform2: 111.111.111.111:7002/dosth2?sid=1¶m1=value1
这样的格式改为
api.website.com/v1/gamename/platform/serverid/what?param....
类似这样的单一的URL查询+一份写好的配置文件
所以需要修改nginx配置文件,查阅了一下网上的例子,按照如下方式书写
server {
server_name api.website.com;
listen 80;
root /home/wwwroot/;
location / {
root /home/wwwroot/;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index server_api.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
rewrite ^/v(\d)/(.+?)/(.+?)/(.+?)/(.+?)?(.*) /server_api.php?v=$1&pf=$2&game=$3&ly=$4&act=$5&$6 last;
}
}
rewrite+正则表达式+重写后的语句+模式
这句话中,正则每个括号里的正则匹配到的单词对应后面的$1到$n。然后重新组织成一个URL就完成了。
最后的last我这里用break也可以,两者的区别大致就是break会停止继续匹配,直接打开请求这个页面。而使用last的话,会继续往下走,查看其他的location条件。
我这个只是简单的替换,没有其他语句了,所以没区别