89ou 发表于 2018-11-10 08:24:32

玩转Nginx - Nginx + Proxy Cache 快速搭建

user www www;  

  
worker_processes 8;
  

  
error_log /data/nginx/logs/error.log crit;
  

  
pid /usr/local/nginx/nginx.pid;
  

  
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 /data/nginx/proxy_cache_tmp;
  
proxy_cache_path /data/nginx/proxy_cache_dir levels=1:2 keys_zone=cache_one:6000m inactive=1d max_size=400g;
  

  
#include vhosts/* ;
  
log_format cache '$request_time - $upstream_cache_status ' '$status $remote_addr - $remote_user [$time_local] - $request' '$body_bytes_sent $http_referer' 'upstream_response_time $upstream_response_time ' 'msec $msec';
  
access_log /data/nginx/logs/access.log cache;
  
upstream cdnups {
  
server www.baidu.com:80 weight=100 max_fails=3 fail_timeout=30s;
  
}
  

  
server
  
{
  
listen 80;
  
server_name www.baidu.com;
  
index index.html index.htm;
  

  

  
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://cdnups;
  
#expires 30d;
  
}
  

  
location ~ /purge(/.*)
  
{
  

  
allow 127.0.0.1;
  
allow 192.168.1.1;
  
deny all;
  
proxy_cache_purge cache_one $host$1$is_args$args;
  
}
  

  
location ~ .*\.(php|jsp|cgi|do|shtml)?$
  
{
  
proxy_set_header Host $host;
  
proxy_set_header X-Forwarded-For $remote_addr;
  
proxy_pass http://cdnups;
  
}
  
}
  

  
}


页: [1]
查看完整版本: 玩转Nginx - Nginx + Proxy Cache 快速搭建