Skip to content

Instantly share code, notes, and snippets.

@tiancheng91
Last active June 8, 2018 18:08
Show Gist options
  • Save tiancheng91/8b182f60a0a58f6f4df832cd3a6db275 to your computer and use it in GitHub Desktop.
Save tiancheng91/8b182f60a0a58f6f4df832cd3a6db275 to your computer and use it in GitHub Desktop.
nginx 配置
// 反代优化 https://www.maxcdn.com/blog/nginx-application-performance-optimization/
worker_processes 4; // params int auto
worker_connections 1024; 每个worker建立连接数,server+client
// 默认值
thread_pool default threads=32 max_queue=65536;
thread_pool pool_1 threads=16;
thread_pool pool_2 threads=16;
events {
// 默认监听一个端口, 通知给所有worker, 所有竞争accept新的连接
accept_mutex on; // 关闭 新请求过来时,多个worker串行获取连接
accept_mutex reuseport; // 新系统特性SO_REUSEPORT, 能加速通知到worker,减少延迟
}
http {
include mime.types;
default_type application/octet-stream;
access_log off;
// 多块磁盘加速
proxy_cache_path /mnt/disk1 levels=1:2 keys_zone=cache_1:10m max_size=1024G inactive=60m
use_temp_path=off;
proxy_cache_path /mnt/disk2 levels=1:2 keys_zone=cache_2:10m max_size=1024G
use_temp_path=off;
// cache参数
// - levels: 目录层级
// - keys_zone: 内存中存 缓存key及metadata(计数什么的) 1MB大约存8000个key
// - max_size: 设置空间上限
// - inactive: 多长时间未使用从缓存删除, 不同于cache-control里的expired, expired时回触发refreshes
// - use_temp_path: 禁用temp,nginx默认回先存tmp目录,在复制到具体的缓存目录
proxy_cache_revalidate on; // cache-control expired 时, 发起请求带If-Modified-Since
proxy_cache_min_uses 3; // 至少访问几次后才缓存, 防止异常刷磁盘
proxy_cache_background_update on;
proxy_cache_lock on; // 只有MISS时第一个连接回源
proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
// 回源异常时使用过期的缓存,对应inactive
add_header X-Cache-Status $upstream_cache_status;
// 覆盖默认 cache-control
proxy_ignore_headers Cache-Control;
proxy_ignore_headers Set-Cookie; // 忽略Set-Cookie头
proxy_cache_methods GET HEAD POST; // cache哪些请求
proxy_cache_valid any 30m;
// 哪些是不需要缓存的,cookies带nocache, 或query带nocache
proxy_cache_bypass $cookie_nocache $arg_nocache;
// key规则, 可以cookie按人缓存
proxy_cache_key $proxy_host$request_uri$cookie_jessionid;
// A/B测试分组
split_clients $request_uri $disk {
80% 1;
* 2; // 默认
}
server {
listen 8000;
location / {
root /storage;
aio threads=default;
sendfile on;
sendfile_max_chunk 512k;
}
location /cache/ {
proxy_pass http://backend;
proxy_cache_key $request_uri;
proxy_cache cache_$disk;
aio threads=pool_$disk;
sendfile on;
}
location /skip_cache/ {
set $skip_cache 0;
if ($request_method = POST) {
set $skip_cache 1;
}
if ($query_string != "") {
set $skip_cache 1;
}
# Don't cache uris containing the following segments
if ($request_uri ~* "/wp-admin/|/wp-json/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
set $skip_cache 1;
}
# Don't use the cache for logged in users or recent commenters
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
set $skip_cache 1;
}
fastcgi_cache_bypass $skip_cache;
fastcgi_no_cache $skip_cache;
proxy_cache_bypass $skip_cache;
proxy_cache_bypass $cookie_nocache $arg_nocache$arg_comment;
proxy_cache_bypass $http_pragma $http_authorization;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment