nginx反向代理及缓存清理

简介:

#--------------- 在未安装nginx的情况下安装ngx_cache_purge ---------------#
cd nginx-1.13.7
./configure --prefix=/usr/local/nginx \
--with-pcre=/opt/pcre-8.41 \
--user=www \
--group=www \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_gzip_static_module
--add-module=/opt/ngx_cache_purge-2.3

make
make install

#--------------- 在已安装nginx情况下安装ngx_cache_purge ---------------#
cd nginx-1.13.7
./configure --prefix=/usr/local/nginx \
--with-pcre=/opt/pcre-8.41 \
--user=www \
--group=www \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_gzip_static_module
--add-module=/opt/ngx_cache_purge-2.3

make //千万不要make install,不然就真的覆盖了

/etc/init.d/nginx stop
mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
cp objs/nginx /usr/local/nginx/sbin/nginx

#nginx反向代理及缓存清理

#user nobody;
worker_processes 8;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;

events {
worker_connections 65535;
}

http {
include mime.types;
default_type application/octet-stream;

#log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
#                  '$status $body_bytes_sent "$http_referer" '
#                  '"$http_user_agent" "$http_x_forwarded_for"';

#access_log  logs/access.log  main;
sendfile        off;
tcp_nopush     on;
keepalive_timeout  300;
#nginx跟后端服务器连接超时时间(代理连接超时)
proxy_connect_timeout 300s; 
#连接成功后,后端服务器响应时间(代理接收超时)
proxy_read_timeout 300s; 
proxy_send_timeout 300s; 
#设置代理服务器(nginx)保存用户头信息的缓冲区大小
proxy_buffer_size 64k;
#proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
proxy_buffers 4 32k;
#高负荷下缓冲大小(proxy_buffers*2)
proxy_busy_buffers_size 64k; 
#设定缓存文件夹大小,大于这个值,将从upstream服务器传递请求,而不缓冲到磁盘
proxy_temp_file_write_size 64k; 
#不允许代理端主动关闭连接
proxy_ignore_client_abort on;
proxy_cache_path /data/cache1 levels=1:2 keys_zone=cache_blufly:20m inactive=1d max_size=1g;
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;
gzip on;
gzip_min_length  1k;
gzip_buffers     4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types       text/plain application/x-javascript text/css application/xml;
gzip_vary on;

#禁止非指定域名访问
server {
listen 80 default;
server_name  _;
return 500;
}

#www.blufly.com前端缓存
server
{
listen  80;
server_name blufly.com www.blufly.com;
add_header X-Via $server_addr;
add_header X-Cache $upstream_cache_status;
#301重定向设置
if ($host != 'www.blufly.com' ) 
{
    rewrite ^/(.*)$ http://www.blufly.com/$1 permanent;
}
location /
{
    proxy_cache cache_blufly;
    proxy_cache_valid 200 302 3h;
    proxy_cache_key $uri$is_args$args;
    proxy_set_header Host www.blufly.com;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_pass http://192.168.5.31:80/;
    expires      1d;
}
#使用ngx_cache_purge清理缓存
    location ~ /purge(/.*) {
    allow              127.0.0.1;
    allow              192.168.5.0/24;
    deny               all;
    proxy_cache_purge  cache_blufly $1$is_args$args;
    }
access_log off;
}

}

#---------------- nginx缓存清理脚本 ------------------------#
cat atuo_nginx_cache_clean.sh

#!/bin/bash
mfile="$*"
cache_dir=/data/cache1
echo $mfile
if [ "$#" -eq 0 ]
then
echo "please input scripts, if not, it will exit"
sleep 2 && exit
fi
echo "what you put $mfile will delete, please wait..."
for i in echo $mfile | sed 's/ /\n/g'
do
grep -ira $i $cache_dir | awk -F ':' '{print $1}' > /tmp/cache_list.txt
for j in cat /tmp/cache_list.txt
do
rm -rf $j
echo "$i $j is delete Success!"
done
done

#脚本用法
./atuo_nginx_cache_clean.sh aa.jpg bb.js cc.html

nginx反向代理及缓存清理

nginx反向代理及缓存清理


本文转自king_81951CTO博客,原文链接:http://blog.51cto.com/kerry/2052027,如需转载请自行联系原作者


相关文章
|
3月前
|
应用服务中间件 nginx
百度搜索:蓝易云【如何用NGINX实现UDP四层反向代理?】
请注意,这个回答是基于NGINX目前的特性和功能,如果有新的版本或更新的特性,建议查阅NGINX官方文档或其他权威资源,以确保你得到最准确的配置信息。
31 0
|
3月前
|
JSON 应用服务中间件 API
利用Grafana的API Key+Nginx反向代理实现Grafana免登录访问
利用Grafana的API Key+Nginx反向代理实现Grafana免登录访问
87 1
|
3月前
|
负载均衡 应用服务中间件 nginx
nginx-tomcat反向代理以及负载均衡测试
nginx-tomcat反向代理以及负载均衡测试
|
1月前
|
负载均衡 应用服务中间件 Linux
|
2月前
|
tengine Rust 负载均衡
反向代理学习笔记(一) Nginx与反向代理绪论
反向代理学习笔记(一) Nginx与反向代理绪论
|
2月前
|
负载均衡 Java 应用服务中间件
|
2月前
|
缓存 JavaScript 前端开发
Nginx 缓存使用指南-简单
Nginx 缓存使用指南-简单
16 0
|
2月前
|
消息中间件 关系型数据库 MySQL
使用Nginx的stream模块实现MySQL反向代理与RabbitMQ负载均衡
使用Nginx的stream模块实现MySQL反向代理与RabbitMQ负载均衡
60 0
|
3月前
|
缓存 前端开发 安全
前端反向代理的神奇世界:加速、安全与缓存的秘密(下)
前端反向代理的神奇世界:加速、安全与缓存的秘密(下)
|
3月前
|
缓存 负载均衡 前端开发
前端反向代理的神奇世界:加速、安全与缓存的秘密(上)
前端反向代理的神奇世界:加速、安全与缓存的秘密(上)
前端反向代理的神奇世界:加速、安全与缓存的秘密(上)