centos7下配置nginx反向代理负载均衡集群

导读nginx是一个web服务器,类似apache一样,但是比apache性能更好,更快。还可以实现反向代理,和负载均衡,常用于构建web服务集群的负载均衡。今天就来记录一下,nginx的安装与配置,实现负载均衡的作用。

centos7下配置nginx反向代理负载均衡集群centos7下配置nginx反向代理负载均衡集群
实验环境:

192.168.1.188    nginx负载均衡器

192.168.1.189    web01服务器

192.168.1.190    web02服务器

软件准备:

centos7.4 x86_64

nginx-1.6.3.tar.gz

安装nginx软件

安装依赖软件包命令集合

[root@localhost ~]# yum -y install openssl openssl-devel pcre pcre-devel gcc

安装nginx软件包命令集合

[root@localhost ~]# mkdir /app

[root@localhost ~]# cd /app

[root@localhost ~]# wget -q http://nginx.org/download/nginx-1.6.3.tar.gz

[root@localhost ~]# useradd -s /sbin/nologin -M

[root@localhost ~]# tar xf nginx-1.6.3.tar.gz

[root@localhost ~]# cd nginx-1.6.3

[root@localhost ~]# ./configure --user=nginx --group=nginx --prefix=/app/nginx --with-http_stub_status_module --with-http_ssl_module

[root@localhost ~]# make && make install

配置文件

(以下操作在web01和web02上进行)

[root@localhost ~]# vim /app/nginx/conf/nginx.conf

将配置文件修改为以下内容

worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "http_referer" '
'"$http_user_agent" " $http_x_forwarded_for"';
server {
listen 80;
server_name bbs.dengchuanghai.org;
location / {
root html/bbs;
index index.html index.htm;
}
access_log logs/access_bbs.log main;
}

}
[root@localhost ~]# mkdir /app/nginx/html/bbs

[root@localhost ~]# echo "192.168.1.189 bbs" >>/app/nginx/html/bbs/index.html            

[root@localhost ~]# echo "192.168.1.189 bbs.dengchuanghai.org" >> /etc/hosts                     

[root@localhost ~]# echo "192.168.1.190 bbs" >>/app/nginx/html/bbs/index.html  

[root@localhost ~]# echo "192.168.1.190 bbs.dengchuanghai.org" >> /etc/hosts

(分别在两台web服务器上输入以上内容)

然后分别启动nginx

[root@localhost ~]# /app/nginx/sbin/nginx -t (检查配置文件有无错误)

[root@localhost ~]# /app/nginx/sbin/nginx  启动

[root@localhost ~]# ss -tnlp | grep 80

使用curl bbs.dengch
centos7下配置nginx反向代理负载均衡集群centos7下配置nginx反向代理负载均衡集群
,以下操作在nginx负载均衡器上进行

[root@localhost ~]# vim /app/nginx/conf/nginx.conf

更改为如下内容

worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream www_server_pools{
server 192.168.1.190:80 weight=1;
server 192.168.1.189:80 weight=1;
}
server {
listen 80;
server_name www.dengchuanghai.org;
location / {
proxy_pass http://www_server_pools;
}
}
}
[root@localhost ~]# echo "192.168.1.188 www.dengchuanghai,org" >> /etc/hosts

检查语法

[root@localhost ~]# /app/nginx/sbin/nginx -t

启动服务

[root@localhost ~]# /app/nginx/sbin/nginx

centos7下配置nginx反向代理负载均衡集群centos7下配置nginx反向代理负载均衡集群
使用curl www.dengchuanghai.org
centos7下配置nginx反向代理负载均衡集群centos7下配置nginx反向代理负载均衡集群
发现结果输出为两台服务器轮流输出

实验结束Linux就该这么学

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值