1.1web服务软件种类介绍

**常用来提供静态Web服务的软件有如下三种:**
**Apache:**  
这是中小型Web服务的主流,Web服务器中的老大哥。
**Nginx:**   
大型网站Web服务的主流,曾经Web服务器中的初生牛犊,现已长大。
Nginx的分支Tengine(http://tengine.taobao.org/)目前也在飞速发展。
**Lighttpd:**
这是一个不温不火的优秀Web软件,社区不活跃,静态解析效率很高。
在Nginx流行前,它是大并发静态业务的首选,国内百度贴吧、豆瓣等众多网站都有Lighttpd奋斗的身影。

**常用来提供动态服务的软件**
**PHP(FastCGI):**
大中小型网站都会使用,动态网页语言PHP程序的解析容器。
它可配合Apache解析动态程序,不过,这里的PHP不是FastCGI守护进程模式,而是mod_php5.so(module)。
也可配合Nginx解析动态程序,此时的PHP常用FastCGI守护进程模式提供服务。
**Tomcat:**
中小企业动态Web服务主流,互联网Java容器主流(如jsp、do)。
**Resin:**
大型动态Web服务主流,互联网Java容器主流(如jsp、do)。

1.2nginx软件服务介绍

如果你听说或使用过Apache软件,那么很快就会熟悉Nginx软件,与Apache软件类似,
Nginx(“engine x”)是一个开源的,支持高性能、高并发的WWW服务器和代理服务软件。
它是由俄罗斯人lgor Sysoev开发的,最初被应用在俄罗斯的大型网站www.rambler.ru上。
后来作者将源代码以类BSD许可证的形式开源出来供全球使用。
Nginx可以运行在UNIX、Linux、BSD、Mac OS X、Solaris,以及Microsoft Windows等操作系统中

1.3nginx软件特征

· 支持高并发:能支持几万并发连接(特别是静态小文件业务环境)
· 资源消耗少:在3万并发连接下,开启10个Nginx线程消耗的内存不到200MB
· 支持异步网络I/O事件模型epoll(Linux 2.6+) apache(select)

1.4nginx软件功能介绍

1)作为Web服务软件(处理用户访问静态请求)
2)反向代理或负载均衡服务
3)前端业务数据缓存服务

1.5nginx软件模型特点说明

apache与nginx软件对比说明???
apache使用select模型
nginx使用epoll模型
举例说明:宿舍管理员
select模型版管理员 会一个一个房间查询人员
epoll模型版管理员 会进行检索后,直接找到需要找的人
举例说明:幼儿园阿姨
select模型版阿姨 会一个一个小朋友进行询问,确认哪个小朋友需要上厕所
epoll模型版阿姨 会告知想上厕所小朋友自觉站到响应位置

1.6nginx软件编译安装

第一个里程:软件依赖包安装
pcre-devel: perl语言正则表达式兼容软件包
openssl-devel:使系统支持https方式访问
yum install -y pcre-devel openssl-devel

第二个里程:创建一个管理nginx进程的虚拟用户
useradd www -s /sbin/nologin/ -M

第三个里程:下载并解压nginx软件
cd /server/tools
wget http://nginx.org/download/nginx-1.12.2.tar.gz
tar xf nginx-1.12.2.tar.gz

第四个里程:进行软件编译安装
软件编译安装三部曲:
①. 编译配置
./configure --prefix=/application/nginx-12.2 --user=www --group=www --with-http_ssl_module --with-http_stub_status_module
--prefix=PATH 指定软件安装在什么目录下
--user=USER 指定软件worker进程管理用户,利用www虚拟用户管理worker进程
--group=USER
--with-http_ssl_module 使nginx程序可以支持HTTPsF访问功能
--with-http_stub_status_module  用于监控用户访问nginx服务情况   
②. 编译过程
③. 编译安装

第五个里程:为nginx程序软件创建链接目录
ln -s /application/nginx-12.2 /application/nginx

