手把手教你最新centos7.4搭建nginx缓存服务器

手把手教你如何在目前最新的CentOs7.4系统上搭建Nginx缓存服务器

 

Nginx缓存服务器原理:Nginx虽然是个轻量级的www服务器,但它本身也支持类似Squid的缓存功能,把URL以及相关的信息当成Key,用MD5编码Hash后将数据文件保存在硬盘上。实验将用到两台CentOs7.4 Linux系统的虚拟机,一台用apache搭建www服务器提供服务,一台用Nignx做缓存服务器,用一台window虚拟机或者真机进行测试。

实验需要的环境:

两台CentOs7.4虚拟机,一台window Nginx缓存加速器IP为192.168.80.186apache服务器IP为192.168.80.187

软件包:

ngx_cache_purge-2.3.tar.gz

pcre-8.41.tar.gz

nginx-1.13.5.tar.gz

下面是简单的实验拓扑图:

当客户访问缓存服务器时,缓存服务器去187服务器拿内容返给用户

 

实验过程:

用WinSCP将软件包传给Nginx服务器


先增加一个nginx用户

useradd -M nginx

解压软件包到/opt/目录下:

tar xzvf ngx_cache_purge-2.3.tar.gz -C /opt/

tar xzvf pcre-8.41.tar.gz -C /opt/

tar xzvf nginx-1.13.5.tar.gz -C /opt/

用本地yum安装插件:

yum install -y zlib-devel

到nginx-1.13.5目录下:

cd /opt/nginx-1.13.5/

手工编译安装Nginx服务器:

配置:

./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 install

出现这些说明安装成功

cd/usr/local/nginx/conf

编辑Nginx配置文件

vi nginx.conf

将原始配置文件删除

插入user  nginx nginx;

worker_processes  1;

error_log  logs/error.log  crit;

worker_rlimit_nofile65535;

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=1dmax_size=30g;

        upstream backend_server{

                server 192.168.80.185:80weight=1 max_fails=2 fail_timeout=30s;

}

    #gzip on;

    server {

        listen       80;

        server_name  test 192.168.80.182;

         index index.html index.htm;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {

                proxy_next_upstream http_502http_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_headerX-Forwarded-For $remote_addr;

                proxy_passhttp://backend_server;

                expires 1d;

        }

        location ~/purge(/.*) {

                        allow 127.0.0.1;

                        allow 192.168.80.0/24;

                        deny all;

                        proxy_cache_purgecache_one $host$1$is_args$args;

                }

                location ~\.(php|jsp|cgi)?$ {

                proxy_set_header Host $host;

                proxy_set_headerX-Forwarded-For $remote_addr;

                proxy_pass http://backend_server;

        }

             access_log off;

}

}

创建目录mkdir -p /var/cache/nginx/cache_temp

mkdir/var/cache/nginx/proxy_cache

将Nginx命令加入可执行命令中

ln -s/usr/local/nginx/sbin/nginx /usr/local/sbin/

检查错误

nginx –t

没有错误

启动:

nginx

过滤nginx看看是否工作

工作中

关闭防火墙和selinux

service firewalldstop

setenforce 0

 

在另一台搭建apache服务器

直接用本地yum搭建

 yum install –y httpd

搭建好后在/var/www/html/index.html里输入测试网页

启动httpd服务器就可以

测试:

访问186可以缓存187httpd服务器的内容证明试验成功

或者看186的日志也可以验证:

ls/var/cache/nginx/proxy_cache用TAB可以补齐说明成功

 

http://192.168.80.186/purge/ 可以清除缓存

如果怕不安全可以做身份验证

下期为大家带来搭建memcached数据库缓存服务器的配置,敬请期待


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值