1. 程式人生 > >Nginx:作為cdn快取時,follow 302

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