关键配置《nginx + lua 字符串过滤实现blogger站点的反向代理》

 具体的编译和配置过程略过,仅贴出nginx关键配置部分如下:


server {
        listen       80;
        server_name  blog.youhost.com;
 
        location / {
#lua字符替换 部分
body_filter_by_lua
'
    local origstr= ngx.arg[1]
    origstr=string.gsub(origstr,"www.blogger.com","blog.youhost.com/blg")
    origstr=string.gsub(origstr,"www.Blogger.com","blog.youhost.com/blg")
    origstr=string.gsub(origstr,"us.zyh.me","blog.youhost.com")
    origstr=string.gsub(origstr,"img1.blogblog.com","blog.youhost.com/img1")
    origstr=string.gsub(origstr,"img2.blogblog.com","blog.youhost.com/img2")
    origstr=string.gsub(origstr,"img3.blogblog.com","blog.youhost.com/img3")
    origstr=string.gsub(origstr,"www.blogblog.com","blog.youhost.com/bbwww")
 
    ngx.arg[1]=origstr
';
 
                proxy_pass  http://youblog.blogspot.com;
                proxy_set_header  X-Real-IP  $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#以下这条很重要,目的是关闭http压缩,如果不关闭压缩则无法进行任何过滤
                proxy_set_header Accept-Encoding "";
                proxy_redirect off;
        }
 
        location /blg/ {
 
                proxy_pass  http://www.blogger.com/;
                proxy_set_header  X-Real-IP  $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
 
        location /img1/ {
 
                proxy_pass  http://img1.blogblog.com/;
                proxy_set_header  X-Real-IP  $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
 
 
        location /img2/ {
 
                proxy_pass  http://img2.blogblog.com/;
                proxy_set_header  X-Real-IP  $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Accept-Encoding "";
                proxy_redirect off;
        }
 
        location /img3/ {
 
                proxy_pass  http://img1.blogblog.com/;
                proxy_set_header  X-Real-IP  $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Accept-Encoding "";
                proxy_redirect off;
        }
 
        location /bbwww/ {
 
                proxy_pass  http://www.blogblog.com/;
                proxy_set_header  X-Real-IP  $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Accept-Encoding "";
                proxy_redirect off;
        }
}

你可能感兴趣的:(字符串,lua,反向代理,nginx反向代理)