使用Nginx作为反向代理服务器,将传入流量分发到多个Web服务器进行负载均衡

1.配置配置阿里云epel源和base源

[root@localhost ~]$ wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
[root@localhost ~]$ wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
[root@localhost ~]$ ls /etc/yum.repos.d/
CentOS-Base.repo       CentOS-fasttrack.repo  CentOS-Vault.repo
CentOS-CR.repo         CentOS-Media.repo      CentOS-x86_64-kernel.repo
CentOS-Debuginfo.repo  CentOS-Sources.repo    epel.repo

2.安装nginx的rpm包

wget http://nginx.org/download/nginx-1.23.3.tar.gz
#解压
tar -zxvf nginx-1.23.3.tar.gz
#安装依赖
yum -y install gcc gcc-c++ pcre pcre-devel openssl openssl-devel zlib zlib-devel
#初始化
./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi
make
make install

3.启动nginx

[root@localhost /]# whereis nginx
nginx: /usr/local/nginx
[root@localhost /]# ./usr/local/nginx/sbin/nginx 
[root@localhost /]# ps -ef | grep nginx
root       7184      1  0 10:11 ?        00:00:00 nginx: master process ./usr/local/nginx/sbin/nginx
nobody     7185   7184  0 10:11 ?        00:00:00 nginx: worker process
root       7187   1773  0 10:11 pts/0    00:00:00 grep --color=auto nginx

4.关闭防火墙

[root@localhost /]# systemctl stop firewalld.service 
[root@localhost /]# systemctl disable firewalld.service 
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost /]# vim /etc/selinux/config 
[root@localhost /]# cat /etc/selinux/config 

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled

5.测试是否访问成功

6.准备好两台配置好的web服务器

192.168.40配置的网站:

192.168.0.50配置的网站:

 7.配置反向代理

修改配置文件:

#重启服务
[root@localhost /]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost /]# /usr/local/nginx/sbin/nginx -s reload

 8.测试结果

访问反向代理服务器可以到达两个web服务器

 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值