为什么?

自从将博客平台更换为Sonic后,访问速度快了很多,但我想要更快,对后端压力更小,因此我想到了使用Nginx缓存对外展示的内容。以下是本网站的一些配置仅供参考。

Ngxin配置

首先在Nginx配置文件(/etc/nginx/nginx.conf)中的http { }中添加以下内容

  # 设置缓存文件位置,并配置缓存文件最大值
  proxy_cache_path /tmp/cache levels=1:2 keys_zone=cache_one:100m inactive=1d max_size=10g;
  # 关闭显示Nginx版本
  server_tokens off;
  # 开启ETAG功能
  etag on;

修改sonic.conf内容如下

server {
     listen 80
     listen [::]:80

     server_name www.itansuo.info;

     client_max_body_size 5m;
     client_body_timeout 60;

     access_log  /tmp/logs/sonic.log;

     gzip on;
     gzip_types application/xml application/json text/css text/javascript application/javascript;
     gzip_vary on;
     gzip_comp_level 6;
     gzip_min_length 500;

     location / {
         proxy_pass https://源站;
         proxy_redirect off;
         proxy_set_header Host $host;
         proxy_set_header X-Real-IP $remote_addr;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_set_header X-Forwarded-Proto $scheme;

         proxy_ssl_name $host;
         proxy_ssl_server_name on;

     }
     # 缓存静态文件,有效期90天
    location ~ .*\.(gif|jpg|png|woff2|ico|svg|css|js)(.*) {
         proxy_pass https://源站;
         proxy_redirect off;
         proxy_set_header Host $host;
         proxy_cache cache_one;
         proxy_cache_valid 200 302 24h;
         proxy_cache_valid 301 30d;
         proxy_cache_valid any 5m;
         proxy_cache_key "$host$request_uri$args";

         proxy_buffer_size 16k;
         proxy_buffers 4 32k;
         proxy_busy_buffers_size 96k;
         proxy_temp_file_write_size 96k;

         expires 90d;
         add_header wall  "Tokyo-Oracle-Cloud";
         add_header Cache-Control "public";

         proxy_ssl_name $host;
         proxy_ssl_server_name on;

         proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504;
    }

    # 缓存RSS源,有效期24小时
    location ~ /feed {
         proxy_pass https://源站;

         proxy_redirect off;
         proxy_set_header Host $host;
         proxy_cache cache_one;
         proxy_cache_valid 200 302 24h;
         proxy_cache_valid 301 30d;
         proxy_cache_valid any 5m;
         proxy_cache_key "$host$request_uri$args";

         proxy_buffer_size 16k;
         proxy_buffers 4 32k;
         proxy_busy_buffers_size 96k;
         proxy_temp_file_write_size 96k;

         expires 24h;
         add_header wall  "Tokyo-Oracle-Cloud";
         add_header Cache-Control "public";
         add_header Last-Modified $date_gmt;

         proxy_ssl_name $host;
         proxy_ssl_server_name on;

         proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504;
    }  
 
    # 缓存/archives/网页,有效期24小时
    location ~ ^/archives/(?!.*/$) {
         proxy_pass https://sonic-nrt.fly.dev;

         proxy_redirect off;
         proxy_set_header Host $host;
         proxy_cache cache_one;
         proxy_cache_valid 200 302 24h;
         proxy_cache_valid 301 30d;
         proxy_cache_valid any 5m;
         proxy_cache_key "$host$request_uri$args";

         proxy_buffer_size 16k;
         proxy_buffers 4 32k;
         proxy_busy_buffers_size 96k;
         proxy_temp_file_write_size 96k;

         expires 24h;
         add_header wall  "Tokyo-Oracle-Cloud";
         add_header Cache-Control "public";
         add_header Last-Modified $date_gmt;

         proxy_ssl_name $host;
         proxy_ssl_server_name on;

         proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504;
    }
    # 缓存首页,有效期24小时
    location = / {
         proxy_pass https://源站;

         proxy_redirect off;
         proxy_set_header Host $host;
         proxy_cache cache_one;
         proxy_cache_valid 200 302 24h;
         proxy_cache_valid 301 30d;
         proxy_cache_valid any 5m;
         proxy_cache_key "$host$request_uri$args";

         proxy_buffer_size 16k;
         proxy_buffers 4 32k;
         proxy_busy_buffers_size 96k;
         proxy_temp_file_write_size 96k;

         expires 24h;
         add_header wall  "Tokyo-Oracle-Cloud";
         add_header Cache-Control "public";
         add_header Last-Modified $date_gmt;

         proxy_ssl_name $host;
         proxy_ssl_server_name on;

         proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504;
    }
    # 缓存URL有/archives/的网页,不包括/archives/,有效期24小时
    location ~ ^/archives(/page/\d+)?/?$ {
         proxy_pass https://源站;

         proxy_redirect off;
         proxy_set_header Host $host;
         proxy_cache cache_one;
         proxy_cache_valid 200 302 24h;
         proxy_cache_valid 301 30d;
         proxy_cache_valid any 5m;
         proxy_cache_key "$host$request_uri$args";

         proxy_buffer_size 16k;
         proxy_buffers 4 32k;
         proxy_busy_buffers_size 96k;
         proxy_temp_file_write_size 96k;

         expires 24h;
         add_header wall  "Tokyo-Oracle-Cloud";
         add_header Cache-Control "public";
         add_header Last-Modified $date_gmt;

         proxy_ssl_name $host;
         proxy_ssl_server_name on;

         proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504;
    }

    # 缓存 /page/页面,有效期24小时
    location ^~ /page/ {
         proxy_pass https://源站;

         proxy_redirect off;
         proxy_set_header Host $host;
         proxy_cache cache_one;
         proxy_cache_valid 200 302 24h;
         proxy_cache_valid 301 30d;
         proxy_cache_valid any 5m;
         proxy_cache_key "$host$request_uri$args";

         proxy_buffer_size 16k;
         proxy_buffers 4 32k;
         proxy_busy_buffers_size 96k;
         proxy_temp_file_write_size 96k;

         expires 24h;
         add_header wall  "Tokyo-Oracle-Cloud";
         add_header Cache-Control "public";
         add_header Last-Modified $date_gmt;

         proxy_ssl_name $host;
         proxy_ssl_server_name on;

         proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504;
    }