nginx 负载均衡和反向代理的基本配置

负载均衡

  • 此处指的是选择一种策略,尽量把请求均匀的分布到每一个上游服务器

upstream

  • 配置块 http
  • 语法 upstream name{}
  • 定义一个上游服务器的集群,便于反向代理的proxy_pass使用
    upstream backend {
        server 127.0.0.1:5566 weight=1 max_fails=2 fail_timeout=30s;
    }

    server {
        location / {
            proxy_pass http://backend; #backend指上游服务器
        }
    }

server
  • 配置块 upstream
  • 语法 server names[parameters]
  • 指定一台上游服务器的名字 可以是域名,ip,UNIX句柄
parameters 定义
weight=number 上游服务器转发的权重 默认1
max_fails=number 在fail_timeout时间内,如果向上游服务器转发失败次数超过number,就认为该服务器在此时间段里不可用,默认1 设为0就不检查失败次数
fail_timeout=time 用于优化反向代理功能,与连接、读取、响应超时无关,默认10秒
down 永久下线服务器,只在使用ip_hash有效
backup 在使用ip_hash时无效 表示上游服务器只是备份服务器,只有在所有非备份上游服务器失效后,才会指向备份
ip_hash
  • 配置块 upstream
  • 语法 ip_hash;
  • 将单个用户的请求固定到某个上游服务器

先根据客户端的ip地址计算一个key,将可以按照upstream集群的上游服务器数量进行取模,再根据取模的结果把请求地址转发到相应的上游服务器,不能与weight同时使用,在标识上游服务器不可用时,要用down不能直接删除,确保转发策略一贯性

    upstream backend {
        ip_hash;
        server 127.0.0.1:5566 weight=1 max_fails=2 fail_timeout=30s;
        server backend down;
    }

记录日志时支持的变量

变量名 定义
$upstream_addr 处理请求的上游服务器地址
$upstream_cache_status 表示是否命中缓存,取值范围:MISS、EXPIRED、UPDATING、SATLE、HIT
$upstream_status 上游服务器返回的响应中HTTP响应码
$upstream_response_time 上游服务器响应时间,精度毫秒
$upstream_http_$HEADER http头部,如upstream_http_host
log_format timing '$remote_addr - $remote_user [$time_local] $request '
    'upstream_response_time $upstream_response_time '
    'msec $msec request_time $request_time';

log_format up_head '$remote_addr - $remote_user [$time_local] $request '
    'upstream_http_content_type $upstream_http_content_type'

反向代理配置

proxy_pass

  • 语法:proxy_pass URL;
  • 配置块 location、if

将当前请求反向代理到URL参数指定的服务器上,URL可以是主机名或ip地址

proxy_pass http://localhost:3000/uri/;

可以加上负载均衡 使用upstream
upstream backend {...}
server {location / {
            proxy_pass http://backend; #backend指上游服务器
        }}

可以把httpz转换成https
proxy_pass https://192.168.0.1/;

若需要转发host头部
proxy_set_header Host $host;

proxy_method

  • 语法:proxy_method method;
  • 配置块 http、server、location

转发时的协议方法名
proxy_method POST; 客户端的GET请求也会被转发成POST

proxy_hide_header

  • 语法:proxy_hide_header the_header;
  • 配置块 http、server、location

Nginx会将上游服务器的响应转发给客户端,但默认不会转发以下HTTP头部字段:Date、Server、X-Pad和X-Accel-*.使用proxy_hide_header可以指定哪些不能转发

proxy_hide_header Cache-Control;

proxy_pass_header

  • 语法:proxy_pass_header the_header;
  • 配置块 http、server、location

将原来禁止转发的header设为转发

proxy_pass_request_body

  • 语法:proxy_pass_request_body on|off;
  • 默认 on;
  • 配置块 http、server、location
    是否向上游转发http body

proxy_pass_request_headers

  • 语法:proxy_pass_request_headers on|off;
  • 默认 on;
  • 配置块 http、server、location
    是否向上游转发http header

prxoy_redirect

  • 语法:prxoy_redirect [default|off|redirect rep|replacement]];
  • 默认 default;
  • 配置块 http、server、location

当上游返回响应是重定向或刷新请求(301,302),proxy_redirect 可以重设HTTP头部的location或refresh字段。

proxy_redirect http://localhost:8000/two/ http://frontend/one/;

对location字段的URI是http://localhost:8000/two/some/uri.实际转发给客户端的是http://frontend/one/;

可以使用ngx-http-core-module提供的变量来设置

proxy_redirect http://localhost:8000/ http://$host:$server_port/;

可以省略replacement参数的主机名部分,这时会用虚拟主机名称填充
proxy_redirect http://localhost:8000/two/ /one/;

使用default参数时,会按照proxy_pass配置项和所属的location配置项重组

location /one/ {
  proxy_pass http://upstream:port/two/;
  proxy_redirect default;
}
等于
location /one/ {
    proxy_pass http://upstream:port/two/;
    proxy_redirect http://upstream:port/two/ /one/;
}

proxy_next_upstream

  • 语法:proxy_next_upstream [error|timeout|invalid_header|http_500|http_502|http_503|http_504|http_404|off];
  • 默认 error timeout;
  • 配置块 http、server、location

当向一台上游转发请求出错时,继续换一台处理
(invaild_header 上游响应不合法)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 上一篇《WEB请求处理一:浏览器请求发起处理》,我们讲述了浏览器端请求发起过程,通过DNS域名解析服务器IP,并建...
    七寸知架构阅读 81,220评论 21 356
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,923评论 18 139
  • Nginx简介 解决基于进程模型产生的C10K问题,请求时即使无状态连接如web服务都无法达到并发响应量级一万的现...
    魏镇坪阅读 2,079评论 0 9
  • Nginx的配置文件nginx.conf配置详解如下: user nginx nginx ; Nginx用户及组:...
    IT码农工阅读 859评论 0 0
  • 帮儿子钩了一条江湖传说中的“三万三”围巾,比实际图解缩小和缩短一些。绿色线不够干脆两头换成白色线搭配,看起来也蛮清...
    往事如烟花阅读 3,727评论 3 4