第六个里程:启动nginx程序服务
/application/nginx/sbin/nginx
[root@m01 ~]# cat /etc/ansible/playbook/nginx-new.yaml 
- hosts: 172.16.1.7
  tasks:
    - name: yum install -y openssl-devel pcre-devel
      yum: name=openssl-devel state=installed
    - name: yum install -y pcre-devel
      yum: name=pcre-devel state=installed
    - name: useradd www -s /sbin/nologin -M
      user: name=www createhome=no shell=/sbin/nologin
    - name: mkdir -p /server/tools
      file: name=/server/tools state=directory
    - name: mkdir -p /application
      file: name=/application state=directory
    - name: copy nginx package to 7
      copy: src=/etc/ansible/conf/nginx/nginx-1.12.2.tar.gz dest=/server/tools/
    - name: tar -xf /server/tools/nginx-1.12.2.tar.gz
      shell: cd /server/tools/;tar -xf nginx-1.12.2.tar.gz
    - name: mv /server/tools/nginx-1.12.2 /server/tools/nginx
      shell: cd /server/tools/;mv nginx-1.12.2 nginx
    - name: compile nginx package
      shell: cd /server/tools/nginx;./configure --prefix=/application/nginx --user=www --group=www --with-http_ssl_module --with-http_stub_status_module
    - name: make&&make install
      shell: cd /server/tools/nginx;make&&make install
    - name: start nginx
      shell: /application/nginx/sbin/nginx 

1.7nginx软件程序目录结构

[root@m01 nginx]# ll
total 36
drwx------ 2 www  root 4096 May 29 20:00 client_body_temp
drwxr-xr-x 2 root root 4096 Jun 20 16:29 conf
drwx------ 2 www  root 4096 May 29 20:00 fastcgi_temp
drwxr-xr-x 3 root root 4096 May 29 20:06 html
drwxr-xr-x 2 root root 4096 May 29 20:09 logs
drwx------ 2 www  root 4096 May 29 20:00 proxy_temp
drwxr-xr-x 2 root root 4096 May 29 20:00 sbin
drwx------ 2 www  root 4096 May 29 20:00 scgi_temp
drwx------ 2 www  root 4096 May 29 20:00 uwsgi_temp
[root@m01 nginx]# pwd
/application/nginx/nginx
[root@m01 nginx]# 

conf --- nginx程序所有配置文件保存目录
nginx.conf nginx程序主配置文件
精简nginx.conf配置文件内容:
grep -Ev "#|^$" nginx.conf.default >nginx.conf

nginx配置文件组成:
①. main nginx主区块
②. event nginx事件区块
③. http nginx http功能区块
④. server nginx 网站主机区块
⑤. location nginx 匹配或者定位区块

html    --- nginx程序站点目录
logs --- nginx程序日志文件保存目录
sbin --- nginx程序命令所在目录
nginx命令参数说明
-V --- 查看nginx软件编译配置参数
-t --- 检查nginx配置文件语法格式是否正确
-s --- 用于管理nginx服务运行状态
stop 停止nginx服务
reload 平滑重启nginx服务器
重启nginx服务
nginx -s stop 先停止
nginx 再启动

13.nginx_第1张图片

1.8编写nginx服务配置

三个语法格式说明:
①. 大括号要成对出现
②. 每一行指令后面要用分号结尾
③. 每一个指令要放置在指定的区块中

1.8.1实现编写一个网站页面

worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.etiantian.org;
location / {
root html/www;
index index.html index.htm;
}
}
}

1.8.2实现编写多个网站页面==编写多个虚拟主机(等于一个网站)

第一个里程编写配置文件:
server {
listen 80;
server_name www.etiantian.org;
location / {
root html/www;
index index.html index.htm;
}
}
server {
listen 80;
server_name bbs.etiantian.org;
location / {
root html/bbs;
index index.html index.htm;
}
}
server {
listen 80;
server_name blog.etiantian.org;
location / {
root html/blog;
index index.html index.htm;
}
}
第二个里程创建站点目录:
mkdir -p /application/nginx/html/{www,bbs,blog}

