链接:https://pan.baidu.com/s/1zSYXU6N5Ae1OpFb8zaejtA
提取码:ww5f
--来自百度网盘超级会员V5的分享
1.Nginx基本概念
(1)Nginx是什么,能做什么事情
Nginx简介:是一款高效的HTTP服务器(任何可以提供HTTP服务的软件都可以称为HTTP服务器)和反向代理WEB服务器.占有内存少,并发能力强
(2)反向代理
所谓的正向和反向是相对的
客户端 代理服务器(运行Nginx) 目标服务器
对于客户端来说代理服务器是正向的
对于目标服务器来说代理服务器是反向的
(3)负载均衡
单个服务器解决不了,我们增加服务器的数量,然后将请求分发到各个服务器上,将原先请求集中到单个服务器上的情况改为将请求分发到多个服务器上,将请求分发到不同的服务器,就是负载均衡
(4)动静分离
为了加快网站的解析速度,可以把动态页面和静态页面由不同的服务器来解析,加快解析速度.降低原来单个服务器的压力
2.Niginx安装,常用命令和配置文件
(1)在Linux系统中安装Nginx
安装必要依赖
yum -y install make automake autoconf wget pcre pcre-devel zlib zlib-devel gcc-c++ libtool openssl openssl-devel gd gd-devel

创建个文件夹用来存放
mkdir /usr/local/develop

进入目录
cd /usr/local/develop
把Nginx等等包上传 也可以使用wget http://nginx.org/download/nginx-1.19.2.tar.gz

解压nginx
tar -xvf nginx-1.19.2.tar.gz


安装git主要为了下个nginx的RTMP模块
yum install git
然后来下来
git clone https://github.com/arut/nginx-rtmp-module.git
进入Nginx解压目录
cd nginx-1.19.2
指定Nginx编译后的安装目录,后面两个是 ssl要用的 最后个是RTMP模块
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --add-module=../nginx-rtmp-module

编译并且安装
make && make install

进入Nginx的安装目录
cd /usr/local/nginx

进入执行文件目录
cd /usr/local/nginx/sbin
启动nginx
./nginx

如果是本地虚拟机 记得开放端口 后面的几个应用也需要开放端口 如果是云服务器设置安全组


现在我们要停止,重启nginx都要在 /usr/local/ngin/sbin目录下执行
一些命令(需要在nginx的sbin目录下也就是/usr/local/ngin/sbin目录下执行):
(2)Nginx常用命令
./nginx -v 查看版本号
./nginx -s stop 强制停止
./nginx -s -quit 优雅退出
./nginx -s reload 在已经启动的情况下重新加载配置文件启动 如果改了(/usr/local/ngin/conf/nginx.conf这个配置需要重启)

**配置环境变量:**方便操作nginx命令 不然每次都要在/usr/local/ngin/sbin目录下才能执行
vim /etc/profile
在最下面
export PATH=$PATH:/usr/local/nginx/sbin
让环境变量生效
source /etc/profile
现在就可以直接用nginx命令了

