nginx利用 proxy_cache 來緩存文件
proxy_cache是nginx自帶的內置緩存模組,配置一下就可以用了,看下面的配置
proxy_cache_path /usr/local/nginx/proxy_cache levels=1:2 keys_zone=content:20m inactive=1d max_size=100m;
# levels設置目錄層次
# keys_zone設置緩存名字和共享記憶體大小
# inactive在指定時間內沒人訪問則被刪除在這裡是1天
# max_size最大緩存空間
範例
proxy_cache_path /usr/local/nginx/proxy_cache levels=1:2 keys_zone=content:20m inactive=1d max_size=100m;
# levels設置目錄層次
# keys_zone設置緩存名字和共享記憶體大小
# inactive在指定時間內沒人訪問則被刪除在這裡是1天
# max_size最大緩存空間
範例
- user zhangy users;
- worker_processes 10;
- error_log //vlogs/nginx_error.log crit;
- pid /var/vlogs/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 gb2312;
- server_names_hash_bucket_size 128;
- client_header_buffer_size 32k;
- large_client_header_buffers 4 32k;
- client_max_body_size 8m;
- sendfile on;
- tcp_nopush on;
- keepalive_timeout 60;
- tcp_nodelay on;
- fastcgi_connect_timeout 300;
- fastcgi_send_timeout 300;
- fastcgi_read_timeout 300;
- fastcgi_buffer_size 64k;
- fastcgi_buffers 4 64k;
- fastcgi_busy_buffers_size 128k;
- fastcgi_temp_file_write_size 128k;
- //============
- client_body_buffer_size 512k;
- proxy_read_timeout 60;
- proxy_send_timeout 5;
- proxy_buffer_size 16k;
- proxy_buffers 464k;
- proxy_busy_buffers_size 128k;
- proxy_temp_file_write_size 128k;
- proxy_temp_path /usr/local/nginx/proxy_temp;
- /*levels設置目錄層次
- keys_zone設置緩存名字和共享記憶體大小
- inactive在指定時間內沒人訪問則被刪除在這裡是1天
- max_size最大緩存空間*/
- proxy_cache_path /usr/local/nginx/proxy_cache levels=1:2 keys_zone=content:20m inactive=1d max_size=100m;
- //============等號中間要加的,關鍵只要加上proxy_cache_path
- gzip on;
- gzip_min_length 1k;
- gzip_buffers 416k;
- gzip_http_version 1.0;
- gzip_comp_level 2;
- gzip_types text/plain application/x-javascript text/css application/xml;
- gzip_vary on;
- upstream myselfxtajmd {
- server 127.0.0.1:10002;
- server 127.0.0.1:10001 weight=5;
- }
- server
- {
- listen 10000;
- server_name localhost;
- index index.html index.htm index.php;
- 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/test.log access;
- location /
- {
- proxy_cache content; //根keys_zone後的內容對應
- proxy_cache_valid 200 304 301 302 10d; //哪些狀態緩存多長時間
- proxy_cache_valid any 1d; //其他的緩存多長時間
- proxy_cache_key $host$uri$is_args $args; //通過key來hash,定義KEY的值
- proxy_pass http://myselfxtajmd;
- proxy_redirect off;
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- }
- //動態的放過
- location ~ .*\.(php|jsp|cgi)?$
- {
- proxy_set_header Host $host;
- proxy_set_header X-Forwarded-For $remote_addr;
- proxy_pass http://myselfxtajmd;
- }
- }
留言
張貼留言