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

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

 

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

实验须要的环境:html

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

软件包:linux

ngx_cache_purge-2.3.tar.gznginx

pcre-8.41.tar.gz数据库

nginx-1.13.5.tar.gzapache

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

当客户访问缓存服务器时,缓存服务器去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数据库缓存服务器的配置,敬请期待