Nginx配置Google Fonts、Google Ajax、Gravatar反向代理教程

Share on:

nginx.conf配置开启proxy缓存

##
# Nginx Cache Settings
##
proxy_temp_file_write_size 128k;
proxy_temp_path   /var/cache/nginx/temp;
proxy_cache_path  /var/cache/nginx/cache levels=1:2 keys_zone=cache_one:50m inactive=7d max_size=5g;

Google Fonts反向代理配置vhost

#fonts.funboxpower.com.conf
upstream google {
        server fonts.googleapis.com:443;
}

upstream gstatic {
        server fonts.gstatic.com:443;
}
server {
        listen 443 ssl;
        server_name fonts.funboxpower.com;

        #控制来源refer
        valid_referers server_name *.funboxpower.com funboxpower.com *.chengyinet.com chengyinet.com fonts.funboxpower.com;
        if ($invalid_referer) {
                return 404;
        }

        #配置https
        ssl_certificate /your_path/fullchain.pem;
        ssl_certificate_key /your_path/privkey.pem;

        location /css {
                #将获取的信息fonts.gstatic.com转化成fonts.funboxpower.com
                sub_filter 'fonts.gstatic.com' 'fonts.funboxpower.com';
                sub_filter_once off;
                sub_filter_types text/css;
                proxy_pass_header Server;
                proxy_set_header Host fonts.googleapis.com;
                proxy_set_header Accept-Encoding '';
                proxy_redirect off;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Scheme $scheme;
                proxy_pass https://google;
                proxy_cache cache_one;
                proxy_cache_valid  200 304 365d;
                proxy_cache_key $host$uri$is_args$args;

                expires 30d;
        }

        location / {
                proxy_pass_header Server;
                proxy_set_header Host fonts.gstatic.com;
                proxy_redirect off;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Scheme $scheme;
                proxy_pass https://gstatic;
                proxy_cache cache_one;
                proxy_cache_valid  200 304 365d;
                proxy_cache_key $host$uri$is_args$args;

                expires 30d;
        }

}

nginx reload之后,Google Fonts反向代理配置就好了,将fonts.googleapis.com切换成fonts.funboxpower.com就可以了
Google Ajax、Gravatar的nginx反向代理配置大同小异就不写出来了

闽ICP备12003472号-7