码迷,mamicode.com
首页 > 其他好文 > 详细

centos7.4 搭建nginx反向缓存代理

时间:2018-03-30 15:15:01      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:nginx反向代理

nginx可以实现基于硬盘缓存的反向代理服务
通过proxy_cache和fastcgi_cache两个功能模块完成配置


-----

本例:
nginx反向代理服务器192.168.80.81
web服务器192.168.80.82
win7客户机 192.168.80.79


-----
### web服务器192.168.80.82 配置:
安装简单的httpd,提供web服务即可


-----
### nginx反向代理服务器192.168.80.81配置:
1.上传反向代理插件、软件和解压:
tar xzvf ngx_cache_purge-2.3.tar.gz -C /opt/
tar xzvf pcre-8.41.tar.bz2 -C /opt/
tar xzvf nginx-1.13.5.tar.gz -C /opt/
yum install -y zlib-devel


2.编译安装nginx
cd /opt/nginx-1.13.5

./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module \
--with-pcre=/opt/pcre-8.41 \
--add-module=/opt/ngx_cache_purge-2.3

make && make intall

useradd -M nginx

cd /usr/local/nginx/conf

vi nginx.conf

清除原内容,插入以下:
user  nginx nginx;
worker_processes  1;
error_log  logs/error.log  crit;
worker_rlimit_nofile 65535;
events {
        use epoll;
    worker_connections  65535;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
        charset utf-8;
    #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        on;
    tcp_nopush     on;
    #keepalive_timeout  0;
    keepalive_timeout  65;
        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;
        proxy_temp_path /var/cache/nginx/cache_temp;
        proxy_cache_path /var/cache/nginx/proxy_cache levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;
        upstream backend_server{
                server 192.168.80.185:80 weight=1 max_fails=2 fail_timeout=30s;
}
    #gzip  on;
    server {
        listen       80;
        server_name  test 192.168.80.81;
         index  index.html index.htm;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
                proxy_next_upstream http_502 http_504 error timeout invalid_header;
                proxy_cache cache_one;
                proxy_cache_valid 200 304 12h;
                proxy_cache_key $host$uri$is_args$args;
                proxy_set_header Host $host;
                proxy_set_header X-Forwarded-For $remote_addr;
                proxy_pass http://backend_server;
                expires 1d;
        }
        location ~/purge(/.*) {
                        allow 127.0.0.1;
                        allow 192.168.80.0/24;
                        deny all;
                        proxy_cache_purge cache_one $host$1$is_args$args;
                }
                location ~\.(php|jsp|cgi)?$ {
                proxy_set_header Host $host;
                proxy_set_header X-Forwarded-For $remote_addr;
                proxy_pass http://backend_server;
        }
             access_log off;
}
}

3.启动nginx
创建缓存目录
mkdir -p /var/cache/nginx/cache_temp
mkdir /var/cache/nginx/proxy_cache

ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
nginx -t   检查配置文件
nginx   
netstat -anpt | grep nginx

service firewalld stop
setenforce 0


测试

验证代理:win7客户机80.79访问nginx反向代理服务器地址80.81
可以看到web服务器80.82内容

验证缓存:在nginx反向代理服务器192.168.80.81上

ls /var/cache/nginx/proxy_cache  //使TAB键向后补全文件夹,可以验证1位文件夹名下的两位文件夹名下的缓存文件

http://192.168.80.81/purge/                        //用来清除缓存。

ls /var/cache/nginx/proxy_cache/              //使TAB键向后补就补不全了



centos7.4 搭建nginx反向缓存代理

标签:nginx反向代理

原文地址:http://blog.51cto.com/13469709/2092947

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!