Nginx反向代理、缓存服务器

Nginx反向代理、缓存服务器

一、 配置nginx反向代理、缓存服务器
1、配置基本环境
1)
配置基本环境centos01、02网站服务器、03nginx和缓存服务器

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
[root@centos01 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens32
GATEWAY=192.168.100.30
[root@centos01 ~]# systemctl restart network
[root@centos02 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens32
GATEWAY=192.168.100.30
[root@centos02 ~]# systemctl restart network

[root@centos03 ~]# cp /etc/sysconfig/network-scripts/ifcfg-ens32 /etc/sysconfig/network-scripts/ifcfg-ens34
[root@centos03 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens34
NAME=ens33
DEVICE=ens34
IPADDR=192.168.200.254
NETMASK=255.255.255.0
[root@centos03 ~]# systemctl restart network
[root@centos03 ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
[root@centos03 ~]# sysctl -p
net.ipv4.ip_forward = 1
2)
两台网站服务器安装httpd

[root@centos01 ~]# rm -rf /etc/yum.repos.d/CentOS-*
[root@centos02 ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
[root@centos01 ~]# yum -y install httpd
[root@centos01 ~]# echo “www.benet.com” > /var/www/html/index.html
[root@centos01 ~]# systemctl start httpd

[root@centos02 ~]# rm -rf /etc/yum.repos.d/CentOS-*
[root@centos02 ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
[root@centos02 ~]# yum -y install httpd
[root@centos02 ~]# echo “www.accp.com” > /var/www/html/index.html
[root@centos02 ~]# systemctl start httpd
2、安装nginx
1)
安装依赖软件

[root@centos03 ~]# rm -rf /etc/yum.repos.d/CentOS-*
[root@centos03 ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
[root@centos03 ~]# yum -y install pcre-devel zlib-devel
2)
创建管理nginx用户

[root@centos03 ~]# useradd -M -s /sbin/nologin nginx
3)
解压缓存依赖工具

[root@centos03 ~]# rz
z waiting to receive.*B0100000023be50
在这里插入图片描述
[root@centos03 ~]# tar zxvf ngx_cache_purge-2.0.tar.gz -C /usr/src/
4)
配置nginx

在这里插入图片描述
[root@centos03 ~]# umount /mnt/
[root@centos03 ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
[root@centos03 ~]# tar zxvf /mnt/nginx-1.6.0.tar.gz -C /usr/src/
[root@centos03 nginx-1.6.0]# ./configure --prefix=/usr/local/nginx --user=nginx --with-http_stub_status_module --add-module=/usr/src/ngx_cache_purge-2.0
5)
编译安装nginx

[root@centos03 nginx-1.6.0]# make && make install
6)
优化nginx命令

[root@centos03 ~]# ln -s /usr/local/nginx/sbin/
/usr/local/sbin/
3、配置nginx缓存
1)
修改主配置文件

[root@centos03 ~]# vim /usr/local/nginx/conf/nginx.conf
3 user nginx;
4 worker_processes 2;
13 events {
14 use epoll;
15 worker_connections 2048;
16 }
17 worker_rlimit_nofile 2048;
23 charset utf-8;
34 keepalive_timeout 10;
37 client_body_buffer_size 512k;
38 proxy_connect_timeout 10;
39 proxy_read_timeout 60;
40 proxy_send_timeout 5;
41 proxy_buffer_size 512k;
42 proxy_buffers 4 512k;
43 proxy_temp_file_write_size 512k;
44 proxy_temp_path /var/cache/nginx/cache_temp;
45 proxy_cache_path /var/cache/nginx/proxy_cache levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=10g;
46 upstream backend_server {
47 server 192.168.100.10:80 weight=1 max_fails=2 fail_timeout=30s;
48 server 192.168.100.20:80 weight=1 max_fails=2 fail_timeout=30s;
49 }
51 server {
52 listen 192.168.200.254:80;
53 server_name www.benet.com;
59 location / {
60 root html;
61 index index.html index.htm;
62 proxy_next_upstream http_502 http_504 error timeout;
63 proxy_cache cache_one;
64 proxy_cache_valid 200 304 12h;
65 proxy_cache_key h o s t host hosturi i s a r g s is_args isargsargs;
66 proxy_set_header host $host;
67 proxy_set_header X-Forearded-for $remote_addr;
68 proxy_pass http://backend_server;
69 expires 1d;
70 }
71 location ~ /cheng(/.*) {
72 allow 127.0.0.1;
73 allow 192.168.200.0/24;
74 deny all;
75 proxy_cache_purge cache_one $host 1 1 1is_argsKaTeX parse error: Expected 'EOF', got '}' at position 18: …gs; 76 }̲ 77 loc… {
78 proxy_pass http://backend_server;
79 proxy_set_header X-Forearded-for $remote_addr;
80 }
2)
创建数据缓存目录

