Nginx的配置文件结构思维导图和常见的使用场景配置示例

先看目录

一、Nginx核心配置文件结构

写的比较好的博客:https://zhuanlan.zhihu.com/p/518538798



二、 Nginx用作反向代理服务器


2.1 场景1 请求全部转发

2.1.1 场景需求

浏览器请求Nginx监听的端口,返回被代理的Tomcat的响应页面。
目前Tomcat端口8080,nginx端口80
image.png

2.1.2 修改Nginx配置

server {
  listen       80;
  server_name  localhost;

  #charset koi8-r;

  #access_log  logs/host.access.log  main;

  location / {
    proxy_pass http://localhost:8080;
    #root   html;
    #index  index.html index.htm;
  }
  ...省略
}

2.1.3 配置成功效果

image.png

2.2 需求2 按照匹配的路径转发

2.2.1 场景需求

Tomcat目前监听两个端口,对应两个Service,需求通过Tomcat代理后,从一个端口访问不同的路径直接对应这两个Tomcat。
Tomcat1 localhost:8080 对应 localhost/abc
Tomcat2 localhost:8081 对应localhost/def
image.png

2.2.2 修改Nginx配置

server {
  listen       80;
  server_name  localhost;

  #charset koi8-r;

  #access_log  logs/host.access.log  main;

  location /abc/ {
    proxy_pass http://localhost:8080/;
    #root   html;
    #index  index.html index.htm;
  }
  location /def/ {
    proxy_pass http://localhost:8081/;
    #root   html;
    #index  index.html index.htm;
  }
  ....省略
}

2.2.3 配置成功效果

image.png

2.3 nginx 的location匹配规则

location语法 location [=|~|~*|^~] /uri/ { … }
nginx常用形式

  1. 正则匹配 location ~ /lagou { }
  2. 不区分⼤⼩写的正则匹配 location ~* /lagou { }
  3. 匹配路径的前缀 location ^~ /lagou { }
  4. 精确匹配 location = /lagou { }
  5. 普通路径前缀匹配 location /lagou { }
  6. 优先级:4 > 3 > 2 > 1 > 5

2.4 location以/结尾和不以/结尾的区别

区别:如果以/结尾,则location作为绝对根路径,转发到代理服务时不会把location中匹配的路径部分代理走。如果没有/,则会把匹配的路径部分也给代理走。
举例

location /abc/ {
  proxy_pass http://localhost:8080/;
}

上述配置

  1. 如果访问 http://localhost/abc 相当于访问 http://localhost:8080/
  2. 如果访问 http://localhost/abc/docs/config/service.html 相当于访问 http://localhost:8080/docs/config/service.html
location /abc {
  proxy_pass http://localhost:8080/;
}

上述配置

  1. 如果访问 http://localhost/abc 相当于访问 http://localhost:8080/abc
  2. 如果访问 http://localhost/abc/docs/config/service.html 相当于访问 http://localhost:8080/abc/docs/config/service.html



三、Nginx用作负载均衡

3.1 负载均衡的场景举例

3.2 负载均衡配置距离

http {
  
  ...省略

  #轮询负载均衡配置
  upstream abcServer{
    server 127.0.0.1:8080;
    server 127.0.0.1:8081;
  }

  server {
    listen       80;
    server_name  localhost;
    
    location /abc/ {
      proxy_pass http://abcServer/;
    }
    
    location /def/ {
      proxy_pass http://localhost:8082/;
    }
  ...省略
  }
  
}
      

3.3 不同的负载均衡策略

3.3.1 默认策略-轮询

轮询策略:每个请求按时间顺序逐⼀分配到不同的服务器,如果某⼀个服务器下线,能⾃动剔除

  #轮询负载均衡配置
  upstream abcServer{
    server 127.0.0.1:8080;
    server 127.0.0.1:8081;
  }

3.3.2 weight 权重策略

权重策略:weight代表权重,默认每⼀个负载的服务器都为1,权重越⾼那么被分配的请求越多(⽤于服务器性能不均衡的场景)

  # 权重策略 负载均衡配置
  upstream abcServer{
    server 127.0.0.1:8080 weight=1;
    server 127.0.0.1:8081 weight=2;
  }

3.3.3 ip_hash 策略

ip_hash策略:每个请求按照ip的hash结果分配,每⼀个客户端的请求会固定分配到同⼀个⽬标服务器处理,可以解决session问题

  # 权重策略 负载均衡配置
  upstream abcServer{
    ip_hash;
    server 127.0.0.1:8080;
    server 127.0.0.1:8081;
  }



四、nginx用作静态文件服务器(动静分离)


配置文件举例

location /static/ {
	root statcFolder;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值