单节点nginx为两台apache服务器提供负载均衡

需求:本实验为单节点nginx为两台apache服务器提供负载均衡,所有配置为最简单

1.初始化3台测试server,该关的关了

[root@host101 ~]# vim /etc/hosts
192.168.1.101   ng-master
192.168.1.161   web1
192.168.1.162   web2

[root@host101 ~]# yum clean all
[root@host101 ~]# systemctl stop firewalld.service
[root@host101 ~]# systemctl disable firewalld.service
[root@host101 ~]# sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config

2.配置web1,web2的apache服务

[root@host161 ~]# yum -y install httpd
[root@host161 ~]# systemctl start httpd
[root@host161 ~]# systemctl enable httpd
ln -s '/usr/lib/systemd/system/httpd.service' '/etc/systemd/system/multiuser.target.wants/httpd.service'
[root@host161 ~]# cat /var/www/html/index.html
hello this lvs-web1

3.通过yum安装配置nginx节点

[root@host101 ~]# vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/x86_64/
gpgcheck=0
enabled=1

[root@host101 ~]# yum clean all
[root@host101 ~]# yum -y install nginx
[root@host101 ~]# vim /usr/share/nginx/html/index.html
<h1>Welcome to ng-master!</h1>
[root@host101 ~]# cd /etc/nginx/conf.d/
[root@host101 conf.d]# mv default.conf default.conf.1
[root@host101 ~]# vim /etc/nginx/conf.d/web.conf
    upstream myapp1 {
        server web1;
        server web2;
    }

    server {
        listen 80;

        location / {
            proxy_pass http://myapp1;
        }
    }

[root@host101 ~]# systemctl restart nginx.service

4.查看nginx相关日志

[root@host101 conf.d]# tail -f /var/log/messages
[root@host101 conf.d]# tail -f /var/log/nginx/access.log
[root@host101 conf.d]# tail -f /var/log/nginx/error.log

5.通过浏览器访问测试http://192.168.1.101/ 或者 http://myapp1/ 可发现流量在web1和web2之间跳转

参考:

http://www.linuxdiyf.com/linux/12955.html
http://nginx.org/en/linux_packages.html
http://blog.csdn.net/e421083458/article/details/30086413

原文地址:https://www.cnblogs.com/oskb/p/5634057.html