user root; worker_processes 4; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; worker_rlimit_nofile 327680; events { worker_connections 327680; use epoll; } http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user $time_local "$request_method http://$host$request_uri HTTP/1.1" $status $bytes_sent "$http_referer" "$http_user_agent" TCP_HIT:NONE $request_time'; log_format miss '$remote_addr - $remote_user $time_local "$request_method http://$host$request_uri HTTP/1.1" $status $bytes_sent "$http_referer" "$http_user_agent" TCP_MISS:NONE $request_time'; access_log logs/access.log main; sendfile on; tcp_nopush on; client_max_body_size 2g; //客户post请求所能上传的文件大小的限制,限制content-length字段 #keepalive_timeout 0; keepalive_timeout 30; limit_zone one $binary_remote_addr 10m; gzip on; gzip_static on; gzip_http_version 1.1; gzip_min_length 1k; gzip_buffers 4 16k; gzip_proxied any; gzip_comp_level 5; gzip_disable "MSIE [1-6]/."; gzip_types text/plain application/x-javascript text/css application/xml; server_names_hash_bucket_size 128; client_header_buffer_size 128k; large_client_header_buffers 4 128k; resolver localdns.com; resolver_timeout 10; proxy_temp_path /home/cache_nginx/temp; proxy_cache_path /home/cache_nginx/cache levels=1:2 keys_zone=cache_one:500m inactive=100d max_size=80g; upstream www_test_com { server 10.1.1.1:80; //源站ip,端口 } server { listen 80; server_name www.test.com; //对此域名做反向代理 if ($host ~ [0-9]$) { return 403; } location / { proxy_temp_path /home/temp; //缓存大请求的,比如客户的大post请求 root /home/cache/www.test.com; //跟目录,其实是缓存下来的镜像文件 index index.html index.htm; expires 8640000s; error_page 404 = /fetch$uri; //当根目录下没有请求的内容时 去请求/fetch 其实是去访问源站 } location /fetch { internal; proxy_pass http://www_test_com; //访问代表源站的upstream proxy_set_header Host $proxy_host; //访问源站添加头信息 Host:www.test.com proxy_store on; //存储缓存下来的文件,on代表root或者alias代表的路径 proxy_store_access user:rw group:rw all:rw; //缓存的文件属性 alias /home/cache/www.test.com; } } }
通过以上配置 如果访问http://www.test.com/ 会返回403,这是因为访问的是主页,/home/cache/www.test.com下还没有index.html或者index.htm. 所以应该先通过访问http://www.test.com/index.html,将index.html缓存下来就可以用http://www.test.com/访问了,以后首次访问的文件因为都没缓存到镜像目录里,会是404,这里配置404去访问源站,访问一次就缓存到镜像目录了。
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/aiops/2495.html