第三个里程创建站点目录下首页文件:
for name in www bbs blog;do echo "10.0.0.7 name.etiantian.org">/application/nginx/html/name/index.html;done
for name in www bbs blog;do cat /application/nginx/html/$name/index.html;done
10.0.0.7 www.etiantian.org
10.0.0.7 bbs.etiantian.org
10.0.0.7 blog.etiantian.org

第四个里程:进行访问测试
浏览器访问测试:
注意:需要编写windows主机hosts文件,进行解析
命令行访问测试:
利用curl命令在linux系统中访问测试
注意:需要编写linux主机hosts文件,进行解析

虚拟主机配置文件编写方法:
①. 基于域名的虚拟主机配置方法(最常用)
②. 基于端口的虚拟主机配置方法
说明:当你访问的网站域名在虚拟主机配置中不存在时,默认会将第一个虚拟主机的配置页面响应给用户
③. 基于IP地址的虚拟主机配置方法
说明:nginx服务中只要涉及IP地址的修改,都需要重启nginx服务,而不能采用平滑重启

1.9Nginx服务日志信息

1.9.1. 错误日志

Syntax: error_log file [level];
Default:    
error_log logs/error.log error;
Context:    main, http, mail, stream, server, location
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

vim nginx.conf
error_log /tmp/error.log error;

补充说明:

错误日志的,默认情况下不指定也没有关系,因为nginx很少有错误日志记录的。
但有时出现问题时,是有必要记录一下错误日志的,方便我们排查问题。
error_log 级别分为 debug, info, notice, warn, error, crit 默认为crit
该级别在日志名后边定义格式如下:
error_log /your/path/error.log crit;

crit 记录的日志最少,而debug记录的日志最多。
如果nginx遇到一些问题,比如502比较频繁出现,但是看默认的error_log并没有看到有意义的信息,
那么就可以调一下错误日志的级别,当你调成error级别时,错误日志记录的内容会更加丰富

1.9.2 访问日志(重点关注)

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;                    --- 调用定义格式信息,生成访问日志
$remote_addr       10.0.0.1           --- 访问客户端的源地址信息
$remote_user          -               --- 访问客户端认证用户信息   ???
[$time_local]                         --- 显示访问时间
$request        GET / HTTP/1.1        --- 请求行信息
$status              304              --- 状态码信息(304状态码利用缓存显示页面信息)
$body_bytes_sent                      --- 服务端响应客户端的数据大小信息
$http_referer                         --- 记录链接到网站的域名信息  ???
$http_user_agent                      --- 用户访问网站客户端软件标识信息
                                          用户利用客户端浏览器测试访问时,win10默认浏览器会有异常问
$http_x_forwarded_for                 --- ???  反向代理
官方链接:http://nginx.org/en/docs/http/ngx_http_log_module.html#access_log

1.10location区块说明

企业需求
搭建好一台nginx的web服务器。配置好内网卡地址和外网卡地址
web服务的网站域名为www.etiantian.org,站点目录为html/www
要求内网用户可以访问http://www.etiantian.org/AV资源
要求外网用户不可以访问http://www.etiantian.org/AV资源

1.如何利用Nginx进行访问控制
deny allow
ngx_http_access_module ---实现访问控制模块
location / {
deny 192.168.1.1;
allow 192.168.1.0/24;
allow 10.1.1.0/16;
allow 2001:0db8::/32;
deny all;
}

2.如何定位站点目录资源信息
location区块进行定位站点目录下资源信息
Syntax: location [=|~*|^~] uri {...}
操作

[root@web01 nginx]#cat nginx.conf
location /AV {
allow 172.16.1.0/24;
deny 10.0.0.0/24;
root html/bbs;
index index.html index.htm;
}

[root@web01 nginx]# mkdir html/bbs/AV
[root@web01 nginx]# echo "AV">html/bbs/AV/av.html
[root@web01 nginx]# pkill nginx
[root@web01 nginx]# ./sbin/nginx
验证

[root@m01 ansible]# cat /etc/hosts
10.0.0.7 bbs.etiantian.org

