Nginx负载均衡服务器配置教程

说明:

Nginx负载均衡服务器:

系统:CentOS 5.5
IP:192.168.21.164

Web服务器列表:

Web1:192.168.21.160
Web2:192.168.21.169

实现目的:

用户访问192.168.21.164服务器时,通过Nginx负载均衡到Web1和Web2服务器

准备篇:

一、配置防火墙,开启80端口
vim /etc/sysconfig/iptables   #编辑防火墙配置文件,添加以下内容
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
/etc/init.d/iptables restart #最后重启防火墙使配置生效
二、关闭SELINUX
vim /etc/selinux/config   #编辑配置文件
#SELINUX=enforcing #注释掉
#SELINUXTYPE=targeted #注释掉
SELINUX=disabled #增加
:wq #保存
shutdown -r now #重启系统
三、系统约定

软件源代码包存放位置:/usr/local/src
源码包编译安装位置:/usr/local/软件名字

四、下载软件包

1、下载nginx(目前稳定版)
http://nginx.org/download/nginx-1.0.15.tar.gz
2、下载pcre (支持nginx伪静态)
ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.30.tar.gz
3、下载ngx_cache_purge(清除指定URL缓存,方便以后扩展配置nginx缓存服务器)
http://labs.frickle.com/files/ngx_cache_purge-1.5.tar.gz

五、安装编译工具及库文件(使用CentOS yum命令安装,安装的包比较多,方便以后配置lnmp环境)
yum install make apr* autoconf automake curl curl-devel gcc gcc-c++ zlib-devel openssl openssl-devel pcre-devel gd kernel keyutils patch perl kernel-headers compat*  cpp glibc libgomp libstdc++-devel keyutils-libs-devel libsepol-devel libselinux-devel krb5-devel zlib-devel libXpm* freetype libjpeg* libpng* php-common php-gd ncurses* libtool* libxml2 libxml2-devel patch

安装篇

1、安装pcre
cd /usr/local/src
mkdir /usr/local/pcre #创建安装目录
tar zxvf pcre-8.30.tar.gz
cd pcre-8.30
./configure --prefix=/usr/local/pcre #配置
make
make install
2、安装nginx
groupadd  www  #添加www组
useradd -g www www -s /bin/false  #创建nginx运行账户www并加入到www组,不允许www用户直接登录系统cd /usr/local/src  #进入安装目录
tar  zxvf  ngx_cache_purge-1.5.tar.gz  #解压
tar  zxvf nginx-1.0.15.tar.gz  #解压
cd nginx-1.0.15
./configure --prefix=/usr/local/nginx --without-http_memcached_module
--user=www --group=www --with-http_stub_status_module --with-openssl=/usr/ --with-pcre=/usr/local/src/pcre-8.30  --add-module=../ngx_cache_purge-1.5  #配置
make
make install
/usr/local/nginx/sbin/nginx  #启动Nginx

注意:--with-pcre=/usr/local/src/pcre-8.30指向的是源码包解压的路径,而不是安装的路径,否则会报错

3、设置nginx开启启动

vim /etc/rc.d/init.d/nginx    #编辑启动文件添加下面内容
#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
#              It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/usr/local/nginx/logs/nginx.pid
RETVAL=0
prog="nginx"
# Source function library.
.  /etc/rc.d/init.d/functions
# Source networking configuration.
.  /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
echo "nginx already running...."
exit 1
fi
echo -n $"Starting $prog: "
daemon $nginxd -c ${nginx_config}
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
return $RETVAL
}
# Stop nginx daemons functions.
stop() {
echo -n $"Stopping $prog: "
killproc $nginxd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /usr/local/nginx/logs/nginx.pid
}
reload() {
echo -n $"Reloading $prog: "
#kill -HUP `cat ${nginx_pid}`
killproc $nginxd -HUP
RETVAL=$?
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;

status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|reload|status|help}"
exit 1
esac
exit $RETVAL
:wq! #保存退出
chmod 775 /etc/rc.d/init.d/nginx  #赋予文件执行权限
chkconfig nginx on    #设置开机启动
/etc/rc.d/init.d/nginx restart #重启Nginx

配置篇

配置Nginx

cp /usr/local/nginx/conf/nginx.conf  /usr/local/nginx/conf/nginx.confbak  #备份nginx配置文件
(一)、设置nginx运行账号
vim /usr/local/nginx/conf/nginx.conf   #编辑

找到user nobody;修改为user www www; 在第一行

(二)、禁止nginx空主机头
vim /usr/local/nginx/conf/nginx.conf   #编辑

找到server,在上面一行添加如下内容:

server {
listen       80 default;
server_name  _;
location / {
root   html;
return 404;
}
location ~ /.ht {
deny  all;
}
}
/etc/rc.d/init.d/nginx restart     #重启nginx

这样设置之后,空主机头访问会直接跳转到nginx404错误页面。

