Nginx

2024-01-09 00:42:35
Nginx

Nginx是一个高性能的HTTP和反向代理服务器,特点是占用内存少,并发能力强,事实上Nginx的并发能力确实在同类型的网页服务器中表现好。Nginx专为性能优化而开发,性能是其最重要的考量,实现上非常注重效率,能经受高负载的考验,有报告表明能支持高达50000个并发连接数。对静态文件(js、css、照片等)处理不是很好,一般静态文件不用nginx

正向代理

需要客户自己在浏览器配置代理服务器地址。
代理安装在客户端

反向代理

反向代理,客户端对代理是无感知的,因为客户端不需要任何配置就可以访问,我们只需要将请求发送到反向代理服务器,由反向代理服务器去选择目标服务器获取数据后,在返回给客户端,此时反向代理服务器和目标服务器对外就是一个服务器,暴露的是代理服务器地址,隐藏了真实服务器IP地址。
代理安装在服务端

负载均衡

单个服务器解决不了,我们增加服务器的数量,然后将请求分发到各个服务器上,将原先请求集中到单个服务器上的情况改为将请求分发到多个服务器上,将负载分发到不同的服务器,也就是我们说的负载均衡。

动静分离

为了加快网站的解析速度,可以把动态页面和静态页面由不同的服务器来解析,加快解析速度。降低原来单个服务器的压力。

nginx配置文件的组成

由全局块+events块+http块组成
在这里插入图片描述

http块:

这块和虚拟主机有密切关系,虚拟主机从用户角度看,和一台独立的硬件主机是完全一样的,该技术的产生是为了节省互联网服务器硬件成本。

每个http块可以包括多个server块,而每个server块就相当于一个虚拟主机。

每个server块也可以分为全局server块,以及可以同时包含多个location块。

NGINX 工作流程

NGINX 多进程, master 主进程 负责协调子进程(worker)
在这里插入图片描述
在这里插入图片描述

location 匹配优先级

语法:location [=|^||~|@] /uri/ { … }
功能: 根据URI的不同需求进行配置,可以使用字符串与正则表达式匹配。 如果要使用正则表达式,你必须指定下列前缀:
~
不区分大小写。
~ 区分大小写。
具体匹配形式如下:

  1. 正则匹配 location ~ /abc { }
  2. 不区分大小写的正则匹配 location ~* /abc { }
  3. 匹配路径的前缀 location ^~ /abc { }
  4. 精确匹配 location = /abc { }
  5. 普通路径前缀匹配 location /abc { }

优先级:

4 > 3 > 2 > 1 > 5

location = / {
# 精确匹配 / ,主机名后面不能带任何字符串
[ path A ]
}

location / {
# 因为所有的地址都以 / 开头,所以这条规则将匹配到所有请求
# 但是正则和最长字符串会优先匹配
[ path B ]
}

location /documents/ {
# 匹配任何以 /documents/ 开头的地址,匹配符合以后,还要继续往下搜索
# 只有后面的正则表达式没有匹配到时,这一条才会采用这一条
[ path C ]
}

location ~ /documents/Abc {
# 匹配任何以 /documents/ 开头的地址,匹配符合以后,还要继续往下搜索
# 只有后面的正则表达式没有匹配到时,这一条才会采用这一条
[ path CC ]
}

location ^~ /images/ {
# 匹配任何以 /images/ 开头的地址,匹配符合以后,停止往下搜索正则,采用这一条。
[ path D ]
}

location ~* .(gif|jpg|jpeg)$ {
# 匹配所有以 gif,jpg或jpeg 结尾的请求
# 然而,所有请求 /images/ 下的图片会被 path D 处理,因为 ^~ 到达不了这一条正则
[ path E ]
}

location /images/ {
# 字符匹配到 /images/,继续往下,会发现 ^~ 存在
[ path F ]
}

location /images/abc {
# 最长字符匹配到 /images/abc,继续往下,会发现 ^~ 存在
# F与G的放置顺序是没有关系的
[ path G ]
}

location ~ /images/abc/ {
# 只有去掉 path D 才有效:先最长匹配 path G 开头的地址,继续往下搜索,匹配到这一条正则,采用
[ path H ]
}

反向代理

在这里插入图片描述

负载均衡 以及会话保存

---------轮询负载均衡---------------
http {
upstream httpd {
server 192.168.0.1;
server 192.168.0.2;
}
server {
listen 80;
server_name xxxx;
location / {
proxy_pass:httpd;
}
}
}
---------权重负载均衡---------------
http {
upstream httpd {
server 192.168.0.1 weight=8;
server 192.168.0.2 weight=10;
}
server {
listen 80;
server_name xxxx;
location / {
proxy_pass:httpd;
}
}
}
---------IP_hash 会话保持负载均衡---------------
//ip_hash 算法 会保持会话, 根据请问的IP地址,就行ip_hash , 会固定访问指定的服务器。
http {
upstream httpd {
ip_hash;
server 192.168.0.1;
server 192.168.0.2;
}
server {
listen 80;
server_name xxxx;
location / {
proxy_pass:httpd;
}
}
}
---------request_uri 会话保持负载均衡---------------
在这里插入图片描述

//会保持会话, 根据请求的ui地址,执行hash算法 , 会固定访问指定的服务器。
http {
upstream httpd {
hash $request_uri;
server 192.168.0.1;
server 192.168.0.2;
}
server {
listen 80;
server_name xxxx;
location / {
proxy_pass:httpd;
}
}
}
在这里插入图片描述

静动分离

在这里插入图片描述

http {
upstream httpd {
server 192.168.0.1;
server 192.168.0.2;
}
server {
listen 80;
server_name xxxx;
location / {
proxy_pass:httpd;
}
# 匹配图片 \js Css
## js、css、img 三个文件夹,在nginx中的 html目录下
location ~ */(js|img|css)$ {
expires 8h;
root html;
index index.html index.htm
}

}

}

rewriteurl

在这里插入图片描述
http {
upstream httpd {
server 192.168.0.1;
server 192.168.0.2;
}
server {
listen 80;
server_name xxxx;
location / {
rewrite ^/([0-9]+).html$ /index.jsp?pageNum=KaTeX parse error: Expected 'EOF', got '}' at position 40: …pass:httpd; }? # 匹配img 、j… {
expires 8h;
root html;
index index.html index.htm
}

}

}

防盗链

http {
upstream httpd {
server 192.168.0.1;
server 192.168.0.2;
}
server {
listen 80;
server_name xxxx;
location / {
rewrite ^/([0-9]+).html$ /index.jsp?pageNum=KaTeX parse error: Expected 'EOF', got '}' at position 40: …pass:httpd; }? # 匹配img 、j… {
valid_referer 192.168.1.101;
if($invalid_referer)
{
return 401;
}
root html;
index index.html index.htm
}
error_page 500 502 503 504 /50x.html;
location=/50x.html {

    		root html
    	
    }
    #  html 文件夹中要有401.html  文件
    error_page 401 /401.html;
    location=/401.html {
    	root html;
    }
    
}

}

keepalived

文章来源:https://blog.csdn.net/u013400314/article/details/135361193
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。