阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

WEB服务器负载均衡之Nginx

138次阅读
没有评论

共计 2489 个字符,预计需要花费 7 分钟才能阅读完成。

负载均衡(Load Balancing)负载均衡建立在现有网络结构之上,它提供了一种廉价有效透明的方法扩展网络设备和服务器的带宽、增加吞吐量、加强网络数据处理能力、提高网络的灵活性和可用性。

最近在自学负载均衡技术,网络上有不少文章,但写的都不是很明了,现把测试的笔记日记发表出来,以供大家学习使用;本文只介绍入门级的负载均衡,进阶还在学习中……

一、基础信息

操作系统:CentOS6.0

服务器:三台服务器(Load Balancing、WEB1、WEB2)

负载均衡技术:Nginx

web 服务技术:apache

IP 地址:Nginx 192.168.1.113、Web1 192.168.1.77、Web2 192.168.1.78


 

二、Web 服务器的安装与配置

请参考本博客的 apache 服务器搭建文章:http://www.linuxidc.com/Linux/2014-01/95256.htm


 

三、负载均衡服务器 (Nginx) 的安装与配置

3.1 安装基础支持套件

yum -y install gcc openssl openssl-devel pcre pcre-devel

 

3.2 安装 nginx

cd
wget http://nginx.org/download/nginx-1.5.8.tar.gz
tar xzvf nginx-1.5.8.tar.gz
cd nginx-1.5.8
./cofnigure –prefix=/usr/local/nginx
make
make install

 

3.3 配置 nginx

编辑 /usr/local/nginx/conf/nginx.conf

cd /usr/local/nginx/conf/

mv nginx.conf nginx.conf.bak

vim nginx.conf

新增如下内容

user apache;
worker_processes 10;
pid logs/nginx.pid;
events {
use epoll;
worker_connections 2048;
}
http {
include mime.types;
default_type application/octet-stream;

log_format main ‘$remote_addr – $remote_user [$time_local] “$request” ‘
‘$status $body_bytes_sent “$http_referer” ‘
‘”$http_user_agent” “$http_x_forwarded_for”‘;
access_log logs/access.log main;
sendfile on;
tcp_nopush on;
upstream linuxidc {
server 192.168.1.77:80;
server 192.168.1.78:80;
}
keepalive_timeout 65;
server {
listen 80;
server_name www.linuxidc.com;
access_log logs/linuxidc.access.log main;
location / {
proxy_pass http://linuxidc;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}

 

注:红色字体部分,可根据自己的实际情况进行更改即可;


 

四、启动 Nginx

4.1 验证 nginx 配置是否正常

# /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

 

4.2 启动 Nginx

# /usr/local/nginx/sbin/nginx

 

4.3 停止 Nginx

# killall -9 nginx

 

4.4 DNS 域名解析

有域名的同学可以上 DNS 服务商上进行解析设置;我是更改本地电脑 hosts 文件来进行测试的,编辑 C:\Windows\System32\drivers\etc\hosts,在最后一行新增如下内容:

 

192.168.1.113 www.linuxidc.com

 

4.5 测试访问

打开浏览器 输入 http://www.linuxidc.com 即能显示出 web 服务器的网站内容,停掉其中一台 web 服务器也不影响用户的正常使用;


 

结束词:Nginx 实现负载均衡就是这样简单,给一万个赞,进阶配置学习中……

推荐阅读

Nginx 实现反向代理和负载均衡的配置及优化 http://www.linuxidc.com/Linux/2013-11/92909.htm

Nginx 做负载均衡报:nginx: [emerg] could not build the types_hash http://www.linuxidc.com/Linux/2013-10/92063.htm

Nginx 负载均衡模块 ngx_http_upstream_module 详述 http://www.linuxidc.com/Linux/2013-10/91907.htm

Nginx+Firebug 让浏览器告诉你负载均衡将请求分到了哪台服务器 http://www.linuxidc.com/Linux/2013-10/91824.htm

Ubuntu 安装 Nginx php5-fpm MySQL(LNMP 环境搭建) http://www.linuxidc.com/Linux/2012-10/72458.htm

Nginx 的详细介绍:请点这里
Nginx 的下载地址:请点这里

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-20发表,共计2489字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中