Skip to content

Nginx 反向代理 Google Fonts

nginx 缓存配置

nginx
proxy_temp_file_write_size 128k;
proxy_temp_path   /nginx_cache/proxy_temp;
proxy_cache_path  /nginx_cache/googlefonts levels=1:2 keys_zone=googlefonts:200m inactive=30d max_size=1g;
proxy_cache_path  /nginx_cache/gravatar levels=1:2 keys_zone=gravatar:200m inactive=30d max_size=1g;
proxy_cache_path  /nginx_cache/jsdelivr levels=1:2 keys_zone=jsdelivr:200m inactive=30d max_size=1g;

反向代理 Google Fonts

批量修改 fonts.yourdomain.com 为你的域名

nginx
upstream googlefonts {
    server fonts.googleapis.com:443;
}

upstream gstatic {
    server fonts.gstatic.com:443;
}

server {
    listen 80;
    server_name fonts.yourdomain.com;
    return 301 https://fonts.yourdomain.com$request_uri;
}

server {
    listen 443   ssl http2;
    server_name  fonts.yourdomain.com;
    index index.html index.htm index.php default.html default.htm default.php;

    ssl_certificate /ssl/yourdomain.com.cer;
    ssl_certificate_key /ssl/yourdomain.com.key;
    ssl_trusted_certificate /ssl/yourdomain.com.cer;

    ssl_stapling on;
    ssl_stapling_verify on;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_prefer_server_ciphers off;
    ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
    ssl_ecdh_curve secp384r1;
    ssl_session_timeout  10m;
    ssl_session_cache builtin:1000 shared:SSL:10m;
    ssl_session_tickets off;
    resolver 8.8.8.8 8.8.4.4 valid=60s ipv6=off;
    resolver_timeout 5s;
    add_header Strict-Transport-Security "max-age=63072000" always;

    location /css {
        sub_filter 'fonts.gstatic.com' 'fonts.yourdomain.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://googlefonts;
        proxy_cache googlefonts;
        proxy_cache_valid  200 304 365d;
        proxy_cache_key $host$uri$is_args$args;
        expires max;
    }

    location /icon {
        sub_filter 'fonts.gstatic.com' 'fonts.yourdomain.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://googlefonts;
        proxy_cache googlefonts;
        proxy_cache_valid  200 304 365d;
        proxy_cache_key $host$uri$is_args$args;
        expires max;
    }

    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 googlefonts;
        proxy_cache_valid  200 304 365d;
        proxy_cache_key $host$uri$is_args$args;
        expires max;
    }

    access_log /var/log/nginx/fonts.yourdomain.com.access.log main;
    error_log  /var/log/nginx/fonts.yourdomain.com.error.log warn;
}