[root@m01 ansible]# curl bbs.etiantian.org/AV/av.html

403 Forbidden

403 Forbidden


nginx/1.14.2
[root@m01 ansible]# cat /etc/hosts 172.16.1.7 bbs.etiantian.org [root@m01 ansible]# curl bbs.etiantian.org/AV/av.html AV 详细讲解location location [=|~|^~] uri {...} = 精确匹配网站uri资源信息 ~ 区分大小写匹配网站uri资源信息 ~ 不区分大小写匹配网站资源信息 ^~ 优先匹配网站uri资源信息 /AV/ 指定匹配网站资源目录信息 / 默认匹配网站资源信息 ! 对匹配的内容进行取反 location = /{ } --精确匹配优先级最高 location ^~ /images/{ } --优先匹配优先级第二 location ~* .(gif|jpg|jpeg)$ /{ } --不区分大小写匹配网站资源,优先级第三 location /{ } -- 所有匹配都不满足,匹配默认location

1.11rewrite

1.11.1Nginx服务rewrite模块功能说明

1.实现域名地址信息跳转
2.用于做伪静态
www.etiantian.org/oldboy?edu.html ---动态资源
www.etiantian.org/oldboy-edu.html ---伪静态

实现类似百度重写域名的功能?
baidu.com ===> www.baidu.com
etiantian.org ===> www.etiantian.org

rewrite
Syntax: rewrite regex replacement [flag];
Default: —
Context: server, location, if

last
stops processing the current set of ngx_http_rewrite_module directives and starts a search for a new location matching the changed URI;
break
stops processing the current set of ngx_http_rewrite_module directives as with the break directive;
redirect
returns a temporary redirect with the 302 code; used if a replacement string does not start with “http://”, “https://”, or “$scheme”;
permanent
returns a permanent redirect with the 301 code.

1.11.2rewrite指令实践操作一:(错误)

[root@web01 extra]# cat bbs.conf 
 server {
     listen       80;
     server_name  www.etiantian.org bbs.org;
     rewrite ^/(.*) http://www.etiantian.org/$1 permanent;
     root   html/bbs;
     index  index.html index.htm;
 }

 -L 意思是查看跳转到了哪里,最大查看50层
[root@web01 extra]# curl -L etiantian.org
curl: (47) Maximum (50) redirects followed

-v 查看详细的跳转信息
[root@web01 extra]# curl -Lv etiantian.org --- 显示无限循环过程
说明:以上配置进入了无限循环状态
 [root@m01 ~]# curl  -Lv www.etiantian.org
