本次以1.21.6版本的nginx作为学习插件。
一、nginx基础篇
1.Nginx开源版本安装
tar -zxcf nginx-1.21 .6 .tar.gz
cd nginx-1.21 .6 .tar.gz
./configure
报错:.configure:error:C compiler cc is not found
yum install -y gcc
./configure --prefix=/usr/local/nginx
报错:/configure: grror; the HTTp rowrite module requires the PCRE library.You can either disable the module by using --without-http-rowrite moduleoption, or install the PCRE library into the system, or build the PCRE library statically from the source with nginx by using --with -peremepath> option.
yum install -y pcre pcre-devel
报错:./configure: error: the HTTP gzip module requires the zlib 1ibrary.You can either disable the module by using --without-http-gzip-moduleoption, or insta11 the zib 1ibrary into the system, or build the z1ib library statically from the source with nginx by using --with -zlib=epath> option.
yum insta11 -y zlib zlib-devel
make && make install
cd /usr/local/nginx/sbin
./nginx
在浏览器访问我们的nginx,访问不了?防火墙没有关闭
systemctl stop firewalld && systemctl disable firewalld
firewall-cmd --zone=pubic --add-port=80 /tcp --permanent
进入安装好的目录/usr/oca1/nginx/sbin
./nginx 启动
./nginx -s stop 快速停止,快速将nginx线程杀死
./nginx -s quit 优雅美闭,在退出前完成已经接受的连接请求;不会再接受新的请求,只把原来没做的一些工作做完,然后再停止服务。
./nginx -s reload 重新加载配置。再修改完配置文件,立即生效,不重启整个nginx服务器。机制比较复杂,会把原来的执行任务的线程停掉,像quit一样,优雅的停掉,reload的时候,会重新开启一个新的线程,重新读取新的配置文件。
vi /usr/lib/systemd/system/nginx.service
[unit]
Description=nginx - web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type =forking
PIDFile=/usr/loca1/nginx/logs/nginx.pid
ExecstartPre=/usr/local/nginx/sbin/nginx -t -c /usr/loca1/nginx/conf/nginx.conf
Execstart=/usr/local/nginx/sbin/nginx -c /usr/loca/nginx/conf/nginx.conf
ExecReload=/usr/loca1/nginx/sbin/nginx -s reload
Execstop=/usr/local/nginx/sbin/nginx -s stop
ExecQuit=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
wantedBy=multi-user.target
./nginx -s stop
systemctl daemon-reload
systemctl start nginx.service
2.Nginx的基础配置
worker_processes 1 ; 默认为1 ,表示开启一个业务进程
worker_connections 1024 ;单个业务进程可接受连接数
include mime.types; 引入http mime类型
default_type application/octet-stream;如果mime类型没匹配上,默认使用二进制流的方式传输,
sendfile on; 使用linux的sendfile(socket,file,len )高效网络传输,也就是数据0 拷贝。
未开启sendfile
核心配置
worker_processes 1 ;
events {
worker connections 1024
}
http {
include mimetypes;
default type application/octet-stream;
sendfile on;
keepalive timeout 65 ,
server {
listen 80 ;
server_name localhost
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
虚拟主机配置
sendfile on/off:
sendfile on;
有用户请求打到nginx上,应用程序内存不加载磁盘文件,而是直接推送一个信号sendfile on【包括(socket,fd)】给网络接口,网络接口通过文件描述符(fd)去读取文件,直接通过网络发给用户。这里少了一次数据拷贝
sendfile off;
如果关闭sendfile,在从磁盘找到文件的时候,这就有read/write的两个过程。
read:nginx read磁盘上的文件,将文件加载到应用程序的内存里,通过应用程序的内存读完之后,再把应用程序内存当中的这份文件发给计算机操作系统的网络接口,也就是我们网卡的驱动程序。当然还会经历DMA的调度、网卡驱动程序的缓存以及内核的缓存。层层缓存,都要复制;nginx读取磁盘文件到应用程序内存缓存一份,发到网络接口缓存一份,之后发给用户。
3.虚拟主机与域名解析
虚拟主机:原本一台服务器只能对应一个站点,通过虚拟主机技术可以虚拟化成多个站点同时对外提供服务。
浏览器、Nginx与http协议
4.ServerName匹配规则
我们需要注意的是servername匹配分先后顺序,卸载前面的匹配上就不会继续往下匹配了。
1. 完整匹配
server_name vod.mmban.com vod1.mmban.com;
location / {
root /www/vod;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
2. 通配符匹配
server_name *.mmban.com;
server {
listen 80 ;
server_name www.mmban.com;
location /{
root /www/www;
index index.html index.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80 ;
server_name *.mmban.com;
location / {
root /www/vod;
index index.html index.htm;
}
}
通配符匹配首先通过虚拟主机vhost至上而下匹配。浏览器输入www.mmban.com回车,首先匹配到的是上面主机名为www.mmban.com的主机,访问文件路径为/www/www。如果输入xxx.mmban.com,则匹配到xxx.mmban.com。
3. 通配符结束匹配
www.mmban.*
server_name www.mmban.*;
4. 正则匹配
server_name ~^[0 -9 ]+\.mmban\.com$;
域名解析相关企业项目实战技术架构
多用户二级域名
短网址
httpdns
Nginx中的虚拟主机配置
多域名(多用户二级域名):
短网址:
httpdns:
5.反向代理
网关、代理与反代理
反向代理在系统架构种的应用场景
Nginx的反向代理配置
基于反向代理的负载均衡器
负载均衡策略
正、反向代理:
Nginx的反向代理配置:
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65 ;
server {
listen 80 ;
server_name localhost;
location /{
proxy_pass http://www.mmban.com
}
}
}
基于反向代理的负载均衡器:
upstream httpds {
server 192.168 .44 .102 :80 ;
server 192.168 .44 .103 :80 ;
}
server {
listen 80 ;
server_name localhost;
location / {
proxy_pass http://httpds
}
}
重启nginx服务(systemctl reload nginx),浏览器访问192.168 .44 .101 。
负载均衡策略:
1. 轮询:默认情况下使用轮询方式,逐一转发,这种方适用于无状态请求。
无法保持会话(session):由于轮流访问多个负载均衡器,比如登录一个负载均衡器,下一次访问轮询到其它负载均衡器上了,同一用户的第二次请求携带的cookie在第二台负载均衡器上没有session做验证而去保持会话。
2. 基于权重(weight)的负载均衡
指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
upstream httpds {
server 192.168 .44 .102 weight=8 ;
server 192.168 .44 .103 weight=2 ;
server 192.168 .44 .104 weight=1 ;
}
upstream httpds {
server 192.168 .44 .102 weight=8 down;
server 192.168 .44 .103 weight=2 ;
server 192.168 .44 .104 weight=1 ;
}
upstream httpds {
server 192.168 .44 .102 weight=8 down;
server 192.168 .44 .103 weight=2 ;
server 192.168 .44 .104 weight=1 backup;
}
down:表示当前的server暂时不参与负载
weight:默认为1 ,weight越大,负载的权重就越大。
backup:其它所有的非backup机器down或者忙的时候,请求backup机器。
ip_hash:根据客户端的ip地址转发同一台服务器,可以保持回话。
如果是移动客户端用户,在路途中走着走着切换了移动基站,则手机端的ip地址也跟着变,使用ip_hash方法也会导致nginx负载均衡到不同的服务器上,导致无法保持会话。所以也很少使用ip_hash的办法去做负载均衡策略来维持会话。
least_conn:最少连接访问。不常用
url_hash:根据用户访问的url定向转发请求。
fair:根据后端服务器响应时间转发请求。
如何在集群服务器上保持会话?1. session共享,使用一个redis服务,将session保存到redis服务器中,当客户端携带cookie时,服务端查找本服务器有没有对应的校验session,如果没有就去redis服务器查找,之后验证该用户,这是有状态保持会话。但是应对高并发的场景,这种方式不太适用。2. 使用token,就是说在服务器端单独设置一台签发token的服务器,客户端第一次访问服务器端,服务端将token下发给客户端,这个token是加密的,客户端下次携带此token,服务端对此token进行解密,获取客户端信息,并通过算法计算一个token与当前token做对比是否一致。一致则表示验证通过。这是无状态保持会话,服务端不保存session值。在Java中有SpringSession,Python中有JWT算法做无状态会话保持,是现在比较主流的方式。。不是使用ip_hash等其它方式做会话保持。
6.动静分离
使用动静分离的场景:一般适用于中小型网站,因为中小型网站并发量不是特别高,而且需要分离出来的静态资源不是特别多,需要把这些静态资源挪到前置的nginx服务器上。如果是大型系统的话,文件比较多,比如淘宝的买家秀,卖家秀,客户的详情页,商品图片等,这些都属于静态资源,就不适合动静分离这种简单的技术架构。动静分离适合初创企业网站的h5的内嵌到app里展示或者网站的展示,这些都没问题的。包括erp系统,传统的项目也可以使用动静分离,动静分离可以起到系统加速的作用。
动静分离原理
Nginx动静分离配置
动静分离:
Nginx动静分离配置:
https://www.bilibili.com/video/BV1yS4y1N76R/?p=28
server {
listen 80 ;
server_name localhost;
location / {
proxy_pass http://192.168 .44 .104 :8080 ;
}
location /js {
root html;
index index.html index.htm;
}
location /css {
root html;
index index.html index.htm;
}
location /img {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
}
7.location后符号的匹配规则
location ~*
location ~*/(css|js|img) {
root html;
index index.html index.htm;
}
8.URLRewrite
URLRewrite的使用场景
rewrite是实现URL重写的关键指令,根据regex(正则表达式)部分内容,重定向到replacement,结尾是flag标记。
rewrite <regex> <replacement> [flag];
关键字 正则 替代内容 flag标记
关键字:其中关键字error_log不能改变
正则:perl兼容正则表达式语句进行规则匹配
替代内容:将正则匹配的内容替换成replacement
flag标记:rewrite 支持的flag标记
rewirte参数的标签段位置;
server,location,if
flag 标记说明:
last:本条规则匹配完成后,继续向下匹配新的location URL规则
break : 本条规则匹配完成即终止,不在匹配后面的任何规则
redirect:返回302 临时重定向,浏览器地址会显示跳转后的URL地址
permanent:返回301 永久重定向,浏览器地址栏会显示跳转后的URL地址
http:192.168 .44 .101 /index.jsp?pageNum=2 改成伪静态:http://192.168 .44 .101 /2. html
配置伪静态:
server {
listen 80 ;
server_name localhost;
location / {
rewrite ^/([0 -9 ]+).html$ index.jsp?pageNum=$1
proxy_pass http://192.168 .44 .104 :8080 ;
}
location ~*/(js|img|css) {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
同时使用负载均衡
开启防火墙
systemctl start firewalld
重启防火墙
systemctl restart firewalld
重载规则
firewall-cmd --reload
查看已配置规则
firewall-cmd --list -all
指定端口和ip访问
firewall-cmd --permanent --add-rich-rule="rule family=" ipv4" source address=" 192.168 .44 .101 " port protocol=" tcp" port=" 8080 " accept"
移除规则
firewall-cmd --permanent --remove-rich-rule="rule family=" ipv4: source address="192.168.44.101" port port="8080" protocol="tcp" accept"
网关配置
upsteam httpds {
server 192.168.44.102 weight=8 down;
server 192.168.44.103:8080 weight=2;
server 192.168.44.104:8080 weight=1 backup;
}
location / {
rewrite ^/([0-9]+).html$ /index.jsp?pageNum=$1 redirect;
}
9.负载均衡+URLRewrite
网关:
应用服务器开启防火墙
配置指定端口和ip访问
在网关上配置URLRewrite
10.nginx防盗链
http协议中的referrer
nginx防盗链配置
valid_referers none | blocked | server_names | strings……;
none,检测Refer头域不存在的情况。
blocked,检测Referer头域的值被防火墙或者代理服务器删除或伪装的情况。这种情况该头域的值不以"http://" 或"https://" 开头。
server_names,设置一个或多个URL,检测Referer头域的值是否是这些URL中的某一个。
在需要防盗链的location中配置:
upstream httpds {
server 192.168 .44 .102 weight=8 down;
server 192.168 .44 .104 :8080 weight=1 backup;
}
server {
listen 80 ;
server_name localhost;
location / {
rewrite ^/([0 -9 ]+).html$ /index.jsp?pageNum=$1 break ;
proxy_pass http://httpds;
}
location ~*/(js|img|css) {
valid_referers 192.168 .44 .101 ;
valid_referers none 192.168 .44 .101 ;
if ($invalid_referer) {
return 403 ;
}
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location =/50x.html {
root html;
}
}
使用curl测试:
curl -I http://192.168 .44 .101 /img/logo.png
带引用:
curl -e "http://baidu.com" -I http://192.168 .44 .101 /img/logo.png
返回错误码
返回错误页面
整合rewrite返回报错图片
浏览器html页面中文乱码,是字符集的问题,可以在html文件中添加meta,改正乱码问题。
<head>
<meta charset='utf-8' >
</head>
upstream httpds {
server 192.168 .44 .102 weight=8 down;
server 192.168 .44 .104 :8080 weight=1 backup;
}
server {
listen 80 ;
server_name localhost;
location / {
rewrite ^/([0 -9 ]+).html$ /index.jsp?pageNum=$1 break ;
proxy_pass http://httpds;
}
location ~*/(js|img|css) {
valid_referers 192.168 .44 .101 ;
valid_referers none 192.168 .44 .101 ;
if ($invalid_referer) {
rewrite ^/ /img/x.png break ;
}
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location =/50x.html {
root html;
}
}
10.高可用配置
高可用场景及解决方案
安装keepalived
选举方式
高可用配置
安装keepalived
1. 编译安装:
下载地址:http://www.keepalived.org/download.html
如遇报错提示:
configure:error:
!!! OpenSSL is not properly installed on your system. !!!
!!! Can not include OpenSSL headers files. !!!
安装依赖:
yum install openssl-devel
2. yum安装
yum install keepalived -y
配置
使用yum安装后配置文件在:/etc/keepalived/keepalived.conf
最小配置:
第一台机器 192.168 .44 .101
!configuration File for keepalived
global defs {
router_id lb111
}
vrrp_instance VL-1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168 .44 .200
}
}
启动192.168 .44 .101 机器的keepalived,查看当前机器ip:
systemctl start keepalived
ip addr
inet 192.168 .44 .200 /32 scope global ens33
第二台机器 192.168 .44 .100
!configuration File for keepalived
global defs {
router_id lb110
}
vrrp_instance VL-1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168 .44 .200
}
}
启动192.168 .44 .100 机器的keepalived,查看当前机器ip:
systemctl start keepalived
ip addr
这里记住,使用keepalived做互为备份机器中的keepalived配置中的vrrp_instance的名称以及authentication的配置必须一致。如果对不上,无法加入到一组当中。
在windows主机的cmd中 ping 192.168 .44 .200 能通。
现在关掉192.168 .44 .101 这台机器,再次ping 192.168 .44 .200 能继续通。
查看backup机器(192.168 .44 .100 ),ip addr 发现vip已经漂移到这台机器上了。
以上两台机器上的keepalived进程互发数据包,bakcup上的keepalived进程检测MASTER上的keepalived进程是否挂掉,然后决定是否将vip漂移到自身这台机器上。实际上keepalived还没起到检测MASTER中nginx进程是否挂掉,然后做漂移。。这时就需要写一个脚本,脚本、keepalived、nginx彼此之间是独立的。脚本访问nginx进程,响应是否是正常的200 ,如果不是200 ,就kill掉MASTER中的keepalived进程,随后vip飘走了。
11.https证书配置
不安全的http协议
https原理
CA机构
证书
客户端(浏览器)
服务器端
证书自签名
在线证书申请
不安全的http协议:
https也是不安全的。。。。。。。
openssl
openssl包含:SSL协议库、应用程序以及密码算法库
自签名:OpenSSL,系统内置
图形化工具XCA:下载地址 http://www.hohnstaedt.de/xca/index.php/download
vps购买
安装环境:使用lnmp集成方式部署 https://oneinstack.com
解析域名到主机:参考视频 https://www.bilibili.com/video/BV1yS4y1N76R?p=48
证书申请:参考视频 https://www.bilibili.com/video/BV1yS4y1N76R?p=49
证书安装:
server {
listen 443 ssl;
server_name localhost;
ssl_certificate 7477542_www.upguigu.com.pem;
ssl_certificate_key 7477542_www.upguigu.com.key;
}
systemctl restart nginx
浏览器访问:https://upguigu.com
http协议跳转https
return 301 https://$server_name$request_uri;
server {
listen 80 ;
server_name www.upguigu.com upguigu.com;
access_log /data/wwwlogs/access_nginx.log combined;
return 301 https://$server_name$request_uri;
root html;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人