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

Posted OS Knowledge Base

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了单节点nginx为两台apache服务器提供负载均衡相关的知识,希望对你有一定的参考价值。

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

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

[[email protected] ~]# vim /etc/hosts
192.168.1.101   ng-master
192.168.1.161   web1
192.168.1.162   web2

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

2.配置web1,web2的apache服务

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

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

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

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

    server {
        listen 80;

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

[[email protected] ~]# systemctl restart nginx.service

4.查看nginx相关日志

[[email protected] conf.d]# tail -f /var/log/messages
[[email protected] conf.d]# tail -f /var/log/nginx/access.log
[[email protected] 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

以上是关于单节点nginx为两台apache服务器提供负载均衡的主要内容,如果未能解决你的问题,请参考以下文章

双节点(nginx+keepalived)为两台apache服务器提供负载均衡

Linux下Nginx + Keepalived搭建负载均衡与高可用服务

Linux下Nginx + Keepalived搭建负载均衡与高可用服务

Nginx反向代理负载均衡

生产环境suse11sp2升级至sp4

在Linux上使用Nginx为Solr集群做负载均衡