* About to connect() to www.etiantian.org port 80 (#0)
*   Trying 10.0.0.7... connected
* Connected to www.etiantian.org (10.0.0.7) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.21 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Host: www.etiantian.org
> Accept: */*
> 
< HTTP/1.1 301 Moved Permanently
< Server: nginx/1.14.2
< Date: Wed, 13 Mar 2019 14:07:55 GMT
< Content-Type: text/html
< Content-Length: 185
< Connection: keep-alive
< Location: http://www.etiantian.org/
< 
* Ignoring the response-body
* Connection #0 to host www.etiantian.org left intact
* Issue another request to this URL: 'http://www.etiantian.org/'
* Re-using existing connection! (#0) with host www.etiantian.org
* Connected to www.etiantian.org (10.0.0.7) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.21 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Host: www.etiantian.org
> Accept: */*
> 
< HTTP/1.1 301 Moved Permanently
< Server: nginx/1.14.2
< Date: Wed, 13 Mar 2019 14:07:55 GMT
< Content-Type: text/html
< Content-Length: 185
< Connection: keep-alive
< Location: http://www.etiantian.org/
< 
* Ignoring the response-body
* Connection #0 to host www.etiantian.org left intact
* Issue another request to this URL: 'http://www.etiantian.org/'
* Re-using existing connection! (#0) with host www.etiantian.org
* Connected to www.etiantian.org (10.0.0.7) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.21 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Host: www.etiantian.org
> Accept: */*

1.11.3rewrite指令实践操作二:(正确)

cat bbs.conf 
server {
    listen 80;
    server_name etiantian.org;
    rewrite ^/(.*) http://bbs.etiantian.org/$1 permanent;
}
server {
    listen       80;
    server_name  bbs.etiantian.org bbs.org;
    root   html/bbs;
    index  index.html index.htm;
}

[root@m01 ~]# curl -Lv  etiantian.org
* About to connect() to etiantian.org port 80 (#0)
*   Trying 10.0.0.7... connected
* Connected to etiantian.org (10.0.0.7) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.21 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Host: etiantian.org
> Accept: */*
> 
< HTTP/1.1 301 Moved Permanently
< Server: nginx/1.14.2
< Date: Wed, 13 Mar 2019 14:15:41 GMT
< Content-Type: text/html
< Content-Length: 185
< Connection: keep-alive
< Location: http://bbs.etiantian.org/
< 
* Ignoring the response-body
* Connection #0 to host etiantian.org left intact
* Issue another request to this URL: 'http://bbs.etiantian.org/'
* About to connect() to bbs.etiantian.org port 80 (#1)
*   Trying 172.16.1.7... connected
* Connected to bbs.etiantian.org (172.16.1.7) port 80 (#1)
> GET / HTTP/1.1
> User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.21 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Host: bbs.etiantian.org
> Accept: */*
> 
< HTTP/1.1 200 OK
< Server: nginx/1.14.2
< Date: Wed, 13 Mar 2019 14:15:41 GMT
< Content-Type: text/html
< Content-Length: 18
< Last-Modified: Wed, 13 Mar 2019 11:52:54 GMT
< Connection: keep-alive
< ETag: "5c88ef16-12"
< Accept-Ranges: bytes
< 
bbs.etiantian.org
* Connection #1 to host bbs.etiantian.org left intact
* Closing connection #0
* Closing connection #1

[root@m01 ~]# curl bbs.etiantian.org
bbs.etiantian.org
[root@m01 ~]# 

1.11.4 rewrite指令实践操作三:(正确)

[root@web01 extra]# cat bbs.conf 
server {
    listen       80;
    server_name  bbs.etiantian.org bbs.org;
    if ($host ~* "^etiantian.org$") {
       rewrite ^/(.*) http://bbs.etiantian.org/$1 permanent;
    }
    root   html/bbs;
    index  index.html index.htm;
}   

1.12反向代理与负载均衡

客户端====代理服务器===web服务器   
客户端看到的服务端==代理服务器    
代理服务器====web服务器

反向代理功能架构
3台web服务器,组建出web服务器集群
web01 10.0.0.7 172.16.1.7
web02 10.0.0.8 172.16.1.8
web03 10.0.0.9 172.16.1.9
1台负载均衡服务器
lb01 10.0.0.5 172.16.1.5

①. 部署web服务器
第一个里程:安装部署nginx软件
mkdir /server/tools -p
cd /server/tools
wget http://nginx.org/download/nginx-1.12.2.tar.gz
tar xf nginx-1.12.2.tar.gz
yum install -y pcre-devel openssl-devel
useradd -M -s /sbin/nologin www
cd nginx-1.12.2
./configure --prefix=/application/nginx-1.12.2 --user=www --group=www --with-http_ssl_module --with-http_stub_status_module
make && make install
ln -s /application/nginx-1.12.2 /application/nginx
/application/nginx/sbin/nginx
netstat -lntup|grep nginx

第二个里程:编辑nginx配置文件
server {
listen 80;
server_name www.etiantian.org;
root html/www;
index index.html index.htm;
}
server {
listen 80;
server_name bbs.etiantian.org;
root html/bbs;
index index.html index.htm;
}
scp -rp /application/nginx/conf/nginx.conf 172.16.1.8:/application/nginx/conf/
scp -rp /application/nginx/conf/nginx.conf 172.16.1.8:/application/nginx/conf/

第三里程:创建模拟测试环境
mkdir /application/nginx/html/{www,bbs} -p
for name in www bbs;do echo "$(hostname) name.etiantian.org">/application/nginx/html/name/oldboy.html;done
for name in www bbs;do cat /application/nginx/html/$name/oldboy.html;done

第四里程:在负载均衡服务器上,进行测试访问
curl -H host:www.etiantian.org 10.0.0.7/oldboy.html
web01 www.etiantian.org
curl -H host:bbs.etiantian.org 10.0.0.7/oldboy.html
web01 bbs.etiantian.org
curl -H host:www.etiantian.org 10.0.0.8/oldboy.html
web02 www.etiantian.org
curl -H host:bbs.etiantian.org 10.0.0.8/oldboy.html
web02 bbs.etiantian.org
curl -H host:www.etiantian.org 10.0.0.9/oldboy.html
web03 www.etiantian.org
curl -H host:bbs.etiantian.org 10.0.0.9/oldboy.html
web03 bbs.etiantian.org

②. 部署负载均衡服务器
第一个里程:安装部署nginx软件
mkdir /server/tools -p
cd /server/tools
wget http://nginx.org/download/nginx-1.12.2.tar.gz
tar xf nginx-1.12.2.tar.gz
yum install -y pcre-devel openssl-devel
useradd -M -s /sbin/nologin www
cd nginx-1.12.2
./configure --prefix=/application/nginx-1.12.2 --user=www --group=www --with-http_ssl_module --with-http_stub_status_module
make && make install
ln -s /application/nginx-1.12.2 /application/nginx
/application/nginx/sbin/nginx
netstat -lntup|grep nginx

第二个里程:编写nginx反向代理配置文件
grep -Ev "#|^$" nginx.conf.default >nginx.conf

官方链接:http://nginx.org/en/docs/http/ngx_http_upstream_module.html#upstream
Syntax: upstream name { ... }
Default:    —
Context:    http
eg:
upstream oldboy {
server 10.0.0.7:80;
server 10.0.0.8:80;
server 10.0.0.9:80;
}
说明:upstream模块就类似定一个一个地址池或者说定一个web服务器组

官方链接:http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass
Syntax: proxy_pass URL;
Default:    —
Context:    location, if in location, limit_except
eg:
location / {
proxy_pass http://oldboy;
}
说明:proxy_pass主要用于进行抛送用户访问请求给upstream模块中的相应节点服务器

worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream oldboy {
server 10.0.0.7:80;
server 10.0.0.8:80;
server 10.0.0.9:80;
}
server {
listen 80;
server_name localhost;
root html;
index index.html index.htm;
location / {
proxy_pass http://oldboy;
}
}
}

/application/nginx/sbin/nginx -t
/application/nginx/sbin/nginx -s reload

第三个里程:进行访问负载均衡服务器测试
1)利用浏览器进行测试
进行hosts解析
http://www.etiantian.org/oldboy.html <--利用ctrl+F5刷新测试,检查是否进行负载调度
2)利用curl命令进行测试

[root@lb01 conf]# curl -H host:www.etiantian.org 10.0.0.5/oldboy.html
web01 www.etiantian.org

[root@lb01 conf]# curl -H host:www.etiantian.org 10.0.0.5/oldboy.html
web02 www.etiantian.org

[root@lb01 conf]# curl -H host:www.etiantian.org 10.0.0.5/oldboy.html
web03 www.etiantian.org

1.13Nginx反向代理负载均衡模块功能详述

ngx_http_upstream_module
模块功能说明:
1.定义后端集群web节点信息,定义一个地址池子
upstream oldboy {   
server 10.0.0.7:80; 
server 10.0.0.8:80; 
server 10.0.0.9:80; 
}   

2.实现权重值负载访问功能
upstream oldboy {
server 10.0.0.7:80 weight=3;    
server 10.0.0.8:80 weight=3;    
server 10.0.0.9:80 weight=1;    
}   

3.定义后端服务器访问的失败次数max_fails
访问失败几次后就不再进行访问
upstream oldboy {
server 10.0.0.7:80 weight=3 max_fails=3;    
server 10.0.0.8:80 weight=3 max_fails=3;    
server 10.0.0.9:80 weight=1 max_fails=3;    
}   

4.定义后端失败重试的间隔-fail_timeout
失败一次后,间隔几秒后再次发送请求
upstream oldboy {
server 10.0.0.7:80 weight=3 max_fails=3 fail_timeout=10s;   
server 10.0.0.8:80 weight=3 max_fails=3 fail_timeout=10s;   
server 10.0.0.9:80 weight=1 max_fails=3 fail_timeout=10s;   
}   

5.定义后端服务的热备节点-backup
7连接不到连接8,8连接不到才会连接9
只有在没有可用的web服务,才会用到9
upstream oldboy {
server 10.0.0.7:80 ;    
server 10.0.0.8:80 ;    
server 10.0.0.9:80 backup;  
}

1.14模块调度算法

1.轮询调度算法rr
平均分配
2.权重调度算法wrr
3.静态调度算法ip_hash
根据用户的地址信息,生成一个hash值,分配到指定的服务器。相当于之前分配到哪个服务器,再次访问还分配到哪个服务器
说明:配置ip_hash时,一定不能和backup与weight参数同时出现
upstream oldboy {
ip_hash;
server 10.0.0.7:80 ;    
server 10.0.0.8:80 ;    
server 10.0.0.9:80 ;    
}
4.最小链接数least_conn   
哪个web服务的连接数小,就分配给谁

1.15proxy_set_header

1.15.1proxy_set_header host $host;

1.nginx负载均衡配置

[root@lb01 nginx]# cat conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream oldboy {
server 10.0.0.7:80;
server 10.0.0.8:80;
server 10.0.0.9:80;
}
server {
listen 80;
server_name www.etiantian.org;
root html;
index index.html index.htm;
location / {
proxy_pass http://oldboy;
}
}
server {
listen 80;
server_name bbs.etiantian.org;
root html;
index index.html index.htm;
location / {
proxy_pass http://oldboy;
}
}
}

2.nginx-web端配置
[root@web02 nginx]# cat conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.etiantian.org;
root html/www;
index index.html index.htm;
}
server {
listen 80;
server_name bbs.etiantian.org;
root html/bbs;
index index.html index.htm;
}

}

3.现象:
访问bbs.etiantian.org找的仍然是www.etiantian.org

13.nginx_第2张图片
13.nginx_第3张图片

原因:想后端web服务器请求时,请求的是http://oldboy,但后端web服务器中没有这个地址,就找了第一位的www.etiantian.org的server
解决办法:修改http请求头部---proxy_set_header host $host;
root@lb01 nginx]# cat conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream oldboy {
server 10.0.0.7:80;
server 10.0.0.8:80;
server 10.0.0.9:80;
}
server {
listen 80;
server_name www.etiantian.org;
root html;
index index.html index.htm;
location / {
proxy_pass http://oldboy;
proxy_set_header host $host;
}
}
server {
listen 80;
server_name bbs.etiantian.org;
root html;
index index.html index.htm;
location / {
proxy_pass http://oldboy;
proxy_set_header host $host;
}
}
}

13.nginx_第4张图片
13.nginx_第5张图片

1.15.2proxy_set_header X-Forwarded-For $remote_addr;

现象:后端服务器的日志中,请求端的Ip都是负载均衡服务器的Ip,不是实际的客户端Ip
10.0.0.5 - - [14/Mar/2019:20:20:24 +0800] "GET / HTTP/1.0" 200 24 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36"

目的:想要日志中记录实际的客户端Ip
配置:
server {
listen 80;
server_name www.etiantian.org;
root html;
index index.html index.htm;
location / {
proxy_pass http://oldboy;
proxy_set_header host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
server {
listen 80;
server_name bbs.etiantian.org;
root html;
index index.html index.htm;
location / {
proxy_pass http://oldboy;
proxy_set_header host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}

现象:日志中记录真实的Ip
10.0.0.1 - - [14/Mar/2019:20:20:24 +0800] "GET / HTTP/1.0" 200 24 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36"