nginx负载均衡简单配置

    xiaoxiao2021-03-25  95

    准备三台虚拟机来做这个实验:

    PA     负载均衡服务器  www.mydomain.com

    PA1   192.168.2.3       web服务器

    PA2   192.168.2.4       web服务器

    首先三台电脑预装nginx软件:

    1、导入外部软件库

    rpm -Uvh http://dl.iuscommunity.org/pub/ius/stable/Redhat/6/i386/epel-release-6-5.noarch.rpm   rpm -Uvh http://dl.iuscommunity.org/pub/ius/stable/Redhat/6/i386/ius-release-1.0-10.ius.el6.noarch.rpm   rpm -Uvh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm   以下添加注释 mirrorlist=http://dmirr.iuscommunity.org/mirrorlist?repo=ius-el6&arch=$basearch   以下删除注释 #baseurl=http://dl.iuscommunity.org/pub/ius/stable/Redhat/5/$basearch  

    2、yum安装nginx

         yum install nginx  

    3、启动nginx

       chkconfig nginx on      service nginx start     向web服务器中放入测试文件: < html >      <head>     <title>Welcome to nginx!</title>     </head>     <body bgcolor="white" text="black">     <center><h1>Welcome to nginx! 192.168.2.1</h1></center>     </body>     </html>  

    配置负载均衡服务器:

    1. 装完之后哈,我们先找到 PA 的nginx.conf配置文件: 在http段加入以下代码:

    upstream servers.mydomain.com { server 192.168.2.3:80; server 192.168.2.4:80; }

    servers.mydomain.com随便取的。

    那么PA的server配置如下: 在http段加入以下代码:

    server{ listen 80; server_name www.mydomain.com; location / { proxy_pass http://servers.mydomain.com; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }

    那么PA1、PA2 的配置如下:

    server{ listen 80; server_name www.mydomain.com; index index.html; root /data/htdocs/www; }

    用其它端口行不行啊,当然也是可以的,假设PA的nginx.conf配置文件:

    upstream servers2.mydomain.com { server 192.168.2.3:8080; server 192.168.2.4:8081; } server{ listen 80; server_name www.mydomain.com; location / { proxy_pass http://servers2.mydomain.com; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }

    那么PA1的配置如下:

    server{ listen 8080; server_name www.mydomain.com; index index.html; root /data/htdocs/www; }

     

    PA2配置:

    server{ listen 8081; server_name www.mydomain.com; index index.html; root /data/htdocs/www; } 配置完成

    转载请注明原文地址: https://ju.6miu.com/read-14849.html

    最新回复(0)