(三)、添加nginx虚拟主机包含文件
cd /usr/local/nginx/conf/   #进入nginx安装目录
mkdir vhost   #建立虚拟目录
vi  /usr/local/nginx/conf/nginx.conf   #编辑

找到上一步添加的代码,在最后添加如下内容:include vhost/*.conf;

server {
listen       80 default;
server_name  _;
location / {
root   html;
return 404;
}
location ~ /.ht {
deny  all;
}
}
include  vhost/*.conf; #添加这行
(四)、添加Web服务器列表文件
cd  /usr/local/nginx/conf/   #进入目录
touch  mysvrhost.conf  #建立文件
vi  /usr/local/nginx/conf/nginx.conf   #编辑
#找到上一步添加的代码,在下面添加一行
include  mysvrhost.conf;
(五)、设置nginx全局参数
vi  /usr/local/nginx/conf/nginx.conf   #编辑 
worker_processes 2;       # 工作进程数,为CPU的核心数或者两倍
events
{
use epoll;   #增加
worker_connections 65535;    #修改为65535,最大连接数。
}

以下代码在http { 部分增加与修改

server_names_hash_bucket_size 128;   #增加
client_header_buffer_size 32k;       #增加
large_client_header_buffers 4 32k;   #增加
client_max_body_size 300m;           #增加
tcp_nopush     on;      #修改为on
keepalive_timeout  60;  #修改为60
tcp_nodelay on;        #增加
server_tokens off;     #增加,不显示nginx版本信息
gzip  on;  #修改为on
gzip_min_length  1k;      #增加
gzip_buffers     4 16k;   #增加
gzip_http_version 1.1;    #增加
gzip_comp_level 2;        #增加
gzip_types       text/plain application/x-javascript text/css application/xml;  #增加
gzip_vary on;  #增加
(六)、设置Web服务器列表
cd  /usr/local/nginx/conf/   #进入目录
vi mysvrhost.conf  #编辑,添加以下代码
upstream  osyunweihost {
server 192.168.21.160:80 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.21.169:80 weight=1 max_fails=2 fail_timeout=30s;
ip_hash;
}
(七)、新建虚拟主机配置文件
cd /usr/local/nginx/conf/vhost   #进入虚拟主机目录
touch osyunwei.conf #建立虚拟主机配置文件
vi  osyunwei.conf #编辑
server
{
listen       80;
server_name  [www.osyunwei.com](http://www.osyunwei.com/) bbs.osyunwei.com sns.osyunwei.com;
location /
{
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_pass http://osyunweihost;
#proxy_redirect off;
proxy_set_header Host  $host;
proxy_set_header X-Forwarded-For  $remote_addr;
}
log_format  access  '$remote_addr - $remote_user [$time_local] $request '
'"$status" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log  /usr/local/nginx/logs/access.log  access;

location /NginxStatus {
stub_status on;
access_log  on;
auth_basic  "NginxStatus";
#auth_basic_user_file  pwd;
}

}
:wq!  #保存配置 service nginx restart  #重启nginx

至此,Nginx负载均衡服务器配置教程完成

相关配置文件

1、/usr/local/nginx/conf/nginx.conf

user  www www;
worker_processes  2;
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid;

events {
use epoll;
worker_connections  65535;
}

http {
include   mysvrhost.conf;
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;

server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 300m;
sendfile        on;
tcp_nopush     on;

#keepalive_timeout  0;
keepalive_timeout  60;
tcp_nodelay on;
server_tokens off;
gzip  on;
gzip_min_length  1k;
gzip_buffers     4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types       text/plain application/x-javascript text/css application/xml;
gzip_vary on;

server {
listen       80 default;
server_name  _;
location / {
root   html;
return 404;
}
location ~ /.ht {
deny  all;
}
}
include  vhost/*.conf;
}

/usr/local/nginx/conf/mysvrhost.conf

upstream  osyunweihost {
server 192.168.21.160:80 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.21.169:80 weight=1 max_fails=2 fail_timeout=30s;
ip_hash;
}

/usr/local/nginx/conf/vhost/osyunwei.conf

server
{
listen       80;
server_name  bbs.osyunwei.com sns.osyunwei.com;
location /
{
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_pass http://osyunweihost;
     #proxy_redirect off;
proxy_set_header Host  $host;
proxy_set_header X-Forwarded-For  $remote_addr;
}
log_format  access  '$remote_addr - $remote_user [$time_local] $request '
'"$status" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log  /usr/local/nginx/logs/access.log  access;

location /NginxStatus {
stub_status on;
access_log  on;
auth_basic  "NginxStatus";
#auth_basic_user_file  pwd;
}

}
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,835评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,598评论 1 295
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,569评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,159评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,533评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,710评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,923评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,674评论 0 203
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,421评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,622评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,115评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,428评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,114评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,097评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,875评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,753评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,649评论 2 271

推荐阅读更多精彩内容