Nginx静态编译、负载均衡、添加模块

######1、实验环境

  • server5 172.25.29.5 nginx
  • server2 172.25.29.2 apache
  • server3 172.25.29.3 apache

######2、nginx的静态编译

[root@server5 ~]# ls
nginx-1.14.0.tar.gz
[root@server5 ~]# tar zxf nginx-1.14.0.tar.gz 
[root@server5 ~]# ls
nginx-1.14.0  nginx-1.14.0.tar.gz
[root@server5 ~]# cd nginx-1.14.0
[root@server5 nginx-1.14.0]# cd src/core/
[root@server5 core]# vim nginx.h
 14 #define NGINX_VER          "nginx/"    //修改server名称
[root@server5 core]# cd ~/nginx-1.14.0/
[root@server5 nginx-1.14.0]# cd auto/cc
[root@server5 cc]# vim gcc 
 171 # debug
 172 #CFLAGS="$CFLAGS -g"
[root@server5 nginx-1.14.0]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module  --with-http_stub_status_module --with-threads --with-file-aio	//生成Makefile文件,检测编译环境是否完善
[root@server5 nginx-1.14.0]# yum install gcc  pcre-devel  openssl-devel -y	//解决依赖性
[root@server5 nginx-1.14.0]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module  --with-http_stub_status_module --with-threads --with-file-aio
[root@server5 nginx-1.14.0]# make
[root@server5 nginx-1.14.0]# make install
[root@server5 nginx-1.14.0]# cd /usr/local/nginx/
[root@server5 nginx]# cd sbin/
[root@server5 sbin]# ln -s /usr/local/nginx/sbin/nginx /sbin/
[root@server5 sbin]# nginx 	//开启
[root@server5 sbin]# nginx -t 	//检测语法错误
[root@server5 sbin]# nginx -s reload 	//重新加载
[root@server5 sbin]# nginx -s stop	//停止
[root@server5 sbin]# nginx 
[root@server5 sbin]# cd -
[root@server5 nginx]# cd html/
[root@server5 html]# vim test.html

www.westos.org

Nginx静态编译、负载均衡、添加模块_第1张图片
######3、nginx实现负载均衡

[root@server5 cc]# cd /usr/local/nginx/conf/
[root@server5 conf]# lscpu
CPU(s):                2
[root@server5 conf]# vim nginx.conf
  1 user  nginx;
  2 worker_processes 2;	  //cpu数
  3 worker_cpu_affinity 01 10;	//Worker和cpu绑定

 12 events {
 13     worker_connections  65535;    //每个worker的最大处理量
 14 }

 17 http {
 18         upstream westos{
 19         server 172.25.29.2:80 weight=3;	//后端端口可以不是80,支持端口转发,weight表示权重
 20         server 172.25.29.3:80;
 21         server 127.0.0.1:80 backup;
 22         }

122         server{
123         listen 80;
124         server_name www.westos.org;
125 
126         location / {
127         proxy_pass http://westos;
128         }
129 
130                 }
131 }
[root@server5 conf]# useradd -M -d /usr/local/nginx/ nginx
[root@server5 conf]# vim /etc/security/limits.conf 
 52   nginx           -       nofile          65535
[root@server5 conf]# nginx -t
[root@server5 conf]# nginx -s reload

#########server2:

[root@server2 ~]# cd /var/www/html/
[root@server2 html]# vim index.html
server2
[root@server2 html]# /etc/init.d/httpd start

#########server3:

[root@server3 ~]# cd /var/www/html/
[root@server3 html]# vim index.html
server3
[root@server3 html]# /etc/init.d/httpd start

#########主机:

[root@foundation29 ~]# vim /etc/hosts
172.25.29.5 www.westos.org       bbs.westos.org         westos.org

Nginx静态编译、负载均衡、添加模块_第2张图片
######4、给nginx添加模块
Nginx静态编译,添加扩展模块需要重新编译

[root@server5 ~]# ls
nginx-1.10.1.tar.gz              nginx-sticky-module-ng.tar.gz
[root@server5 ~]# /usr/local/nginx/sbin/nginx  -s stop
[root@server5 ~]# tar zxf nginx-sticky-module-ng.tar.gz 
[root@server5 ~]# tar nginx-1.10.1.tar.gz
[root@server5 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
[root@server5 nginx-1.10.1]# make
[root@server5 nginx-1.10.1]# make install
[root@server5 nginx-1.10.1]# /opt/nginx/sbin/nginx
[root@server5 nginx-1.10.1]# cd /opt/nginx/conf/
[root@server5 conf]# cp /usr/local/nginx/conf/nginx.conf .
[root@server5 conf]# vim nginx.conf
http {
        upstream westos{
        sticky;
        server 172.25.29.2:80;
        server 172.25.29.3:80;
        }

[root@server5 conf]# /opt/nginx/sbin/nginx -t
[root@server5 conf]# /opt/nginx/sbin/nginx -s reload

Sticky是nginx的一个模块,它是基于cookie的一种nginx的负载均衡解决方案,通过分发和识别cookie,来使同一个客户端的请求落在同一台服务器上
浏览器查看,可以看到cookie
Nginx静态编译、负载均衡、添加模块_第3张图片

你可能感兴趣的:(Linux)