使用 Nginx 作为反向代理并开启缓存提高网站性能

# 设置代理缓存路径位置,最大大小为1g
proxy_cache_path  /etc/blog_cache  levels=1:2 keys_zone=blogcache:100m inactive=24h max_size=1g;

server {
	listen 443
	server_name blog.monogogo.cn;

	location / {
		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 https;
		proxy_pass http://127.0.0.1:2368;

		# 删除对匿名访问者无效的cookies并阻止缓存
		proxy_ignore_headers Set-Cookie Cache-Control;
		proxy_hide_header Set-Cookie;
		# 为缓存状态添加标头(未命中或命中
		add_header X-Cache-Status $upstream_cache_status;

		proxy_cache blogcache;
		# 默认 TTL: 1 天
		proxy_cache_valid 1d;
		# 缓存 404 页 1小时
		proxy_cache_valid 404 1h;
		# 在刷新来自服务器的内容时使用GET请求
		proxy_cache_revalidate on;
		proxy_buffering on;
		#允许启动后台子请求以更新过期的缓存项
		# 不缓存错误
		proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;
	}

	# 不缓存页面
	location ^~ /ghost/ {
		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 https;
		proxy_pass http://127.0.0.1:2368;
	}

}