Skip to content

Instantly share code, notes, and snippets.

@vmlive
Last active August 29, 2015 13:58
Show Gist options
  • Save vmlive/10001382 to your computer and use it in GitHub Desktop.
Save vmlive/10001382 to your computer and use it in GitHub Desktop.
Nignx 缓存配置文件
user nginx nginx;
worker_processes 8;
error_log /var/log/nginx/error.log crit;
pid /var/run/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;
events {
use epoll;
worker_connections 65535;
}
http {
include mime.types;
default_type application/octet-stream;
charset utf-8;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 300m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
client_body_buffer_size 512k;
proxy_connect_timeout 5;
proxy_read_timeout 60;
proxy_send_timeout 5;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
proxy_temp_path /var/lib/nginx/proxy_temp;
proxy_cache_path /var/lib/nginx/proxy_cache levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=3g;
upstream backend_server {
server 192.168.17.151:8080 weight=1 max_fails=2 fail_timeout=30s;
# server 192.168.17.152:8080 weight=1 max_fails=2 fail_timeout=30s;
}
server {
listen 80;
server_name web01.ysten.com 192.168.17.150;
index index.html index.htm;
root /var/www;
location / {
#如果后端的服务器返回502、504、执行超时等错误,自动将请求转发到upstream负载均衡池中的另一台服务器,实现故障转移。
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_cache cache_one;
#对不同的HTTP状态码设置不同的缓存时间
proxy_cache_valid 200 304 12h;
#以域名、URI、参数组合成Web缓存的Key值,Nginx根据Key值哈希,存储缓存内容到二级缓存目录内
proxy_cache_key $host$uri$is_args$args;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://backend_server;
expires 1d;
}
location ~ /purge(/.*) {
#设置只允许指定的IP或IP段输入正确的密码才可以清除URL缓存。(Nginx限制目录访问,可以用apache的htpasswd命令生成密钥文件,如:htpasswd -c /usr/local/nginx/conf/htpasswd adm_cache ,提示输入密码即可。)
auth_basic "Please Insert User And Password";
auth_basic_user_file /etc/nginx/conf/htpasswd;
allow 127.0.0.1;
allow 192.168.17.0/24;
deny all;
proxy_cache_purge cache_one $host$1$is_args$args;
}
location ~ .*\.(php|jsp|cgi)?$ {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://backend_server;
}
}
log_format access '$remote_addr – $remote_user[$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
access_log /var/log/nginx/access.log access;
#$upstream_cache_status表示资源缓存的状态,有HIT MISS EXPIRED三种状态
add_header X-Cache $upstream_cache_status;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment