scp? -r /usr/local/nginx server2:/usr/local/?? ??? ??? ?##将服务部署到server2 scp? -r /usr/local/nginx server3:/usr/local/ ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/?? ??? ?##建立软链接,全局启动nginx scp /usr/lib/systemd/system/nginx.service? server3:/usr/lib/systemd/system/nginx.service?? ??? ??? ?##将nginx进程添加系统进程 useradd -d /usr/local/nginx/ -M -s /sbin/nologin nginx?? ?##创建系统级nginx用户 systemctl daemon-reload?? ??? ??? ??? ??? ??? ??? ??? ??? ?##重新加载服务配置 systemctl enable --now nginx?? ??? ??? ??? ??? ??? ??? ?##开机自启服务 各主机的地址解析需要完善(/etc/hosts) ?
一、Nginx反向代理
正向代理和反向代理
1.1反向代理
正向代理(代理客户侧网络) 正向代理:局域网中的电脑用户想要直接访问网络是不可行的,只能通过代理服务器来访问,这种代理服务就被称为正向代理。

反向代理(代理服务侧网络) 反向代理:客户端无法感知代理,因为客户端访问网络不需要配置,只要把请求发送到反向代理服务器,由反向代理服务器去选择目标服务器获取数据,然后再返回到客户端,此时反向代理服务器和目标服务器对外就是一个服务器,暴露的是代理服务器地址,隐藏了真实服务器IP地址

1.2负载均衡
客户端发送多个请求到服务器,服务器处理请求,有一些可能要与数据库进行交互,服务器处理完毕之后,再将结果返回给客户端
普通请求和响应过程
 但是随着信息数量增长,访问量和数据量飞速增长,普通架构无法满足现在的需求
我们首先想到的是升级服务器配置,可以由于摩尔定律的日益失效,单纯从硬件提升性能已经逐渐不可取了,怎么解决这种需求呢?
我们可以增加服务器的数量,构建集群,将请求分发到各个服务器上,将原来请求集中到单个服务器的情况改为请求分发到多个服务器,也就是我们说的负载均衡
图解负载均衡
 假设有15个请求发送到代理服务器,那么由代理服务器根据服务器数量,平均分配,每个服务器处理5个请求,这个过程就叫做负载均衡
1、轮询
server1 nginx主机 server2 server3 代理服务器
server1
vim /usr/local/nginx/conf/nginx.conf
server1主机中)
http{
upstream westos { ##负载均衡器upstream
server 172.25.22.2:80;
server 172.25.22.3:80;
}
server {
listen 80;
server_name www.westos.org;
location / {
proxy_pass http://westos; ##反向代理器proxy_pass
}
}
}
nginx -t ##检测语法
nginx -s reload ##重新加载nginx
代理服务器server2,server3配置主页
echo server2 > /usr/local/nginx/html/index.html
echo server3 > /usr/local/nginx/html/index.html
nginx -s reload
在真机中测试 需要添加域名解析 (172.25.22.1 www.westos.org)

?curl www.westos.org server1作为nginx主机会将任务分发至代理服务器上

2、加权轮询
实验


?backup: 不能和ip_hash一起使用,backup 参数是指当所有非备机都宕机或者不可用的情况下,就只能使用带backup标准的备机。(备用机)

3、ip_hash
ip_hash对后端做健康检测,如果server2出问题,则调用server3 (server2 nginx -s stop) 如果后端下线,则http报错502(500表示服务器错误) 在server1主机中修改配置文件,在负载均衡模块中添加ip_hash,检测语法,重启服务。 ?

?
二、算法扩展
nginx并不支持一些算法,当我们需要使用时,则需要进行扩展。比如sticky模块,nginx本身不支持,当在配置文件中写入并调用时,会报错\
 
在配置文件中注释掉sticky;
nginx -t
nginx -s stop ##停止nginx服务
?下载sticky软件包,对软件包解压,安装解压软件并解压sticky软件包



?进入nginx目录,清理缓存,执行configure->make
cd nginx-1.20.1/
make clean ##清理缓存
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio --add-module=/root/nginx-goodies-nginx-sticky-module-ng-08a395c66e42
make
将nginx复制到sbin并覆盖,切入配置目录,编辑配置文件,取消对sticky的注释并检测语法,但是此时sticky模块会与backup冲突,所以还是检测失败
ls
cd objs/
\cp -f nginx /usr/local/nginx/sbin/ ##复制覆盖文件,\表示告诉shell不要去查alias,直接执行原本的cp
cd cd /usr/local/nginx/conf/
vim nginx.conf
nginx -t

