Nginx配置

localtion规则解释

= #表示精确匹配,优先级也是最高的
^~ #表示uri以某个常规字符串开头,理解为匹配url路径即可
~ #表示区分大小写的正则匹配
~* #表示不区分大小写的正则匹配
!~ #表示区分大小写不匹配的正则
!~* #表示不区分大小写不匹配的正则
/ #通用匹配,任何请求都会匹配到
@ #内部服务跳转

匹配优先级

  1. 精确匹配 > 匹配url路径 > 正则匹配 > 通用匹配 >@ ( = 大于 ^~ 大于 ||!|! 大于 /)
  2. = 匹配优先级最高。一旦匹配成功,则不再查找其他匹配项。
  3. location ^~ # 带参前缀匹配 。一旦匹配成功,则不再查找其他匹配项。
  4. location /a # 普通前缀匹配,优先级低于带参数前缀匹配。
  5. location / # 任何没有匹配成功的,都会匹配这里处理

location URI结尾带不带 /

  1. 如果 URI 结构是 https://domain.com/ 的形式,尾部有没有 / 都不会造成重定向。因为浏览器在发起请求的时候,默认加上了 / 。虽然很多浏览器在地址栏里也不会显示 / 。这一点,可以访问百度验证一下。

  2. 如果 URI 的结构是 https://domain.com/some-dir/ 。尾部如果缺少 / 将导致重定向。因为根据约定,URL 尾部的 / 表示目录,没有 / 表示文件。所以访问 /some-dir/ 时,服务器会自动去该目录下找对应的默认文件。如果访问 /some-dir 的话,服务器会先去找 some-dir 文件,找不到的话会将 some-dir 当成目录,重定向到 /some-dir/ ,去该目录下找默认文件。

image.png
image.png
image.png

proxy_pass代理路径替换与否

  • 配置proxy_pass时,可以实现URL路径的部分替换。
  • proxy_pass的目标地址,默认不带/,表示只代理域名(ip+端口),path和query部分不会变(把请求的path和query拼接到proxy_pass目标域名之后作为代理的URL)
  • 如果在目标地址端口后有‘/’或者‘/xx/yy’等目录,则表示把path中location匹配成功的部分剪切掉之后再拼接到proxy_pass目标地址后面比如请求 /a/b.html

结论:
1若proxy_pass代理地址端口后无任何字符,则转发后地址为:代理地址+访问的path
2若proxy_pass代理地址端口后有目录(包括"/"),则转发后地址为:代理地址+访问的path去除location匹配的路径
image.png

验证上述结果:(结果验证都正确)

nginx.conf配置

server {
listen       8081;
server_name  172.16.204.51;    

#1 proxy_pass只有ip+port,访问路径http://localhost:8081/replace1
location /replace1 {
proxy_pass http://localhost:9002; #最终路径http://localhost:9002/replace1
}
#1 proxy_pass只有ip+port,访问路径http://localhost:8081/replace1/
location /replace1/ {
proxy_pass http://localhost:9002;#最终路径http://localhost:9002/replace1/
}
#2 proxy_pass只有ip+port+/,访问路径http://localhost:8081/replace2/aaa
location /replace2 {
proxy_pass http://localhost:9002/;#最终路径http://localhost:9002//aaa
}
#2 proxy_pass只有ip+port+/,访问路径http://localhost:8081/replace22/aaa
location /replace22/ {
proxy_pass http://localhost:9002/;#最终路径http://localhost:9002/aaa
}

#3 proxy_pass只有ip+port+path,访问路径http://localhost:8081/replace3/aaa
location /replace3 {
proxy_pass http://localhost:9002/test;#最终路径http://localhost:9002/test/aaa
}
#3 proxy_pass只有ip+port+path,访问路径http://localhost:8081/replace33/aaa
location /replace33/ {
proxy_pass http://localhost:9002/test;#最终路径http://localhost:9002/testaaa
}

#4 proxy_pass只有ip+port+path+/,访问路径http://localhost:8081/replace4/aaa
location /replace4 {
proxy_pass http://localhost:9002/test2/;#最终路径http://localhost:9002/test2//aaa
}
#4 proxy_pass只有ip+port+path+/,访问路径http://localhost:8081/replace44/aaa
location /replace44/ {
proxy_pass http://localhost:9002/test2/;#最终路径http://localhost:9002/test2/aaa
}




}

后台接口9002配置

package com.example.nginx1;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@Slf4j
public class NginxController2 {
    @Value("${server.port}")
    private String port;

    //----------------------------路径替换--------------------------------------------------//
    @RequestMapping(value = "/replace1")
    @ResponseBody
    public String replace1() {
        return "http://location:9002/replace1";
    }

    @RequestMapping(value = "/replace1/")
    @ResponseBody
    public String replace1_() {
        return "http://location:9002/replace1/";
    }

