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;
        }
	
 }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BACKWASH2038

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值