[root@centos03 ~]# mkdir -p /var/cache/nginx/cache_temp
[root@centos03 ~]# mkdir -p /var/cache/nginx/proxy_cache
3)
启动nginx

[root@centos03 ~]# nginx
4、配置dns
1)
使用系统自带光盘

在这里插入图片描述
2)
安装dns

[root@centos03 ~]# yum -y install bind bind-chroot bind-utils
3)
配置dns

[root@centos03 ~]# echo “” > /etc/named.conf
[root@centos03 ~]# vim /etc/named.conf
options {
listen-on port 53 { any; };
directory “/var/named/”;
};
zone “benet.com” IN {
type master;
file “benet.com.zone”;
};

[root@centos03 ~]# vim /var/named/benet.com.zone
$TTL 86400
@ SOA benet.com. root.benet.com (
2020040610
1H
15M
1W
1D
)
@ NS centos03.benet.com.
centos03 A 192.168.200.254
www A 192.168.200.254
[root@centos03 ~]# chmod +x /var/named/benet.com.zone
[root@centos03 ~]# chown named:named /var/named/benet.com.zone
[root@centos03 ~]# named-checkconf /etc/named.conf
[root@centos03 ~]# named-checkzone benet.com /var/named/benet.com.zone
zone benet.com/IN: loaded serial 2020040610
OK
4)
启动dns

[root@centos03 ~]# systemctl start named
[root@centos03 ~]# systemctl enable named
5、客户端访问
1)
客户端配置IP地址

在这里插入图片描述

在这里插入图片描述
2)
客户端使用域名访问

在这里插入图片描述
3)
清空缓存停掉accp服务器访问

[root@centos03 ~]# cd /var/cache/nginx/proxy_cache/
[root@centos03 proxy_cache]# ls
0 b
[root@centos03 proxy_cache]# rm -rf ./*
[root@centos02 ~]# systemctl stop httpd
在这里插入图片描述
4)
访问清空缓存目录数据

在这里插入图片描述
六、配置nginx授权访问
1、配置客户端访问清空缓存目录数据进行身份验证
1)
安装httpd-tools工具

[root@centos03 ~]# yum -y install httpd-tools
2)
生成身份验证文件

[root@centos03 ~]# htpasswd -c /usr/local/nginx/.admin admin
New password:
Re-type new password:
Adding password for user admin
3)
修改配置文件

[root@centos03 ~]# vim /usr/local/nginx/conf/nginx.conf
70 location ~ /cheng(/.*) {
71 allow 127.0.0.1;
72 allow 192.168.200.0/24;
73 auth_basic “secret”;
74 auth_basic_user_file /usr/local/nginx/.admin;
75 stub_status on;
76 access_log off;
77 deny all;
78 proxy_cache_purge cache_one $host 1 1 1is_args$args;
79 }
4)
重新启动nging

[root@centos03 ~]# killall nginx
[root@centos03 ~]# nginx
5)
客户端访问

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值