一、什么是Nginx
Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务
二、Nginx定制化安装
1、准备Nginx的源码
2、解压,开始安装
2.1关闭debug
vim auto/cc/gcc
2.2在 server1 安装模块
1。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-threads --with-file-aio --with-http_ssl_module
首次执行编译报错,没有安C语言编译器
yum install -y gcc`
2。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-threads --with-file-aio --with-http_ssl_module
yum install -y pcre-devel
3。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-threads --with-file-aio --with-http_ssl_module
yum install -y openssl-devel
模块安装成功
编译
安装部署
安装成功
三、在server1创建一个nginx用户
useradd -d /usr/local/nginx/ -M -s /sbin/nologin nginx
四、将服务部署到server2与server3
scp -r /usr/local/nginx server2:/usr/local/
scp -r /usr/local/nginx server3:/usr/local/
五、建立软链接以便全局使用nginx,编辑配置文件
Nginx实验
一、并发优化
ulimit -a 可以查看用户可以打开文件的限制 ##open files
sysctl -a | grep file ##打开文件的限制
///fs.file-max = 183593
free -m
二、nginx反向代理+均衡负载
1、默认轮询
cd /usr/local/nginx/conf/
vim nginx.conf ##编辑配置文件
http {
upstream westos1 { ##负载均衡器upstream
server 172.25.73.2:80;
server 172.25.73.3:80;
}
server {
listen 80;
server_name www.westos.com;
location / {
proxy_pass http://westos1; ##反向代理器proxy_pass
}
}
}##注意此处http的后括号
nginx -t
nginx -s reload
在server1上完成 反向代理+均衡负载 ,检测语法,重新启动服务
在真机做域名的地址解析,此时在真机访问www.westos1.com,实现server2与server3的轮询
2、改变默认权重,默认为1
此时在真机访问server2服务器的轮询变多
3、ip_hash
ip_hash对后端做健康检测,如果server2出问题,则调用server3
(server2 nginx -s stop)
如果后端下线,则http报错502(500表示服务器错误)
在server1主机中修改配置文件,在负载均衡模块中添加ip_hash,检测语法,重启服务。
4、备用机
当所有后端主机都出现问题时,备用机可以暂时运行,但不是长久之计,需要及时修理后端服务器。
在server1主机中设定负载均衡中将本机作为备用机,检测语法,重启服务。同时编辑发布文件。
关闭server2与server3的Nginx服务
真机访问502错误,当后端主机恢复,不会访问备用机
三、平滑升级与平滑回退
1、平滑升级
查看一下当前使用的版本
2、下载新版本:http://nginx.org/en/download.html
然后:解压 > 编译前的准备 > 编译
3、将新版本的nginx.conf配置修改到旧版本的配置
4、make之后备份旧程序,并拷贝新程序
5、获取当前nginx主进程pid,即master进程。开启新版本。关闭worker进程但保留主进程master
ps ax | grep nginx ##获取当前nginx主进程pid,即master进程
kill -USR2 PID ##开启新版本
ps ax | grep nginx
curl localhost -I ##显示新版本号
kill -WINCH PID ##关闭worker进程但保留主进程
6、有时候我们会发现新版本并没有旧版本用着舒服,那么关闭worker进程但保留主进程就是为了回退,即就是关闭工作端worker,保留master
2、版本回退
回退的过程是相反的,先还原nginx程序,唤醒原进程,回收新版本,并且关闭
cd /usr/local/nginx/sbin/
ls
\cp -f nginx.old nginx ##还原nginx程序
kill -HUP PID ##唤醒原进程
ps ax | grep nginx
kill -WINCH PID ##回收新版本的worker进程
kill -QUIT PID ##关闭新版本主进程
ps ax | grep nginx
curl localhost -I
四、算法扩展
nginx并不支持一些算法,当我们需要使用时,则需要进行扩展。比如sticky模块,nginx本身不支持,当在配置文件中写入并调用时,会报错
1、所以要对nginx进行扩展,先将sticky注释掉,并停止服务。
在配置文件中注释掉sticky;
nginx -t
nginx -s stop
2、下载sticky软件包,对软件包解压,安装解压软件并解压sticky软件包
3、进入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
4、将nginx复制到sbin并覆盖,切入配置目录,编辑配置文件,取消对sticky的注释并检测语法,但是此时sticky模块会与backup冲突,所以还是检测失败
ls
cd objs/
\cp -f nginx /usr/local/nginx/sbin/ ##复制覆盖文件,\表示告诉shell不要去查alias,直接执行原本的cp
vim nginx.conf
nginx -t
5、将之前的设定注释或删掉,重新检测语法,并开启服务
五 、nginx限流
准备实验素材
1、限制并发连接数
1、在真机进行压力测试,全部通过
ab -c10 -n 10 http://172.25.73.1/download/vim.jpg ##并发用户数为10
2、在server1主机中设定访问下载链接时,受到控制,重启服务
3、重新在真机执行压力测试,因为被限流,所以有部分请求被拒绝。
2、限制每秒请求数
1、在真机压力测试
###真机
ab -c1 -n 10 http://172.25.24.1/download/vim.jpg ##全部通过
2、在server1主机中编辑配置文件,设定每秒只通过1个请求,重启服务
3、在真机重新执行压力测试,因为每秒只通过一个,则其余9个被拒绝。
3、排队,超过指定数量则排队访问
1、在配置文件中设定一次访问5个,超过的排队等待,因为上一个实验设定每秒通过1个请求,则访问2次,差不多10秒。
2、在真机重新执行压力测试
4、无延迟
编辑配置文件,设定请求无延迟,在上一个实验的情况下,只能执行1次,即5个请求,重启服务。
vim nginx.conf
///
location /download/ {
limit_conn addr 1;
limit_req zone=one burst=5 nodelay;
///
nginx -s reload
在真机中,执行压力测试,只能通过5个,其余被拒绝
###真机
ab -c1 -n 10 http://172.25.24.1/download/vim.jpg
5、限制带宽
现实生活中,我们某盘下载东西时,会被限制网速,其实就是限制带宽。
在配置文件中设定带宽50k,重启服务,注释掉上面实验的参数
在真机中执行压力测试,文件大小为444k,访问10次,限制带宽50k,大概需要80s。
六、nginx配置管理
1、自动索引
可以在浏览器访问,下载更方便
在配置文件中设定自动索引,注意注释上文参数设定,重启服务
vim nginx.conf
///
location /download/ {
limit_conn addr 1;
#limit_req zone=one burst=5 nodelay;
#limit_rate 50k; ##注释
autoindex on;
///
nginx -s reload
浏览器访问
2、nginx expire 缓存配置
缓存可以降低网站带宽,加速用户访问。
编辑配置文件,设定对图片等进行缓存,缓存时间为1年,在此期间访问就会减少访问时间
vim nginx.conf
///
location /download/ {
limit_conn addr 1;
#limit_req zone=one burst=5 nodelay;
#limit_rate 50k;
autoindex on;
} ##在此位置下方进行设定
location ~ .*\.(gif|jpg|png)$ { ##对图片等进行缓存
expires 365d;
root html;
}
///
nginx -s reload
在真机中,使用curl命令访问素材文件,可以看到缓存至2022年。
3、日志轮询
cd /opt/ ##第三方软件安装位置
vim nginx.sh
///
#!/bin/bash
cd /usr/local/nginx/logs && mv access.log access_$(date +%F -d -1day).log
kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
///
给脚本执行权限,执行脚本,切入到日志目录,产生日志
chmod +x nginx.sh
./nginx.sh
cd /usr/local/nginx/logs/
ls --> access_2021-09-03.log ##生成日志
4、禁用不必要的日志记录,以节省磁盘IO的消耗
在配置文件中设定禁用浏览器访问指定目录时生成日志文件
cd ..(nginx)
cd conf/
vim nginx.conf
///
location ~ .*\.(gif|jpg|png)$ {
expires 365d;
root html;
} ##在这个位置的下面加入设定
location /status {
stub_status on;
access_log off;
}
///
nginx -t
nginx -s reload
此时在浏览器中访问该目录,在日志目录中不会生成日志记录
在浏览器访问
172.25.24.1/status ##刷新会增加访问次数,但不会有日志生成
cd ../logs
ls
cat access.log ##不再有新的访问日志
5、站点目录和文件的限制
在配置文件中设定指定目录只能本机访问,拒绝其他所有请求
cd conf/
vim nginx.conf
///
location /status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
///
nginx -t
nginx -s reload
当在真机访问status目录时,拒绝访问,报错403,资源不可用,服务器理解客户的请求,但拒绝处理
当在server1本机访问时,允许访问
6、中文乱码
nginx默认不支持中文字符,在浏览器访问时,中文会变成乱码,在nginx发布文件中加入一行中文,在浏览器中试访问为乱码
但是在server1本机中,可以使用curl命令查看本地的发布文件,是可以正常显示中文的,编辑配置文件,设定nginx支持中文字符,并重启服务
vim nginx.conf
///
server {
listen 80;
server_name localhost;
charset utf-8;
///
nginx -s reload
如果刷新无效,请清除浏览器缓存,在试
七、nginx 重定向
1、防止域名恶意解析到服务器IP
1、拒绝访问,报错500
切入配置目录,编辑配置文件,设定在访问本机时,返回500,重启服务。此时使用curl命令访问本机,会显示http报错500。
cd /usr/local/nginx/conf/
vim nginx.conf
///
server {
listen 80;
server_name localhost;
return 500;
///
nginx -s reload
curl -I localhost ##HTTP/1.1 500 Internal Server Error
2、将所有访问重定向至指定域名
编辑配置文件,设定将所有访问请求重定向至指定域名,重启服务。此时使用curl命令访问本机,会显示访问地址为http://www.LINUX.com
vim nginx.conf
///
server {
listen 80;
server_name localhost;
rewrite ^(.*) http://www.LINUX.com permanent;
///
nginx -s reload
curl -I localhost ##Location: http://www.LINUX.com
浏览器输入ip 172.25.73.1 自动定向到 www.LINUX.com
2、端口重定向
由于443是 https 使用,所以先制作一个加密证书,并移动到配置目录中,编辑配置文件,将80端口定向到443端口,检测语法,重启服务,查询端口
cd /etc/pki/tls/certs
make cert.pem ##生成证书
mv cert.pem /usr/local/nginx/conf
vim nginx.conf
///
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;
location / {
root html;
index index.html index.htm;
}
}
server {
listen 80;
server_name www.westos.org;
rewrite ^/(.*)$ https://www.westos.org/$1 permanent;
#location / {
# proxy_pass http://westos;
# }
}
///
nginx -t
nginx -s reload
netstat -antlup | grep 443
在浏览器输入172.25.73.1 自动 https 访问www.westos.com
3、虚拟主机重定向
1、www.westos.org 重定向bbs.westos.org
cd ..(nginx)
cd html
mkdir bbs
vim index.html
echo bbs.wetos.com > index.html
mv bbs/ / #移动到根目录
vim nginx.conf
///
server {
listen 80;
server_name www.westos.com;
#rewrite ^/(.*)$ https://www.westos.com/$1 permanent;
rewrite ^/bbs$ http://bbs.westos.com permanent;
rewrite ^/(.*)$ http://bbs.westos.com/$1 permanent;
#location / {
# proxy_pass http://westos;
# }
}
server {
listen 80;
server_name bbs.westos.com;
location / {
root /bbs;
index index.html;
}
}
///
nginx -s reload
在真机使用curl命令可以查看、浏览器输入www.westos.org会自动跳转为bbs.westos.org
2、bbs.westos.org 重定向www.westos.org
vim nginx.conf
///
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/$1 permanent;
if ($host = "bbs.westos.org") {
rewrite ^/(.*)$ http://www.westos.org/bbs/$1 permanent;
}
#server { ##全部注释
# listen 80;
# server_name bbs.westos.org;
#
# location / {
# root /bbs;
# index index.html;
# }
# }
///
nginx -s reload
在真机先做地址解析,使用curl命令可以查看、在浏览器中输入bbs.westos.com会自动跳转至www.westos.com/bbs
八、防盗链
对于图片来说,A网站,如果想使用B网站的图片,可以直接写上B网站图片的链接地址,或者将B网站的图片通过右键另存为的方式下载到本地,然后在页面上使用。如果B网站不想A网站这么干了,那么B网站可以采取防盗链的措施来干这个工作,结果就是,A网站想请求所需要的资源,通过url的方式,获取的可能不是原来的图片了,出现404或者别的图片替代了。如果通过浏览器直接打开图片url,那么仍然有可能显示404,这就是防盗链。
盗链主机server2
防盗主机server1
在我们的server1当中/usr/local/nginx/html下建立一个目录download
目录内下载一张图片,盗链这张图片
1、配置server2盗链主机
cd /usr/local/nginx/html
vim index.html
\\\
<html>
<body>
<br>server2</br>
<img src="http://www.westos.com/download/vim.jpg">
</body>
</html>
\\\
2、此时通过浏览器访问172.25.73.2可以访问到server1的图 片
3、在server1 加入防盗链,重新启动nginx
location ~* \.(gif|jpg|png)$ {
valid_referers none blocked www.westos.com;
if ($invalid_referer) {
return 403;
}
}
4、此时不能通过server2的服务访问到server1的资源
5、也可以通过重定向让盗链指向其他资源
location ~* \.(gif|jpg|png)$ {
valid_referers none blocked www.westos.com;
if ($invalid_referer) {
#return 403;
rewrite ^/ http://www.westos.com/daolian.jpg;
}
}