Nginx:作为cdn缓存时,follow 302

一、说明

1.1 rewrite

        www.guowenyan.cn/test.html(任意url)

        rewrite

        www.guowenyan.cn/test01.html

1.2 302

        www.guowenyan.cn/test01.html

        302

        www.guowenyan.cn/test.html

二、配置nginx.conf

#user  nobody;
worker_processes  1;

events {
    worker_connections  1024;
}


http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  logs/access.log  main;

    sendfile        on;

    keepalive_timeout  65;

    #dns resolver
    resolver 192.168.175.2:53 valid=1;

    server {
        listen       80;
        server_name  localhost;

        #set
        proxy_intercept_errors on;
        recursive_error_pages on;

        #follow 302
        #step 1,  rewrite "any url" to "/test01.html"
        location / {
             rewrite_by_lua '
                        ngx.exec("/test01.html")
                        ';
        }

        #step 2,  request "/test01.html", get 302 "/test.html"
        location ~ /test01.html {
                proxy_pass http://$host;
                error_page 302 = @error_page_302;
        }

        #step 3,  follow 302, rewrite "/test.html"
        location @error_page_302 {
                rewrite_by_lua '
                        local _, _, upstream_http_location = string.find(ngx.var.upstream_http_location, "^http://(.*)$")
                        ngx.exec("/302-location/" .. upstream_http_location)
                        ';
        }

        #step 4, request "/test.html"
        location ~ /302-location/([^/]+)/(.*) {
                proxy_pass http://$1/$2;
        }
    }
}

三、测试

3.1 用了follow 302


3.2 没用follow 302




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值