    @RequestMapping(value = "/replace2/aaa")
    @ResponseBody
    public String replace2() {
        return "http://location:9002/replace2/aaa";
    }

    @RequestMapping(value = "/replace2//aaa")
    @ResponseBody
    public String replace2__() {
        return "http://location:9002/replace2//aaa";
    }

    @RequestMapping(value = "/replace22/aaa")
    @ResponseBody
    public String replace2__aa() {
        return "http://location:9002/replace22/aaa";
    }

    @RequestMapping(value = "/aaa")
    @ResponseBody
    public String aaa() {
        return "http://location:9002/aaa";
    }

    @RequestMapping(value = "/replace3/aaa")
    @ResponseBody
    public String replace3aaa() {
        return "http://location:9002/replace3/aaa";
    }

    @RequestMapping(value = "/test/aaa")
    @ResponseBody
    public String tetstaaa() {
        return "http://location:9002/test/aaa";
    }

    @RequestMapping(value = "/replace33/aaa")
    @ResponseBody
    public String replace33aaa() {
        return "http://location:9002/replace33/aaa";
    }

    @RequestMapping(value = "/testaaa")
    @ResponseBody
    public String testreplace3() {
        return "http://location:9002/testaaa";
    }

    @RequestMapping(value = "/replace4/aaa")
    @ResponseBody
    public String replace4aaa() {
        return "http://location:9002/replace4/aaa";
    }

    @RequestMapping(value = "/test2/aaa")
    @ResponseBody
    public String test2aaa() {
        return "http://location:9002/test2/aaa";
    }
    @RequestMapping(value = "/replace44aaa")
    @ResponseBody
    public String replace44aaa() {
        return "http://location:9002/replace44aaa";
    }

}

测试截图(从上到下依次测试)

image.png
image.png
image.png
image.png
image.png
image.png
image.png

image.png

配置建议

location后斜杆与proxy_pass后斜杆"/"问题,最好要么两者都加斜杆,要么都不加

测试demo

nginx.conf配置文件

