1. 程式人生 > >Nginx反向代理緩存配置

Nginx反向代理緩存配置

nginx反向代理緩存配置

安裝nginx和清理緩存的nginx模塊

下載地址https://pan.baidu.com/s/1mi9Xks4

https://pan.baidu.com/s/1i5eVpid



./configure --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --modules-path=/usr/lib64/nginx/modules --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_auth_request_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-http_slice_module --with-mail=dynamic --with-mail_ssl_module --with-stream=dynamic --with-stream_ssl_module --with-threads --with-pcre --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --add-module=../ngx_cache_purge-2.3

make && make install




設置反向代理緩存

proxy_cache_path /usr/share/nginx/cache levels=1:2 keys_zone=one:20m max_size=1g inactive=1d;

#keys_zone指的是緩存空間名稱

#max_size指的是緩存文件可以占用的最大空間

#inactive指的是如果一個緩存文件多長時間不被訪問,就會被刪除

server {

listen 80;

server_name xujx.pw www.xujx.pw;

#charset koi8-r;

access_log /var/log/nginx/cache.access.log;

error_log /var/log/nginx/v363.cn.error.log;

#增加兩頭部

add_header X-Via $server_addr;

add_header X-Cache $upstream_cache_status;

location / {

proxy_pass http://api.000o.cc/newparse;

proxy_set_header X-Real-IP $remote_addr;

proxy_cache_key "$host$request_uri";

proxy_cache one;

proxy_cache_valid 200 302 10m; #頁面返回碼為200 302 的緩存10分

proxy_cache_valid 404 1m; #頁面錯誤響應嗎404緩存時間1分

}


location ~ /purge(/.*) {

proxy_cache_purge one $host$1$is_args$args;

}


}


Nginx反向代理緩存配置