需求
用户访问: http://localhost:8080/20200409-1-20-100.gif
实际访问:http://localhost:8080/gif.php?time=2020-04-09&theme=1&timezone=20&delay=100
而我需要PHP的传参time,theme,timezone,deday进行逻辑处理
解决思路
使用nginx重写(rewrite)
nginx配置
server {
listen 80;
server_name gif.aukeybi.com;
access_log /data/wwwlogs/gif_aukeybi_com_nginx.log combined;
index index.html index.htm index.php;
root /data/wwwroot/local/PHP-GIF-Countdown-master;
location / {
if (!-e $request_filename) {
#方法一:直接根据对应字段进行正则匹配相应参数
rewrite ^/([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)\.(gif) /gif/gif.php?time=$1&theme=$2&timezone=$3&delay=$4 last;
#方法二:把20200409-1-20-100.gif中的20200409-1-20-100整体转换为一个参数(p),如:http://localhost:8080/gif.php?p=20200409-1-20-100,然后,自己切割相应的参数
# rewrite ^/(.+?)\.(gif) /gif/gif.php?p=$1 last; //这种简单粗暴,比较自由
break;
}
}
#include /usr/local/nginx/conf/rewrite/yii.conf;
error_page 404 /404.html;
#error_page 502 /502.html;
location ~ [^/]\.php(/|$) {
#fastcgi_pass remote_php_ip:9000;
fastcgi_pass unix:/dev/shm/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
}
}
备注
1.nginx的rewrite重写的正则与一般正则有差入,最好参考nginx相关的官方文档。
2.相关参考文档:
https://mhl.xyz/Linux/rewrite.html
https://tool.oschina.net/uploads/apidocs/jquery/regexp.html
https://www.cnblogs.com/tugenhua0707/p/10798762.html
https://blog.csdn.net/lxbwolf/article/details/80715950
源码
具体代码请到我的git上拉取源码。地址: https://github.com/tangfengyun/gif-PHP.git