编译安装nginx
1.安装依赖包
1
|
yum -y
install
openssl openssl-devel zlibzlib-devel gcc gcc-c++
|
2.安装pcre
安装nginx所需要的pcre库(为了使nginx支持http rewrite模块)
1
2
3
4
5
6
7
8
|
mkdir
-p
/home/darren/tools
cd
/home/darren/tools/
wget
ftp
:
//ftp
.csx.cam.ac.uk
/pub/software/programming/pcre/pcre-8
.37.
tar
.gz
tar
zxf pcre-8.37.
tar
.gz
cd
pcre-8.37
.
/configure
make
&& makeinstall
cd
..
|
1
2
|
如果安装完以后需要重新安装pcre,则需要
rm
-rf
/usr/local/share/man/man3/pcre
*
|
3.安装nginx
创建nginx用户
1
2
|
groupaddnginx
useradd
nginx-s
/sbin/nologin
-M
|
1
2
3
4
5
6
7
|
cd
/home/darren/tools/
wget http:
//nginx
.org
/download/nginx-1
.8.1.
tar
.gz
wgethttp:
//nginx
.org
/download/nginx-1
.2.6.
tar
.gz
tarzxf nginx-1.8.1.
tar
.gz
cd
nginx-1.8.1
.
/configure
--user=nginx --group=nginx --prefix=
/usr/local/nginx
--with-http_stub_status_module --with-http_ssl_module
make
&& makeinstall
|
检查nginx安装
1
|
/usr/local/nginx/sbin/nginx
-
v
|
1
2
3
4
5
6
|
#提示错误:/usr/local/nginx/sbin/nginx: error while loading shared libraries:libpcre.so.1: cannot open shared object file: No such file ordirectory
yum
install
pcre -y 则不会出现这种错误
find
/-name
libpcre.so.1
/usr/local/lib/libpcre
.so.1
echo
'/usr/local/lib/'
>>
/etc/ld
.so.conf
ldconfig
|
检查语法
1
|
/usr/local/nginx/sbin/nginx
-t
|
启动nginx服务
1
2
|
/usr/local/nginx/sbin/nginx
netstat
-lnt
|
浏览器中输入10.1.1.1测试
附一个一键安装脚本:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
#!/bin/sh
#for one install nginx
#for wangjiadongge
#install centos source
rpm -Uvh http:
//mirrors
.yun-idc.com
/epel/6Server/x86_64/epel-release-6-8
.noarch.rpm
#install plugin
yum
install
gcc gcc-c++ openssl-devel pcre-devel zlib-devel -y
#create user and group
groupadd nginx
useradd
nginx -g nginx -s
/sbin/nologin
-M
#cd /tmp/
[ -d
/tmp/download
] ||
mkdir
-p
/tmp/download
cd
/tmp/download/
#wget nginx from www.nginx.org
wget http:
//nginx
.org
/download/nginx-1
.8.1.
tar
.gz
tar
zxvf nginx-1.8.1.
tar
.gz
cd
nginx-1.8.1
.
/configure
--prefix=
/usr/local/nginx
--user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module
make
&&
make
install
chown
-R nginx.nginx
/usr/local/nginx/
echo
"/usr/local/nginx/sbin/nginx"
>>
/etc/rc
.
local
#end
|
#nginx负载均衡脚本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
mv
/usr/local/nginx/conf/nginx
.conf
/usr/local/nginx/conf/nginx
.conf.bak
echo
"
user nginx;
worker_processes 4;
events {
use epoll;
worker_connections 10240;
}
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;
keepalive_timeout 120;
include conf.d/*.conf;
}
" >>
/usr/local/nginx/conf/nginx
.conf
mkdir
/usr/local/nginx/conf/conf
.d
echo
"
server {
listen 80;
server_name login.xiaodongge.com;
access_log logs
/host
.access.log main;
location / {
proxy_pass http:
//loginserver
;
}
}
upstream loginserver {
server 10.86.10.21:8300;
server 10.86.10.22:8300;
server 10.86.10.23:8300;
}
|
解释:nginx做负载均衡默认是轮询负载,下面逐一来看:
1
2
3
4
5
6
7
8
9
10
11
12
|
#默认的负载方式,轮询
upstream myapp1 {
server srv1.example.com;
server srv2.example.com;
server srv3.example.com;
}
server {
listen 80;
location / {
proxy_pass http:
//myapp1
;
}
}
|
1
2
3
4
5
6
7
|
#最小连接数,哪台服务器连接的最少就向哪台服务器上负载
upstream myapp1 {
least_conn;
server srv1.example.com;
server srv2.example.com;
server srv3.example.com;
}
|
1
2
3
4
5
6
7
|
#ip hash如果一个连接建立,就会始终和这台服务器连接
upstream myapp1 {
ip_hash;
server srv1.example.com;
server srv2.example.com;
server srv3.example.com;
}
|
1
2
3
4
5
6
|
#权重,自定义设置哪台服务器负载的多
upstream myapp1 {
server srv1.example.com weight=3;
server srv2.example.com;
server srv3.example.com;
}
|
版权声明:原创作品,谢绝转载。否则将追究法律责任
本文转自 王家东哥 51CTO博客,原文链接:http://blog.51cto.com/xiaodongge/1919496