?将之前的设定注释或删掉,重新检测语法,并开启服务


三 、nginx限流
准备实验素材

1、限制并发连接数
在真机进行压力测试,全部通过
ab -c10 -n 10 http://172.25.70.1/download/vim.jpg

limit_conn_zone 参数解释 限制连接数的能力
limit_conn_zone $binary_remote_addr
zone=NAME:10m; ##limit_conn_zone 参数配置
//第一个参数:$binary_remote_addr 表示通过remote_addr这个标识来做限制,“binary_”的目的是缩写内存占用量,是限制同一客户端ip地址。
//第二个参数:zone=addr:10m表示生成一个大小为10M,名字为addr的内存区域,用来存储访问的频次信息。
location /download/ {
limit_conn addr 1;
}
//imit_conn为限制并发连接数
 
2、限制每秒请求数(控制速率)
limit_req_zone 参数解释 限制请求处理速率能力
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s; ##limit_req_zone 参数配置
//第一个参数:$binary_remote_addr 表示通过remote_addr这个标识来做限制,“binary_”的目的是缩写内存占用量,是限制同一客户端ip地址。
//第二个参数:zone=addr:10m表示生成一个大小为10M,名字为addr的内存区域,用来存储访问的频次信息。
//第三个参数:rate 用于设置最大访问速率,rate=10r/s 表示每秒最多处理10个请求
location /download/ {
limit_conn addr 1;
limit_req zone=one;
}
?在server1主机中编辑配置文件,设定每秒只通过1个请求,重启服务
?
?在真机重新执行压力测试,因为每秒只通过一个,则其余9个被拒绝。

3、排队,超过指定数量则排队访问
burst 在超过设定的处理速率后能额外处理的请求数
location /download/ {
limit_conn addr 1;
limit_req zone=one burst=5;
}
// rate=1r/s 每秒通过一个请求.若同时有6个请求到达,Nginx 会处理第一个请求,剩余5个请求将放入队列,然后每隔1m从队列中获取一个请求进行处理。若请求数大于6,将拒绝处理多余的请求
在配置文件中设定一次访问5个,超过的排队等待,因为上一个实验设定每秒通过1个请求,则访问2次,差不多10秒。

?
?
?4、无延迟
nodelay 针对的是 burst 参数
location /download/ {
limit_conn addr 1;
limit_req zone=one burst=5 nodelay;
}
// nodelay 超过访问频次而且缓冲区也满了的时候就会直接返回503,如果没有设置,则所有请求会等待排队。
 
5、限制带宽
下载东西时,会被限制网速,其实就是限制带宽。 在配置文件中设定带宽50k,重启服务,注释掉上面实验的参数

?ab -c1 -n5 http://172.25.70.1/download/vim.jpg 文件大小为444k,访问5次,限制带宽50k,大概需要40s。

?四、nginx配置管理
1、自动索引
可以在浏览器访问,下载更方便 在配置文件中设定自动索引,注意注释上文参数设定,重启服务
 
2、expires缓存
缓存可以降低网站带宽,加速用户访问。 编辑配置文件,设定对图片等进行缓存,缓存时间为1年,在此期间访问就会减少访问时间
location ~ .*\.(gif|jpg|png)$ { ##对图片等进行缓存
expires 365d; ##缓存365天
root html; ##缓存指定html下
}
?
?curl -I 172.25.70.1/download/vim.jpg 缓存至2022年

?3、日志轮询
cd /opt/ ##第三方软件安装位置
vim nginx_log.sh
#!/bin/bash
cd /usr/local/nginx/logs/ && mv access.log access_$(date +%F -d -1day).log ##生成日至文件,date +%F -d -1day 昨天的年月日
kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
chmod +x nginx_log.sh /opt/nginx_log.sh ##运行脚本文件
给脚本执行权限,执行脚本,切入到日志目录,产生日志
chmod +x nginx.sh
./nginx.sh
cd /usr/local/nginx/logs/
ls --> access_2021-09-03.log ##生成日志
测试:

?4、禁用不必要的日志记录,以节省磁盘IO的消耗
在配置文件中设定禁用浏览器访问指定目录时生成日志文件 mkdir /usr/local/nginx/html/status
location /status {
stub_status on;
access_log off;
}

?结果:
此时在浏览器中访问该目录,在日志目录中不会生成日志记录
 在真机中使用curl命令访问该目录时,会产生日志文件 curl -I 172.25.7.1/status curl localhost/status

5、站点目录和文件的限制
在配置文件中设定指定目录只能本机访问,拒绝其他所有请求
location /status {
stub_status on;
access_log off;
allow 127.0.0.1; ##回环接口,只允许本机访问此目录
deny all; ##拒绝其他一切
}

实验测试: 真机测试:
 本机访问:

6、中文乱码
nginx默认不支持中文字符,在浏览器访问时,中文会变成乱码
server {
listen 80;
server_name localhost;
charset utf-8; ##utf-8
}
 

实验测试:

五、nginx 重定向
防止域名恶意解析到服务器IP 在 web 服务中,通常会通过重定向功能,将一个域名的请求转发到另一个域名上。
1、拒绝访问,报错500
设定在访问本机时,返回500
server {
listen 80;
server_name localhost;
return 500; ##重定向返回值为500
}

实验测试: curl localhost
 curl -I http://172.25.22.1
2、将所有访问重定向至指定域名
设定将所有访问请求重定向至指定域名
server {
listen 80;
server_name localhost;
rewrite ^(.*) http://www.westos.org permanent; ##访问本机重定向至www.westos.org
}

实验测试: curl -I http://172.25.70.1
 
3、端口重定向
生成证书,并移动到配置目录中
cd /etc/pki/tls/certs/
make cert.pem
mv /etc/pki/tls/certs/cert.pem /usr/local/nginx/conf/

?编辑配置文件,将80端口定向到443端口
server {
listen 443 ssl;
server_name www.westos.org;
ssl_certificate cert.pem;
ssl_certificate_key cert.pem;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
charset utf-8;
location / {
root html;
index index.html index.htm;
}
server {
listen 80;
server_name www.westos.org;
rewrite ^/(.*)$ https://www.westos.org/$1 permanent; ##将80端口定向443端口
location / {
proxy_pass http://westos;
}
}

实验测试:
 
4、虚拟主机重定向
1、当访问www.westos.org 重定向bbs.westos.org
server {
listen 80;
server_name www.westos.org;
#rewrite ^/(.*)$ https://www.westos.org/$1 permanent;
rewrite ^/bbs$ http://bbs.westos.org permanent;
rewrite ^/(.*)$ http://bbs.westos.org permanent;`##以任何开头或结尾都重定向到bbs.westos.org
#location / {
# proxy_pass http://westos;
#}
}
server {
listen 80;
server_name bbs.westos.org;
location / {
root /bbs;
index index.html;
}

?实验测试:

六、防盗链
盗链是指服务提供商自己不提供服务的内容,通过技术手段绕过其它有利益的最终用户界面(如广告),直接在自己的网站上向最终用户提供其它服务提供商的服务内容,骗取最终用户的浏览和点击率。可以通过设置配置防止别人盗链 server1 防盗主机 server2 盗链主机
server2
cd /usr/local/nginx/html/
vim index.html
<html>
<body>
<br>server2</br>
<img src="http://www.westos.org/download/vim.jpg">
</body>
</html>
通过访问server2可以直接访问server1的图片
server1
server {
listen 80;
server_name www.westos.org;
#rewrite ^/(.*)$ https://www.westos.org/$1 permanent;
#rewrite ^/bbs$ http://bbs.westos.org permanent;
#rewrite ^/(.*)$ http://bbs.westos.org permanent;
location / {
root html;
index index.html;
#proxy_pass http://westos;
}
location ~ \.(jpg|png)$ { ##防盗设置
valid_referers none blocked www.westos.org;
if ($invalid_referer) {
return 403;
#rewrite ^/ http://www2.westos.org/daolian.jpg;
}
}
}

?可以通过重定向让盗链指向其他资源
location ~* \.(gif|jpg|png)$ {
valid_referers none blocked www.westos.com;
if ($invalid_referer) {
#return 403;
rewrite ^/ http://www.westos.com/daolian.jpg;
}
}

?
|