nginx重写图片

3 篇文章 0 订阅
2 篇文章 0 订阅
需求
用户访问: 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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值