nginx的基本配置 1.tar zxf nginx-1.12.0.tar.gz vim nginx-1.12.0/src/core/nginx.h 12 #define nginx_version 1012000 13 #define NGINX_VERSION "1.12.0" 14 #define NGINX_VER "nginx/" (隐藏nginx的版本号) vim nginx-1.12.0/auto/cc/gcc 171 # debug 172 #CFLAGS="$CFLAGS -g" 关闭debug模块,减少nginx模块大小

2.开始编译

cd nginx-1.12.0 ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module yum install -y pcre-devel 解决依赖,缺少什么装什么 make make install ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ 做个软连接 nginx -t 检查语法错误 nginx 启动nginx 此时nginx安装完成

3.nginx 实现反向代理和负载均衡(主服务器server1,后端的负载均衡服务器server2和server3) server2和server3服务器分别安装http服务器,并且在默认发布目录下写上你需要的东西,然后启动http服务器 2 user nginx nginx; 12 events { 13 worker_connections 65535; 14 } upstream westos { 35 server 172.25.45.2:80; 36 server 172.25.45.3:80; http中增加upstream模块 37 } server { 122 listen 80; 123 server_name www.westos.org; http中增加反向代理模块 124 125 location / { 126 proxy_pass http://westos; 127 } 128 } nginx -t nginx -s reload 测试 真机:访问www.westos.org server2和server3轮询

4.nginx自带健康检查

34 upstream westos { 35 server 172.25.45.2:80;130
36 server 172.25.45.3:80; 37 server 127.0.0.1:8080 backup; 38 }

130 server { 131 listen 8080; 132 server_name 127.0.0.1; 133 134 location / { 135 root /backup; 136 index index.html;
137 charset utf-8; 识别中文 添加一个虚拟主机模块 138 } 139 } nginx -t nginx -s reload mkdir /backup echo "系统正在维护中.." > /backup/index.html 测试;当同时关掉server2和server3服务器时,页面显示“系统正在维护中”

5.第三方sticky模块,持续连接,解决session丢失问题

重新编译安装一个低版本的nginx软件包 cd /root tar zxf nginx-1.10.1.tar.gz tar zxf nginx-sticky-module-ng.tar.gz cd nginx-1.10.1 ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --add-module=/root/nginx-sticky-module-ng make make install cp nginx.conf /usr/local/nginx/conf/ 将前面做实验的conf文件拷贝过来 vim /usr/local/nginx/conf/nginx.conf 34 upstream westos { 35 sticky;
36 server 172.25.45.2:80; 37 server 172.25.45.3:80; 38 # server 127.0.0.1:8080 backup; 39 } nginx -t nginx -s reload

测试: 浏览器访问www.westos.org,进入持续连接的状态,当一台服务器出现问题时,才会持续连接到另外一台服务器上