[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
保存后执行,将nginx服务注册为系统启动后自动启动
systemctl enable nginx
这样服务器重启后也会自动启动nginx
现在启动 停止nginx的命令有如下:
nginx -v 查看版本号
nginx -s stop 强制停止
nginx -s -quit 优雅退出
nginx 直接启动
也可以使用
systemctl start nginx 启动
systemctl reload nginx 重新加载启动
systemctl stop nginx 停止
systemctl status nginx nginx状态
干掉nginx不文明的做法

(3)Nginx配置文件
nginx的配置文件解析(/usr/local/nginx/conf/nginx.conf)
第一部分:全局块.
主要设置影响Nginx服务器整体运行的配置:配置运行nginx服务器的用户(组),允许生成的work process数(主进程设置子进程的数量一般用CPU核数),进程PID存放路径,日志存放路径和类型以及配置文件的引入等
第二部分:events块.
events块主要影响nginx服务器与用户的网络连接,常用的设置报错是否开启对多 work process下的网络连接进行序列化,是否允许同时接收多个网络连接,选取哪种事件驱动模型来处理连接请求,每个word process可以同时支持的最大连接数. word process指主进程下每一个子进程
第三部分:http块(可以包含多个server,每个server块相当于一个虚拟主机)
http全局快配置的指令包括文件引入,MIME-TYPE定义,日志自定义,连接超时事件,单链接请求数上限等
第四部分:server(一个server可以包含多个location)
最常用的配置是配置监听端口
第五部分:location
IP:端口后的字符串进行匹配
3.Nginx配置实例1-反向代理

下面用个示例演示反向代理
例如:我们需要的是访问www.xxxx.com通过nginx反向代理,让我们访问到tomcat主页面
安装Tomcat

进入压缩文件所在目录
cd /usr/local/develop
解压
tar -xvf apache-tomcat-9.0.74.tar.gz
没有安装JDK

解压JDK包
tar -xvf jdk-8u191-linux-x64.tar.gz

进入jdk目录
cd jdk1.8.0_191/
pwd记录下路径 配置环境变量

vim /etc/profile
在文件最后
export JAVA_HOME=/usr/local/develop/jdk1.8.0_191
export PATH=JAVA_HOME/bin:PATH
export CLASSPATH=.:$JAVA_HOME/lib

保存:wq
让环境变量生效
source /etc/profile

启动Tomcat
进入Tomcat的bin目录
cd /usr/local/develop/apache-tomcat-9.0.74/bin

启动
./startup.sh

进入Tomcat的日志目录
cd /usr/local/develop/apache-tomcat-9.0.74/logs
查看日志
tail -f catalina.out
可以看到在8080端口已经启动 可以按ctrl+c退出

访问你的IP加端口
可爱的雄猫

注意是http不是https
既然已经到这了 顺便说下这个页面有啥用


这个原因是因为 conf/tomcat-users.xml
vim /usr/local/develop/apache-tomcat-9.0.74/conf/tomcat-users.xml


那就添加个用户 加上所有权限
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<role rolename="manager-jmx"/>
<role rolename="manager-status"/>
<user username="admin" password="admin" roles="manager-gui,manager-script,manager-jmx,manager-status"/>

保存
进入Tomcat执行目录
cd /usr/local/develop/apache-tomcat-9.0.74/bin
停止Tomcat
./shutdown.sh
然后重新运行
./startup.sh

还是不行 如果是自己电脑上
http://localhost:8080 访问此时是没有问题的 原因在于tomcat进行了ip限制
因我这里是用AIP访问的BIP


编辑该文件
vim /usr/local/develop/apache-tomcat-9.0.74/webapps/manager/META-INF/context.xml
保存

进入Tomcat执行目录
cd /usr/local/develop/apache-tomcat-9.0.74/bin
停止Tomcat
./shutdown.sh
然后重新运行
./startup.sh
现在就可以访问了


差点回不来了
让Nginx做反向代理 就是比如访问http://www.asd.com 实际访问到Tomcat页面
如果是本地测试放开端口 还需要设置host文件进行域名和ip对应关系配置
host文件地址:
C:\Windows\System32\drivers\etc 下的hosts文件

想知道自己虚拟机IP: ifconfig
现在去配置Nginx的配置文件
vim /usr/local/nginx/conf/nginx.conf
保存
重启Nginx
注意

此时我可以安全组里关闭8080端口 也就是说访问雄猫只能通过NGINX转发,NGINX可以用作反向代理服务器,用于转发客户端请求到后端服务器

注意:云服务器安全组关闭8080是可以的 内部用nginx转发 但是如果转发的是其他服务器 那么那么服务器安全组或者防火墙需要开启该端口
Nginx配置实例2-反向代理
实现的效果:
访问http://123.321.123.1:9001/edu/ 直接跳转到123.321.123.1:8081
访问http://123.321.123.1:9001/vod/ 直接跳转到123.321.123.1:8082
把原先的Tomcat停掉
进入Tomcat执行目录
cd /usr/local/develop/apache-tomcat-9.0.74/bin
停止Tomcat
./shutdown.sh
进入develop目录
cd /usr/local/develop
创建两个文件夹分别用来启动Tomcat8081和Tomcat8080端口
mkdir tomcat8081
mkdir tomcat8082
将tomcat的压缩包分别复制到两个文件夹中
cp /usr/local/develop/apache-tomcat-9.0.74.tar.gz /usr/local/develop/tomcat8081
cp /usr/local/develop/apache-tomcat-9.0.74.tar.gz /usr/local/develop/tomcat8082
进入两文件夹分别解压
cd /usr/local/develop/tomcat8081
tar -xvf apache-tomcat-9.0.74.tar.gz
cd /usr/local/develop/tomcat8082
tar -xvf apache-tomcat-9.0.74.tar.gz
分别修改端口
vim /usr/local/develop/tomcat8081/apache-tomcat-9.0.74/conf/server.xml

vim /usr/local/develop/tomcat8082/apache-tomcat-9.0.74/conf/server.xml
分别在两边的webapp下放了不同文件

分别启动两个Tomcat
/usr/local/develop/tomcat8081/apache-tomcat-9.0.74/bin/startup.sh
/usr/local/develop/tomcat8082/apache-tomcat-9.0.74/bin/startup.sh




下面这样配置也一样

4.Nginx配置实例-负载均衡

实现效果:
浏览器地址栏输入地址 http://123.321.12.1/myapp/app.html,将请求平均的分给8081和8082端口
在两个Tomcat的webapps下新建myapp文件夹 里面都放个app.html 标记号是8081和8082
然后在nginx的配置文件做负载均衡配置
负载均衡策略:
1.轮询(默认):下面这样配置是轮询,会将请求平均分发个8081和8082
每个请求按事件顺序逐一分配到不同后端服务器

2.权重(weight):默认为1,权重越高被分配的客户请求越多.weight的值和访问比率成正比,用于后端服务器性能不均的情况 下面这样就是说访问1次8081 访问3次8082 再访问8081 一次 8082 3次

3.ip_hash
每个请求按访问的ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决会话(Session)问题

4.fair:访问8081 和 8082 看谁的耗时短.尽量将请求分发到响应时间更短的服务器上
在 Nginx 的负载均衡中,"fair" 策略通常指的是 Nginx 的第三方负载均衡模块 "ngx_http_upstream_fair",它实现了一种称为"fair"(公平)的负载均衡算法。
传统的轮询(Round Robin)负载均衡算法在处理请求时是按照服务器列表的顺序逐个分发的,但并不考虑服务器的当前负载情况。而"fair" 策略考虑了后端服务器的响应时间和负载情况,更加智能地分发请求,以保证各个服务器的负载相对均匀。
"ngx_http_upstream_fair" 模块使用了一种公平分发请求的算法,它动态地根据每个后端服务器的响应时间和当前连接数来进行负载均衡。具体来说,响应时间较短或当前连接数较少的服务器会获得更多的请求,从而实现了一种相对公平的负载均衡。
要使用 "ngx_http_upstream_fair" 模块,您需要首先安装这个模块并在 Nginx 配置中启用它。请注意,该模块是第三方模块,不是 Nginx 的官方模块。在配置文件中,

当使用 Nginx 进行负载均衡时,如果一个 Tomcat 服务器宕机,Nginx 会根据负载均衡策略自动将流量转移到其他正常工作的 Tomcat 服务器。这是负载均衡的一个主要优势,提高了系统的可用性和稳定性。
具体的行为取决于使用的负载均衡算法。常见的负载均衡算法包括轮询(Round Robin)、加权轮询、IP 哈希、最小连接数等。以下是一些常见的负载均衡策略对单个服务器故障的处理方式:
-
轮询(Round Robin): 当发生故障时,Nginx 会将请求发送到下一个存活的服务器。
-
加权轮询: 类似于轮询,但每个服务器有一个权重,故障服务器的权重会被调整或排除。
-
IP 哈希: 基于客户端 IP 地址的哈希值将请求发送到同一服务器,即使有服务器宕机,其他服务器仍然可以处理特定客户端的请求。
-
最小连接数: 将请求发送到当前连接数最少的服务器,故障服务器的连接数会被排除在选择范围之外。
-
Least Time: 将请求发送到响应时间最短的服务器。
5.Nginx配置实例-动静分离
Nginx动静分离简单来说就是把动态跟静态请求分开,不能理解成只是单纯的把动态页面和静态页面物理分开.严格意义上说应该是动态请求跟静态请求分开,可以理解成使用Nginx处理静态页面,Tomcat处理动态页面.动静分离从目前实现角度来讲大致分为两种:一种是纯粹把静态文件独立成的域名,放在独立的服务器上,也是目前主流推荐的方案,另外一种方式就是动态跟静态文件混合在一起发布,通过nginx来分开.
通过location指定不同的后缀名实现不同的请求转发.通过Expires参数设置,可以使浏览器缓存过期时间,减少与服务器之间的请求和流量,具体的Expires定义:是给一个资源设置过期时间,也就是说无需去服务端验证,直接通过浏览器自身确认是否过期即可,所以不会产生额外的流量.此种方法非常适合不经常变动的资源(如果经常跟新的文件,不建议使用Expires来缓存),这里设置了3d,表示三天之内访问这个URL,发送一个请求,比对服务器该文件最后更新时间没有变化,则不会从服务器抓取,返回状态码304,如果有修改,则直接从服务器重新下载,返回状态码200
测试
在根目录下创建data 里面 images放图片 video(视频)

通过Nginx配置访问静态资源