#user  nobody;
worker_processes  1;
events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    gzip on;
    gzip_min_length 1k;
    gzip_comp_level 9;
    gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
    gzip_vary on;
    gzip_disable "MSIE [1-6]\.";
    
    #重点,分文件放置路径
    include  selfconfig/*.conf;
      

    }



location.conf

server {
      listen       8080;
      server_name  172.16.204.51;
		#精确匹配,内容要同表达式完全一致才匹配成功
	  location =/abc/{
			proxy_pass http://172.16.204.51:9001/nginx1;
			
        }
        #不加任何规则时,默认是大小写敏感,前缀匹配,相当于加了“~”与“^~”
      location  /abc/ {
	      #这里指定需要指向的地址
		  proxy_pass http://localhost:9001/nginx2;
      }
      #表示普通字符串匹配上以后不再进行正则匹配,以 /a/ 开头的请求,都会匹配上,注意是开头的,/a能匹配,/a/a就不能匹配,一定在ip:port之后首个匹配的
      location ^~ /a/ {
	      #这里指定需要指向的地址
		  proxy_pass http://localhost:9001/nginx3;
      }
     location  /b/ {
	      #这里指定需要指向的地址
		  proxy_pass http://localhost:9001/nginx4;
      }
     
      location  /a/b/ {
	      #这里指定需要指向的地址
		  proxy_pass http://localhost:9001/nginx5;
      }
     location  /a/b/c {
	      #这里指定需要指向的地址
		  proxy_pass http://localhost:9001/nginx6;
      }
 
 }

image.png
image.png
image.png

image.png
image.png
image.png

正则匹配

常用正则表达式

KaTeX parse error: Undefined control sequence: \w at position 109: …大小写的正则表达式匹配。 ~[\̲w̲-]+:匹配一个或多个字母、数…:分别表示字符串的开头和结尾。
\A和\Z:分别表示字符串的开始和结束位置。
s:匹配任意空白字符(包括空格、制表符、换行符等)。
\S:匹配任意非空白字符。
\d:匹配任意数字字符。
D:匹配任意非数字字符。
\w:匹配任意字母、数字、下划线或短横线字符。
\W:匹配任意非字母、数字、下划线或短横线字符。
b:匹配单词边界。
\B:匹配非单词边界。
\c:匹配ASCII码为char的字符。
C:匹配Unicode编码为U+char的字符。
\p{L}:匹配任何字母字符(包括Unicode字母)。
\p{N}:匹配任何数字字符(包括Unicode数字)。
p{P}:匹配任何标点符号字符(包括Unicode标点符号)。
\p{S}:匹配任何符号字符(包括Unicode符号)。
\p{M}:匹配任何修饰符字符(包括Unicode修饰符)。
\p{C}:匹配任何控制字符(包括Unicode控制字符)。
\p{X}:匹配任何扩展属性字符(包括Unicode扩展属性)。
-[a-z]:匹配连字符后跟一个小写字母的字符组合。
[regex]:匹配方括号内的正则表达式。
[[^]]*]:匹配方括号内的任意非方括号字符序列。
(?pattern):命名捕获组,用于提取匹配结果中的特定部分。
(?Ppattern):普通捕获组,用于提取匹配结果中的特定部分。
(?=pattern):前瞻断言,用于判断当前位置后面的内容是否符合给定的正则表达式。
(?!pattern):否定前瞻断言,用于判断当前位置后面的内容是否不符合给定的正则表达式。
(?<!pattern):负向后顾断言,用于判断当前位置前面的内容是否不符合给定的正则表达式。
(?!pattern|$):否定向前瞻断言,用于判断当前位置后面的内容是否不是以给定的正则表达式结尾或者已经到达字符串末尾。
(?<=\w):正向后顾断言,用于判断当前位置前面的内容是否是字母、数字或下划线字符。
(?<!\w):负向后顾断言,用于判断当前位置前面的内容是否不是字母、数字或下划线字符。
(?<=[a-z]):正向前置断言,用于判断当前位置前面的内容是否是小写字母字符。
(?<![a-z]):负向前置断言,用于判断当前位置前面的内容是否不是小写字母字符。
(?i):忽略大小写模式,用于忽略正则表达式的大小写差异。
(?r):递归模式,用于执行递归操作并返回整个输入字符串作为输出结果。
(?s):单行模式,用于忽略多行注释并使.元字符能够匹配除换行符以外的任何字符。
(?x):标记模式,用于启用扩展的Perl兼容性特性,例如“s”修饰符和“u”修饰符等。
+(pattern):贪婪模式,用于尽可能多地匹配给定的正则表达式。
*+(pattern):非贪婪模式,用于尽可能少地匹配给定的正则表达式

nginx.conf配置

server {
      listen       8082;
      server_name  172.16.204.51;    
   #表示当出现404错误时,Nginx会重定向到/404.txt页面。
    error_page 404 /404.txt;
    #这样用户访问产生403 的时候给用户的返回状态是200,内容是 http://example.com/forbidden.html。
    error_page 403  =200    http://example.com/forbidden.html;
    error_page 401   /1.txt;
    #可以使用^~和~*来匹配URL,并使用相应的模式进行处理
     location ~ ^/api/users  {
         ## 处理以/api/users开头的请求
         proxy_pass http://localhost:9003; #最终路径http://localhost:9003/api/users
     }
    #~*不区分大小写,$字符串结尾的
     location ~* \.(jpg|png|gif)$ {
        # 匹配以.jpg、.png或.gif结尾的文件,并返回静态文件
       proxy_pass http://localhost:9003; #最终路径http://localhost:9003/xxx.jpg
      }
    location /1.txt {
      root html;#root是根目录,如果location是文件不需要配置indext,如果location是目录还需要配置index默认文件
    }
 
    
    location /403 {
     set $ret_body '{"code": "V00006","msg": "操作太频繁了,请坐下来喝杯茶。"}';
      return 200 $ret_body;
    }
 
 }

nginx根目录文件
image.png
测试结果截图
image.png
image.png
image.png
image.png

image.png

重定向

应用场景

① 伪静态化,是将动态页面显示为静态页面方式的一种技术。理论上,搜索引擎更喜欢静态页面形式的网页,搜索引擎对静态页面的评分一般要高于动态页面。所以,URL Rewrite可以让我们网站的网页更容易被搜索引擎所收录。
  ② 提高安全性,如果在URL中暴露太多的参数,无疑会造成一定量的信息泄漏,可能会被一些黑客利用,对你的系统造成一定的破坏,所以静态化的URL地址可以给我们带来更高的安全性。
  ③ 美化URL,去除一些后缀名或参数串,让网页的地址看起来尽可能简洁明快,有利于反映访问模块内容。
  ④ 实现地址跳转、协议跳转、端口跳转。

名词解释

rewrite 指令

image.png

if 指令(判断语句)

image.png

set 指令(定义一个新的变量)

  语法:set variable_name value

return 指令(结束执行配置语句并为客户端返回状态码)

  • code:状态码,可以是204,400,402-406,408,410,411,413,416,500-504,默认返回None。

常用全局变量

image.png

root和alias

server {
      listen       8084;
      server_name  172.16.204.51;    
   #表示当出现404错误时,Nginx会重定向到/404.txt页面。
    error_page 404 /404.txt;
    

  # 用root方式location中的路径会拼加到root的地址后面
  # 请求路径为:http://localhost:8084/files/1.txt   实际访问为:html/files/1.txt
  location ~^/files/ {
    root html;
    index index.html index.htm;
  }
  # 用alias方式,location中的路径不会拼加到alias的地址后面
  # 这请求路径为http://localhost:8084/files2/1.txt    实际访问为:html/1.txt
  location ~^/files2/ {
    alias html;
    index index.html index.htm;
  }


 }

image.png

添加自定义提示

 location /cashier {
        add_header Content-Type 'text/html; charset=utf-8';
        return 200 'returnCode=600002&returnMessage=系统维护中(8:50-9:40),请稍后再试!System Maintaining(8:50-9:40), try it later!';
   }

image.png

反向代理

server {
      listen       8081;
      server_name  172.16.204.51;    
      
      #1 proxy_pass只有ip+port,访问路径http://localhost:8081/replace1
     location /replace1 {
         proxy_pass http://localhost:9002; #最终路径http://localhost:9002/replace1
     }
     #1 proxy_pass只有ip+port,访问路径http://localhost:8081/replace1/
     location /replace1/ {
        proxy_pass http://localhost:9002;#最终路径http://localhost:9002/replace1/
     }
     #2 proxy_pass只有ip+port+/,访问路径http://localhost:8081/replace2/aaa
     location /replace2 {
         proxy_pass http://localhost:9002/;#最终路径http://localhost:9002//aaa
     }
     #2 proxy_pass只有ip+port+/,访问路径http://localhost:8081/replace22/aaa
     location /replace22/ {
        proxy_pass http://localhost:9002/;#最终路径http://localhost:9002/aaa
     }
     
     #3 proxy_pass只有ip+port+path,访问路径http://localhost:8081/replace3/aaa
      location /replace3 {
         proxy_pass http://localhost:9002/test;#最终路径http://localhost:9002/test/aaa
     }
     #3 proxy_pass只有ip+port+path,访问路径http://localhost:8081/replace33/aaa
     location /replace33/ {
        proxy_pass http://localhost:9002/test;#最终路径http://localhost:9002/testaaa
     }
     
     #4 proxy_pass只有ip+port+path+/,访问路径http://localhost:8081/replace4/aaa
        location /replace4 {
         proxy_pass http://localhost:9002/test2/;#最终路径http://localhost:9002/test2//aaa
     }
     #4 proxy_pass只有ip+port+path+/,访问路径http://localhost:8081/replace44/aaa
     location /replace44/ {
        proxy_pass http://localhost:9002/test2/;#最终路径http://localhost:9002/test2/aaa
     }
     

 
 
 }

负载均衡

.

1、轮询(默认)
每个请求按时间顺序逐一分配到不同的后端服务,如果后端某台服务器死机,自动剔除故障系统,使用户访问不受影响。
2、weight(轮询权值)
weight的值越大分配到的访问概率越高,主要用于后端每台服务器性能不均衡的情况下。或者仅仅为在主从的情况下设置不同的权值,达到合理有效的地利用主机资源。
3、ip_hash
每个请求按访问IP的哈希结果分配,使来自同一个IP的访客固定访问一台后端服务器,并且可以有效解决动态网页存在的session共享问题。
4、fair
比 weight、ip_hash更加智能的负载均衡算法,fair算法可以根据页面大小和加载时间长短智能地进行负载均衡,也就是根据后端服务器的响应时间 来分配请求,响应时间短的优先分配。Nginx本身不支持fair,如果需要这种调度算法,则必须安装upstream_fair模块。
5、url_hash
按访问的URL的哈希结果来分配请求,使每个URL定向到一台后端服务器,可以进一步提高后端缓存服务器的效率。Nginx本身不支持url_hash,如果需要这种调度算法,则必须安装Nginx的hash软件包。

准备环境

1 后台启动三个服务端口分别为9001,9002,9003

在这里插入图片描述
2 nginx配置

#定义转发分配规则
upstream  myloadbalence{
   server  localhost:9001;
   server  localhost:9002;
   server  localhost:9003;


}

server {
      listen       8085;
      server_name  172.16.204.51;    
      #表示当出现404错误时,Nginx会重定向到/404.txt页面。
      error_page 404 /404.txt;
    
     location /{
        #使用自定义负载名称myloadbalence
        proxy_pass  http://myloadbalence;
     }
  
 }

测试场景

容灾问题

1正常开启三个服务

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

结论:每次都会请求不同的端口服务,负载平衡

2 关闭9003服务

在这里插入图片描述
在这里插入图片描述
发现一直loading转圈圈,nginx代理9003连接不上,其中这之间的超时等待默认是60s
查看nginx日志

在这里插入图片描述

结论:关闭9003服务,负载如果轮询到9003会超时,9001和9002不会受影响

解决方案:

配置超时时间

proxy_connect_timeout 定义与代理服务器建立连接的超时。

#定义转发分配规则
  upstream  myloadbalence{
    server  localhost:9001;
    server  localhost:9002;
    server  localhost:9003;
  }
server {
  listen       8085;
  server_name  172.16.204.51;    
  #表示当出现404错误时,Nginx会重定向到/404.txt页面。
  error_page 404 /404.txt;
  location /{
    #使用自定义负载名称myloadbalence
      proxy_pass  http://myloadbalence;
      proxy_set_header Host $http_host;
      # 添加 HTTP 响应头,以便知道负载到哪台服务器上
      add_header backendIP $upstream_addr; 
      # 响应码
      add_header backendCode $upstream_status; 
      # 服务器与被代理服务连接超时时间,代理超时
      proxy_connect_timeout 1s;
      }

 }

在这里插入图片描述
范围配置
nginx配置

#定义转发分配规则
upstream  myloadbalence{
   server  localhost:9001;
   server  localhost:9002;
   server  localhost:9003 max_fails=1 fail_timeout=300;#设置失败1次,300s内不在往这台设备转发


}

server {
      listen       8085;
      server_name  172.16.204.51;    
      #表示当出现404错误时,Nginx会重定向到/404.txt页面。
      error_page 404 /404.txt;
    
     location /{
        #使用自定义负载名称myloadbalence
         proxy_pass  http://myloadbalence;
         #通过这个指令,把客户端请求的host,转发给后端。
          proxy_set_header Host $http_host;
          # 添加 HTTP 响应头,以便知道负载到哪台服务器上
          add_header backendIP $upstream_addr; 
          # 响应码
          add_header backendCode $upstream_status; 
          #通过这个指令,把客户端的IP转发给后端服务器,在后端服务器的日志格式中,添加$http_x_real_ip即可获取原始客户端的IP了。
          proxy_set_header X-Real-IP $remote_addr;
          #默认proxy_next_upstream error timeout;,返回error timeout时,访问会继续分配到下一台服务器处理 ,会分配到9003,超时走下一个负载服务器,这里设置off,直接剔除
          #proxy_next_upstream off;
          # 服务器与被代理服务连接超时时间,代理超时
           proxy_connect_timeout 1s;
     }
  
 }

上述配置超时1s立马会负载到下一个正常的服务器中,并且300s内不会再访问9003服务器了

#定义转发分配规则
upstream  myloadbalence{
   server  localhost:9001;
   server  localhost:9002;
   server  localhost:9003 max_fails=1 fail_timeout=300;#设置失败1次,300s内不在往这台设备转发


}

server {
      listen       8085;
      server_name  172.16.204.51;    
      #表示当出现404错误时,Nginx会重定向到/404.txt页面。
      error_page 404 /404.txt;
    
     location /{
        #使用自定义负载名称myloadbalence
         proxy_pass  http://myloadbalence;
         #通过这个指令,把客户端请求的host,转发给后端。
          proxy_set_header Host $http_host;
          # 添加 HTTP 响应头,以便知道负载到哪台服务器上
          add_header backendIP $upstream_addr; 
          # 响应码
          add_header backendCode $upstream_status; 
          #通过这个指令,把客户端的IP转发给后端服务器,在后端服务器的日志格式中,添加$http_x_real_ip即可获取原始客户端的IP了。
          proxy_set_header X-Real-IP $remote_addr;
          # 服务器与被代理服务连接超时时间,代理超时
          proxy_connect_timeout 1s;
          #默认proxy_next_upstream error timeout;,返回error timeout时,访问会继续分配到下一台服务器处理 ,会分配到9003,超时走下一个负载服务器,这里设置off,不会自动分配到下一个正常的服务器
          proxy_next_upstream off;

     }
  
 }

上述配置超时不会走下一个正常服务器,直接抛出错误,错误之后300s内不会再访问9003

在这里插入图片描述

负载方式

权重
#定义转发分配规则
upstream  myloadbalence{
   server  localhost:9001 weight=10 max_fails=1 fail_timeout=300;#设置失败1次,300s内不在往这台设备转发,当前权重10,适合服务器抗压比9002、9003强的服务器
   server  localhost:9002 weight=1 max_fails=1 fail_timeout=300;#设置失败1次,300s内不在往这台设备转发
   server  localhost:9003 weight=1 max_fails=1 fail_timeout=300;#设置失败1次,300s内不在往这台设备转发
}
server {
      listen       8085;
      server_name  172.16.204.51;    
      #表示当出现404错误时,Nginx会重定向到/404.txt页面。
      error_page 404 /404.txt;
      location /{
        #使用自定义负载名称myloadbalence
         proxy_pass  http://myloadbalence;
         #通过这个指令,把客户端请求的host,转发给后端。
          proxy_set_header Host $http_host;
          # 添加 HTTP 响应头,以便知道负载到哪台服务器上
          add_header backendIP $upstream_addr; 
          # 响应码
          add_header backendCode $upstream_status; 
          #通过这个指令,把客户端的IP转发给后端服务器,在后端服务器的日志格式中,添加$http_x_real_ip即可获取原始客户端的IP了。
          proxy_set_header X-Real-IP $remote_addr;
          # 服务器与被代理服务连接超时时间,代理超时
          proxy_connect_timeout 1s;
          proxy_next_upstream error  http_404 http_502;

     }
  
 }
ip_hash
#定义转发分配规则
upstream  myloadbalence{
  #通过客户端ip进行hash,再通过hash值选择后端server
   ip hash;
   server  localhost:9001 weight=10 max_fails=1 fail_timeout=300;#设置失败1次,300s内不在往这台设备转发,当前权重10,适合服务器抗压比9002、9003强的服务器
   server  localhost:9002 weight=1 max_fails=1 fail_timeout=300;#设置失败1次,300s内不在往这台设备转发
   server  localhost:9003 weight=1 max_fails=1 fail_timeout=300;#设置失败1次,300s内不在往这台设备转发
}
server {
      listen       8085;
      server_name  172.16.204.51;    
      #表示当出现404错误时,Nginx会重定向到/404.txt页面。
      error_page 404 /404.txt;
      location /{
          #使用自定义负载名称myloadbalence
          proxy_pass  http://myloadbalence;
          #通过这个指令,把客户端请求的host,转发给后端。
          proxy_set_header Host $http_host;
          # 添加 HTTP 响应头,以便知道负载到哪台服务器上
          add_header backendIP $upstream_addr; 
          # 响应码
          add_header backendCode $upstream_status; 
          #通过这个指令,把客户端的IP转发给后端服务器,在后端服务器的日志格式中,添加$http_x_real_ip即可获取原始客户端的IP了。
          proxy_set_header X-Real-IP $remote_addr;
          # 服务器与被代理服务连接超时时间,代理超时
          proxy_connect_timeout 1s;
          #默认proxy_next_upstream error timeout;,返回error timeout时,访问会继续分配到下一台服务器处理 ,会分配到9003,超时走下一个负载服务器,这里设置off,不会自动分配到下一个正常的服务器
          proxy_next_upstream error  timeout;

     }
  
 }
url_hash
#定义转发分配规则
upstream  myloadbalence{
  #通过请求url进行hash,再通过hash值选择后端server
   hash $request_uri consistent;
   server  localhost:9001 weight=1 max_fails=1 fail_timeout=300;#设置失败1次,300s内不在往这台设备转发
   server  localhost:9002 weight=1 max_fails=1 fail_timeout=300;#设置失败1次,300s内不在往这台设备转发
   server  localhost:9003 weight=1 max_fails=1 fail_timeout=300;#设置失败1次,300s内不在往这台设备转发
}
server {
      listen       8085;
      server_name  172.16.204.51;    
      #表示当出现404错误时,Nginx会重定向到/404.txt页面。
      error_page 404 /404.txt;
      location /{
          #使用自定义负载名称myloadbalence
          proxy_pass  http://myloadbalence;
          #通过这个指令,把客户端请求的host,转发给后端。
          proxy_set_header Host $http_host;
          # 添加 HTTP 响应头,以便知道负载到哪台服务器上
          add_header backendIP $upstream_addr; 
          # 响应码
          add_header backendCode $upstream_status; 
          #通过这个指令,把客户端的IP转发给后端服务器,在后端服务器的日志格式中,添加$http_x_real_ip即可获取原始客户端的IP了。
          proxy_set_header X-Real-IP $remote_addr;
          # 服务器与被代理服务连接超时时间,代理超时
          proxy_connect_timeout 1s;
          #默认proxy_next_upstream error timeout;,返回error timeout时,访问会继续分配到下一台服务器处理 ,会分配到9003,超时走下一个负载服务器,这里设置off,不会自动分配到下一个正常的服务器
          proxy_next_upstream error  timeout;

     }
  
 }

当前请求多次请求固定在9003服务器上了
在这里插入图片描述

备用服务器
#定义转发分配规则
upstream  myloadbalence{
   server  localhost:9001 weight=1 max_fails=1 fail_timeout=300;#设置失败1次,300s内不在往这台设备转发,
   server  localhost:9002 weight=1 ;
   server  localhost:9003 weight=1 backup;#备用服务器 只有9001、9002不能提供服务的时候才会启动
}
server {
      listen       8085;
      server_name  172.16.204.51;    
      #表示当出现404错误时,Nginx会重定向到/404.txt页面。
      error_page 404 /404.txt;
      location /{
          #使用自定义负载名称myloadbalence
          proxy_pass  http://myloadbalence;
          #通过这个指令,把客户端请求的host,转发给后端。
          proxy_set_header Host $http_host;
          # 添加 HTTP 响应头,以便知道负载到哪台服务器上
          add_header backendIP $upstream_addr; 
          # 响应码
          add_header backendCode $upstream_status; 
          #通过这个指令,把客户端的IP转发给后端服务器,在后端服务器的日志格式中,添加$http_x_real_ip即可获取原始客户端的IP了。
          proxy_set_header X-Real-IP $remote_addr;
          # 服务器与被代理服务连接超时时间,代理超时
          proxy_connect_timeout 1s;
          #默认proxy_next_upstream error timeout;,返回error timeout时,访问会继续分配到下一台服务器处理 ,会分配到9003,超时走下一个负载服务器,这里设置off,不会自动分配到下一个正常的服务器
          proxy_next_upstream error  timeout;

     }
  
 }

需要注意的是,ip_hash不能与backup同时使用,另外当有服务器需要剔除,必须手动down掉
安装注意,负载需要编译,前端服务器主要配置stream和upstream,注意该模块需要在预编译时指定,没有被默认编译进nginx
#预编译
./configure --prefix=/home/hadoop/nginx --add-module=./echo-nginx-module-0.61 --with-http_stub_status_module --with-stream

动静分离

	upstream static {
		server 172.16.204.51:8086;    #设置静态访问
	 }

	upstream dynamic {
		server 172.16.204.51:8086;  #设置动态访问
	 }
   server {
      listen       8086;
      server_name  172.16.204.51;    
      #表示当出现404错误时,Nginx会重定向到/404.txt页面。
      error_page 404 /404.txt;
      
      location / {
        #会被代理到这个地址,只写一个代理,需要写全名,配置外网
        proxy_pass   http://dynamic;  #处理以.txt结尾的动态资源,这里动态负载轮询
        }

      location ~ \.txt$ {#处理以.txt结尾的动态资源,这里动态负载轮询
        root html;
	      index 1.txt;
        }
	
 }

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/114024.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

【Linux】Nignx的入门使用负载均衡动静分离(前后端项目部署)---超详细

一&#xff0c;Nignx入门 1.1 Nignx是什么 Nginx是一个高性能的开源Web服务器和反向代理服务器。它使用事件驱动的异步框架&#xff0c;可同时处理大量请求&#xff0c;支持负载均衡、反向代理、HTTP缓存等常见Web服务场景。Nginx可以作为一个前端的Web服务器&#xff0c;也可…

react条件渲染

目录 前言 1. 使用if语句 2. 使用三元表达式 3. 使用逻辑与操作符 列表渲染 最佳实践和注意事项 1. 使用合适的条件判断 2. 提取重复的逻辑 3. 使用适当的key属性 总结 前言 在React中&#xff0c;条件渲染指的是根据某个条件来决定是否渲染特定的组件或元素。这在构…

Pycharm安装jupyter和d2l

安装 jupyter: jupyter是d2l的依赖库&#xff0c;没有它就用不了d2l pycharm中端输入pip install jupyter安装若失败则&#xff1a; 若网速过慢&#xff0c;则更改镜像源再下载&#xff1a; pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/ pip …

【Kubernetes部署】二进制部署单Master Kurbernetes集群 超详细

二进制部署K8s 一、基本架构和系统初始化操作1.1 基本架构1.2 系统初始化操作 二、部署etcd集群2.1 证书签发Step1 下载证书制作工具Step2 创建k8s工作目录Step3 编写脚本并添加执行权限Step4 生成CA证书、etcd 服务器证书以及私钥 2.2 启动etcd服务Step1 上传并解压代码包Step…

第21期 | GPTSecurity周报

GPTSecurity是一个涵盖了前沿学术研究和实践经验分享的社区&#xff0c;集成了生成预训练 Transformer&#xff08;GPT&#xff09;、人工智能生成内容&#xff08;AIGC&#xff09;以及大型语言模型&#xff08;LLM&#xff09;等安全领域应用的知识。在这里&#xff0c;您可以…

tp6使用Spreadsheet报错:Class ‘PhpOffice\PhpSpreadsheet\Spreadsheet‘ not found

问题提示如下&#xff1a; 可能vendor下的 phpoffice是从别的项目拷贝过来的&#xff0c;所以咋都不行 解决办法是删掉vendor下的phpoffice&#xff0c;用composer重新下载 具体操作&#xff1a;1、在项目根目录下cmd执行下面这条命令 composer require phpoffice/phpspread…

2.4G合封芯片 XL2422,集成M0核MCU,高性能 低功耗

XL2422芯片是一款高性能低功耗的SOC集成无线收发芯片&#xff0c;集成M0核MCU&#xff0c;工作在2.400~2.483GHz世界通用ISM频段。该芯片集成了射频接收器、射频发射器、频率综合器、GFSK调制器、GFSK解调器等功能模块&#xff0c;并且支持一对多线网和带ACK的通信模式。发射输…

uniapp app端选取(上传)多种类型文件

这里仅记录本人一些遇到办法&#xff0c;后台需要file对象&#xff0c;而App端运行在jsCore内&#xff0c;并非浏览器环境&#xff0c;并没有File类&#xff0c;基本返回的都是blob路径&#xff0c;uni-file-picker得app端只支持图片和视频&#xff0c;我这边需求是音视频都要支…

Web APIs——日期对象的使用

1、日期对象 日期对象&#xff1a;用来表示时间的对象 作用&#xff1a;可以得到当前系统时间 1.1实例化 在代码中发现了new关键字时&#xff0c;一般将这个操作称为实例化 创建一个时间对象并获取时间 获得当前时间 const date new Date() <script>// 实例化 new //…

【零基础抓包】Fiddler超详细教学(一)

​Fiddler 1、什么是 Fiddler? Fiddler 是一个 HTTP 协议调试代理工具&#xff0c;它能够记录并检查所有你的电脑和互联网之间的 HTTP 通讯。Fiddler 提供了电脑端、移动端的抓包、包括 http 协议和 https 协议都可以捕获到报文并进行分析&#xff1b;可以设置断点调试、截取…

libuv进程通信与管道描述符

libuv 提供了大量的子进程管理&#xff0c;抽象了平台差异并允许使用流或命名管道与子进程进行通信。Unix 中的一个常见习惯是每个进程只做一件事&#xff0c;并且把它做好。在这种情况下&#xff0c;一个进程通常会使用多个子进程来完成任务&#xff08;类似于在 shell 中使用…

OpenCV实战——OpenCV.js介绍

OpenCV实战——OpenCV.js介绍 0. 前言1. OpenCV.js 简介2. 网页编写3. 调用 OpenCV.js 库4. 完整代码相关链接 0. 前言 本节介绍如何使用 JavaScript 通过 OpenCV 开发计算机视觉算法。在 OpenCV.js 之前&#xff0c;如果想要在 Web 上执行一些计算机视觉任务&#xff0c;必须…

【实战Flask API项目指南】之七 用JWT进行用户认证与授权

实战Flask API项目指南之 用JWT进行用户认证与授权 本系列文章将带你深入探索实战Flask API项目指南&#xff0c;通过跟随小菜的学习之旅&#xff0c;你将逐步掌握 Flask 在实际项目中的应用。让我们一起踏上这个精彩的学习之旅吧&#xff01; 前言 当小菜踏入Flask后端开发…

【卷积神经网络】YOLO 算法原理

在计算机视觉领域中&#xff0c;目标检测&#xff08;Object Detection&#xff09;是一个具有挑战性且重要的新兴研究方向。目标检测不仅要预测图片中是否包含待检测的目标&#xff0c;还需要在图片中指出它们的位置。2015 年&#xff0c;Joseph Redmon, Santosh Divvala 等人…

防火墙日志记录和分析

防火墙监控进出网络的流量&#xff0c;并保护部署防火墙的网络免受恶意流量的侵害。它是一个网络安全系统&#xff0c;它根据一些预定义的规则监控传入和传出的流量&#xff0c;它以日志的形式记录有关如何管理流量的信息&#xff0c;日志数据包含流量的源和目标 IP 地址、端口…

Everything结合内网穿透搭建在线资料库,一秒实现随时随地访问

Everythingcpolar搭建在线资料库&#xff0c;实现随时随地访问 文章目录 Everythingcpolar搭建在线资料库&#xff0c;实现随时随地访问前言1.软件安装完成后&#xff0c;打开Everything2.登录cpolar官网 设置空白数据隧道3.将空白数据隧道与本地Everything软件结合起来总结 前…

在Ubuntu上安装Redis并学习使用get、set和keys命令

目录 安装Redis切换到root用户搜索redis相关软件包安装redis修改配置文件重启服务器使用redis客户端连接服务器 get与set命令keys 安装Redis 我们在Ubuntu20.04上进行Redis的安装 切换到root用户 使用su命令&#xff1a; 在终端中&#xff0c;输入su并按回车键。然后输入roo…

C++ 输入输出流

iostream库&#xff0c;包含两个基础类型istream(输入流)和ostream(输出流)。一个流就是一个字符序列。 流 输入输出产生的字符串称为流。 被称为流的原因&#xff1a;所有的字符都在缓冲区中&#xff0c;从缓冲区拿/放都是顺序进行的&#xff0c;字符串的消耗&#xff0c;像…

恒驰服务 | 华为云数据使能专家服务offering之大数据建设

恒驰大数据服务主要针对客户在进行智能数据迁移的过程中&#xff0c;存在业务停机、数据丢失、迁移周期紧张、运维成本高等问题&#xff0c;通过为客户提供迁移调研、方案设计、迁移实施、迁移验收等服务内容&#xff0c;支撑客户实现快速稳定上云&#xff0c;有效降低时间成本…

vue封装独立组件:实现分格密码输入框/验证码输入框

目录 第一章 实现效果 第二章 核心实现思路 第三章 封装组件代码实现 第一章 实现效果 为了方便小编的父组件随便找了个页面演示的通过点击按钮&#xff0c;展示子组件密码输入的输入框通过点击子组件输入框获取焦点&#xff0c;然后输入验证码数字即可子组件的确定按钮是验…