nginx负载均衡 proxy_set_header的简单配置

场景

  • nginx负载均衡

参考文档

用法

语法

Syntax: proxy_set_header field value;
Default:
proxy_set_header Host $proxy_host;
proxy_set_header Connection close;
Context: http, server, location

简介

Allows redefining or appending fields to the request header passed to the proxied server. The value can contain text, variables, and their combinations. These directives are inherited from the previous level if and only if there are no proxy_set_header directives defined on the current level. By default, only two fields are redefined:
proxy_set_header Host $proxy_host; proxy_set_header Connection close;

允许重新定义或者追加传递到proxy server的header ; value可以是text 或者 变量,或者是他们的组合体,如果没有定义 则从父级继承, 下面两个字段是默认定义的 Host, Connection;

详细

If caching is enabled, the header fields “If-Modified-Since”, “If-Unmodified-Since”, “If-None-Match”, “If-Match”, “Range”, and “If-Range” from the original request are not passed to the proxied server.

如果缓存 则“If-Modified-Since”, “If-Unmodified-Since”, “If-None-Match”, “If-Match”, “Range”, and “If-Range” 不能传递到proxy server

An unchanged “Host” request header field can be passed like this: proxy_set_header Host $http_host;
However, if this field is not present in a client request header then nothing will be passed. In such a case it is better to use the $host variable - its value equals the server name in the “Host” request header field or the primary server name if this field is not present:

proxy_set_header Host如果设置成$http_host,则proxy server $_SERVER['HTTP_HOST'] 是请求的client request $host,; 如果是默认的$proxy_host 则proxy server $_SERVER['HTTP_HOST'] 则是proxy_pass的设置的host; 如果客户端请求的header中没有Host字段,则建议使用$host字段

If the value of a header field is an empty string then this field will not be passed to a proxied server:
proxy_set_header Accept-Encoding “”;

如果header字段值是空字符串 则不会传递到proxy server

Embedded Variables(嵌入的变量)

简洁

The ngx_http_proxy_module module supports embedded variables that can be used to compose headers using the proxy_set_header directive:

ngx_http_proxy_module模块内置一些变量,它们可以在proxy_set_header的指令中用到

$proxy_host
name and port of a proxied server as specified in the proxy_pass directive;

$proxy_host 是proxy_pass指定的name &&port

$proxy_port
port of a proxied server as specified in the proxy_pass directive, or the protocol’s default port;

$proxy_port是proxy_pass指定的port 或者是协议默认的port

$proxy_add_x_forwarded_for
the “X-Forwarded-For” client request header field with the $remote_addr variable appended to it, separated by a comma. If the “X-Forwarded-For” field is not present in the client request header, the $proxy_add_x_forwarded_for variable is equal to the $remote_addr variable.

$proxy_add_x_forwarded_for是客户端X-Forwarded-For的值, 如果客户端header中没有设置, 则¥$proxy_add_x_forwarded_for 等于$remote_addr

We use proxy_set_header directives to pass relevant information to the WAP servers, and also so we can capture it in the logs:

The X-Real-IP header contains the source (client’s) IP address as captured in the $remote_addr variable.

The X-Forwarded-For conveys that header from the client request, with the client’s IP address appended to it (or just that address if the client request doesn’t have the header).

X-Real-IP包含客户端原始的IP,可以被$remote_addr捕获,

X-Forwarded-For 代表客户端header中标记的IP, 它可以被$proxy_add_x_forward_for捕获, 如果header没有X-Forwarded-For 则它和$remoter_addr是相同的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值