nginx的基础配置
概述:
什么是负载均衡
我们知道单台服务器的性能是有上限的,当流量很大时,就需要使用多台服务器来共同提供服务,这就是所谓的集群。
负载均衡服务器,就是用来把经过它的流量,按照某种方法,分配到集群中的各台服务器上。这样一来不仅可以承担
更大的流量、降低服务的延迟,还可以避免单点故障造成服务不可用。一般的反向代理服务器,都具备负载均衡的功能。
负载均衡功能可以由硬件来提供,比如以前的F5设备。也可以由软件来提供,LVS可以提供四层的负载均衡(利用IP和端口),
Haproxy和Nginx可以提供七层的负载均衡(利用应用层信息)。
nginx负载均衡原理
负载均衡在服务端开发中算是一个比较重要的特性。因为Nginx除了作为常规的Web服务器外,还会被大规模的用于反向代理前端,因为Nginx的异步框架可以处理很大的并发请求,把这些并发请求hold住之后就可以分发给后台服务端(backend servers, 后面简称backend)来做复杂的计算、处理和响应,并且在业务量增加的时候可以方便地扩容后台服务器。
负载均衡可以分为硬件负载均衡和软件负载均衡,前者一般是专用的软件和硬件相结合的设备,设备商会提供完整成熟的解决方案,通常也会更加昂贵。软件的复杂均衡以Nginx占据绝大多数。
源码编译三部曲:
./configure 生成makefile
make 读取makefile
make install 编译makefile
实验1:
先下载:nginx-1.14.0.tar.gz
[root@server1 ~]# ls
anaconda-ks.cfg install.log install.log.syslog nginx-1.14.0.tar.gz
[root@server1 ~]# tar zxf nginx-1.14.0.tar.gz
[root@server1 ~]# ls
anaconda-ks.cfg install.log.syslog nginx-1.14.0.tar.gz
install.log nginx-1.14.0
[root@server1 ~]# cd nginx-1.14.0
[root@server1 nginx-1.14.0]# ls
auto CHANGES.ru configure html man src
CHANGES conf contrib LICENSE README
[root@server1 nginx-1.14.0]# cd src/
[root@server1 src]# ls
core event http mail misc os stream
[root@server1 src]# cd core/
[root@server1 core]# vim nginx.h
[root@server1 core]# cd ..
[root@server1 src]# cd ..
[root@server1 nginx-1.14.0]# ls
auto CHANGES.ru configure html man src
CHANGES conf contrib LICENSE README
[root@server1 nginx-1.14.0]# cd auto/
[root@server1 auto]# ls
cc feature headers install module options stubs types
define have include lib modules os summary unix
endianness have_headers init make nohave sources threads
[root@server1 auto]# cd cc
[root@server1 cc]# ls
acc bcc ccc clang conf gcc icc msvc name owc sunc
[root@server1 cc]# vim gcc
[root@server1 cc]# cd ..
[root@server1 auto]# cd ..
[root@server1 nginx-1.14.0]# ls
auto CHANGES.ru configure html man src
CHANGES conf contrib LICENSE README
[root@server1 nginx-1.14.0]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio
checking for OS
+ Linux 2.6.32-431.el6.x86_64 x86_64
checking for C compiler ... not found
./configure: error: C compiler cc is not found
[root@server1 nginx-1.14.0]# yum install gcc
[root@server1 nginx-1.14.0]# yum install openssl-devel
##只要不报错即可
[root@server1 nginx-1.14.0]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio
checking for OS
+ Linux 2.6.32-431.el6.x86_64 x86_64
checking for C compiler ... found
+ using GNU C compiler
+ gcc version: 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC)
checking for gcc -pipe switch ... found
checking for -Wl,-E switch ... found
checking for gcc builtin atomic operations ... found
....
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
[root@server1 nginx-1.14.0]# make
make -f objs/Makefile
make[1]: Entering directory `/root/nginx-1.14.0'
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
-o objs/src/core/nginx.o \
src/core/nginx.c
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
-o objs/src/core/ngx_buf.o \
src/core/ngx_buf.c
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
-o objs/src/core/ngx_queue.o \
src/core/ngx_queue.c
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
-o objs/src/core/ngx_output_chain.o \
src/core/ngx_output_chain.c
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
-o objs/src/core/ngx_string.o \
src/core/ngx_string.c
.........................
[root@server1 nginx-1.14.0]# ls
auto CHANGES.ru configure html Makefile objs src
CHANGES conf contrib LICENSE man README
[root@server1 nginx-1.14.0]# cd /usr/local/nginx/
[root@server1 nginx]# ls
conf html logs sbin
[root@server1 nginx]# du -sh
980K .
[root@server1 nginx]# cd sbin/
[root@server1 sbin]# pwd
/usr/local/nginx/sbin
[root@server1 sbin]# ls
nginx
#制作软链接
[root@server1 sbin]# ln -s /usr/local/nginx/sbin/nginx /sbin/
[root@server1 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
[root@server1 sbin]# nginx
[root@server1 sbin]# nginx -s stop
[root@server1 sbin]# nginx
[root@server1 sbin]# nginx -s reload
[root@server1 sbin]# cd /usr/local/nginx/
[root@server1 nginx]# ls
client_body_temp fastcgi_temp logs sbin uwsgi_temp
conf html proxy_temp scgi_temp
[root@server1 nginx]# cd html/
[root@server1 html]# ls
50x.html index.html
[root@server1 html]# pwd
/usr/local/nginx/html
[root@server1 html]# vim test.html
写入 www.westos.org
测试:
实验2:
[root@server1 html]# cd /usr/local/nginx/
[root@server1 nginx]# ls
client_body_temp fastcgi_temp logs sbin uwsgi_temp
conf html proxy_temp scgi_temp
[root@server1 nginx]# pwd
/usr/local/nginx
[root@server1 nginx]# cd conf/
[root@server1 conf]# ls
fastcgi.conf koi-win scgi_params
fastcgi.conf.default mime.types scgi_params.default
fastcgi_params mime.types.default uwsgi_params
fastcgi_params.default nginx.conf uwsgi_params.default
koi-utf nginx.conf.default win-utf
[root@server1 conf]# pwd
/usr/local/nginx/conf
[root@server1 conf]# nginx -s reload
[root@server1 conf]# ls
fastcgi.conf koi-win scgi_params
fastcgi.conf.default mime.types scgi_params.default
fastcgi_params mime.types.default uwsgi_params
fastcgi_params.default nginx.conf uwsgi_params.default
koi-utf nginx.conf.default win-utf
[root@server1 conf]# sysctl -a | grep file
fs.file-nr = 480 0 98864
fs.file-max = 98864 #文件最大数
[root@server1 conf]# lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 1 ##cpu为1
On-line CPU(s) list: 0
Thread(s) per core: 1
Core(s) per socket: 1
Socket(s): 1
NUMA node(s): 1
Vendor ID: GenuineIntel
CPU family: 6
Model: 42
Stepping: 1
CPU MHz: 2491.902
BogoMIPS: 4983.80
Hypervisor vendor: KVM
Virtualization type: full
L1d cache: 32K
L1i cache: 32K
L2 cache: 4096K
NUMA node0 CPU(s): 0
[root@server1 conf]# useradd -M -d /usr/nginx/ nginx
[root@server1 conf]# vim /etc/security/limits.conf
####################
添加: nginx - nofile 65536
[root@server1 conf]# id nginx
uid=500(nginx) gid=500(nginx) groups=500(nginx)
[root@server1 conf]# vim nginx.conf
####################
更改:
添加用户,组
2 user nginx nginx;
设定工作cpu
3 worker_processes 1;
4 worker_cpu_affinity 01;
设定最大工作连接数
12 events {
13 worker_connections 65535;
14 }
17 http {
18 upstream westos{
19 server 172.25.52.22:80;
20 server 172.25.52.33:80;
21 }
121 server{
122 listen 80; #监听端口
123 server_name www.westos.org; #域名
124 location / {
125 proxy_pass http://westos; #站点目录
126 }
127 }
128 }
[root@server1 conf]# 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
[root@server1 conf]# nginx -s relead
nginx: invalid option: "-s relead"
[root@server1 conf]# nginx -s reload
[root@server1 conf]# vim /etc/security/limits.conf
####################
添加: nginx -nofile 47536
[root@server2 ~]# /etc/init.d/httpd start
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using 172.25.52.2 for ServerName
[root@server3 ~]# /etc/init.d/httpd start
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using 172.25.52.3 for ServerName
[ OK ]
[ OK ]
测试:
server2 server3 交替更改
注意:如果关闭server2的apache 则网页上只出现server3
[root@server1 conf]# vim nginx.conf
更改:
http {
upstream westos{
server 172.25.52.22:80 weight=2; #加权重
server 172.25.52.33:80;
}
include mime.types;
default_type application/octet-stream;
[root@server1 conf]# 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
[root@server1 conf]# nginx -s reload
测试:
[root@foundation52 ~]# for i in {1..10}; do curl www.westos.org; done
www.westos.org - server2
www.westos.org - server3
www.westos.org - server2
www.westos.org - server2
www.westos.org - server3
www.westos.org - server2
www.westos.org - server2
www.westos.org - server3
www.westos.org - server2
www.westos.org - server2
添加本机策略
[root@server1 conf]# vim nginx.conf
#################
更改:
http {
upstream westos{
server 172.25.52.22:80;
server 172.25.52.33:80;
server 127.0.0.1:80 backup; #添加本机策略
}
[root@server1 conf]# 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
[root@server1 conf]# nginx -s reload
[root@server2 ~]# /etc/init.d/httpd stop
Stopping httpd: [ OK ]
[root@server3 ~]# /etc/init.d/httpd stop
Stopping httpd: [ OK ]
测试:
将server2和server3的阿帕奇停止后会访问本机:
实验3~添加模块
先下载: nginx-1.10.1.tar.gz
[root@server1 ~]# ls
anaconda-ks.cfg nginx-1.10.1.tar.gz nginx-sticky-module-ng.tar.gz
install.log nginx-1.14.0
install.log.syslog nginx-1.14.0.tar.gz
[root@server1 ~]# nginx -s stop
[root@server1 ~]# tar zxf nginx-1.10.1.tar.gz
[root@server1 ~]# ls
anaconda-ks.cfg nginx-1.10.1 nginx-1.14.0.tar.gz
install.log nginx-1.10.1.tar.gz nginx-sticky-module-ng.tar.gz
install.log.syslog nginx-1.14.0
[root@server1 ~]# tar zxf nginx-sticky-module-ng.tar.gz
[root@server1 ~]# ls
anaconda-ks.cfg nginx-1.10.1 nginx-1.14.0.tar.gz
install.log nginx-1.10.1.tar.gz nginx-sticky-module-ng
install.log.syslog nginx-1.14.0 nginx-sticky-module-ng.tar.gz
[root@server1 ~]# cd nginx-1.10.1
##源码编译
[root@server1 nginx-1.10.1]# ./configure --prefix=/opt/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio --add-module=/root/nginx-sticky-module-ng
checking for OS
+ Linux 2.6.32-431.el6.x86_64 x86_64
checking for C compiler ... found
+ using GNU C compiler
+ gcc version: 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC)
checking for gcc -pipe switch ... found
checking for -Wl,-E switch ... found
checking for gcc builtin atomic operations ... found
checking for C99 variadic macros ... found
..........
Configuration summary
+ using threads
+ using system PCRE library
+ using system OpenSSL library
+ md5: using OpenSSL library
+ sha1: using OpenSSL library
+ using system zlib library
.................
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
[root@server1 nginx-1.10.1]# make
make -f objs/Makefile
make[1]: Entering directory `/root/nginx-1.10.1'
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
-o objs/src/core/nginx.o \
src/core/nginx.c
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
-o objs/src/event/ngx_event_pipe.o \
src/event/ngx_event_pipe.c
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
-o objs/src/os/unix/ngx_time.o \
src/os/unix/ngx_time.c
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I o
.............
[root@server1 nginx-1.10.1]# make install
make -f objs/Makefile install
make[1]: Entering directory `/root/nginx-1.10.1'
test -d '/usr/opt/nginx' || mkdir -p '/usr/opt/nginx'
...............
|| mkdir -p '/usr/opt/nginx/logs'
test -d '/usr/opt/nginx/html' \
|| cp -R html '/usr/opt/nginx'
test -d '/usr/opt/nginx/logs' \
|| mkdir -p '/usr/opt/nginx/logs'
make[1]: Leaving directory `/root/nginx-1.10.1'
[root@server1 nginx-1.10.1]#cd conf
[root@server1 conf]# pwd
/root/nginx-1.10.1/conf
[root@server1 conf]# cd /opt/nginx
[root@server1 nginx]# ls
conf html logs sbin
[root@server1 nginx]# cd conf/
[root@server1 conf]# ls
fastcgi.conf koi-win scgi_params
fastcgi.conf.default mime.types scgi_params.default
fastcgi_params mime.types.default uwsgi_params
fastcgi_params.default nginx.conf uwsgi_params.default
koi-utf nginx.conf.default win-utf
[root@server1 conf]# cp /local/nginx/conf/nginx.conf .
cp: overwrite `./nginx.conf'? yes
[root@server1 conf]# vim nginx.conf
####################
http {
upstream westos{
sticky; #添加Sticky模块
server 172.25.52.22:80;
server 172.25.52.33:80;
#server 127.0.0.1:80 ;
}
##注意:要用绝对路径调用 防止与旧版的发生冲突
[root@server1 conf]# /usr/opt/nginx/sbin/nginx -t #检测语法是否正确
nginx: the configuration file /usr/opt/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/opt/nginx/conf/nginx.conf test is successful
[root@server1 conf]# /opt/nginx/sbin/nginx #开启新版本的nginx
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
[root@server2 ~]# /etc/init.d/httpd start
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using 172.25.52.2 for ServerName
[root@server3 ~]# /etc/init.d/httpd start
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using 172.25.52.3 for ServerName
[ OK ]
[ OK ]
在网页上测试:
由于这个策略是基于浏览器的,故在真机测试效果不正确