nginx

nginx配置

location

在nginx中配置proxy_pass时,如果是按照^~匹配路径时,要注意proxy_pass后的url最后的/,当加上了/,相当于是绝对根路径,则nginx不会把location中匹配的路径部分代理走;如果没有/,则会把匹配的路径部分也给代理走。

参考链接:https://www.cnblogs.com/kevingrace/p/6804429.html

格式:location [ 空格 | = | ~ | ~* | !~ | !~* | @ ] /uri/ {}
解释:
= 表示精确匹配,如果找到,立即停止搜索并立即处理此请求。
~ 表示执行一个正则匹配,区分大小写匹配
~* 表示执行一个正则匹配,不区分大小写匹配
!~ 区分大小写不匹配
!~* 不区分大小写不匹配
^~ 即表示只匹配普通字符(空格)。使用前缀匹配,^表示“非”,即不查询正则表达式。如果匹配成功,则不再匹配其他location。
@ 指定一个命名的location,一般只用于内部重定向请求。例如 error_page, try_files
/ 表示通用匹配,任何请求都会匹配到


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 configuration 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指令时才从先前的配置级别中继承。默认情况下,只有两个字段被重新定义。

proxy_set_header Host $proxy_host;
proxy_set_header Connection close;

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”将不会被传递到代理服务器。
An unchanged “Host” request header field can be passed like this:
一个不变的“Host”请求头字段可以像这样被传递:

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:
但是,假如这个字段在客户端请求头中不存在,那么它将不会被传递。在这样的情况下最好使用$host变量-假如这个字段不存在的话,它的值等于“Host”请求头字段中的服务器名或者主服务器名。

proxy_set_header Host $host;

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_port
port of a proxied server as specified in the proxy_pass directive, or the protocol’s default 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.
在客户端的“X-Forwarded-For”请求头字段中追加了 $remote_addr 变量,用逗号分隔。 如果客户端请求标头中不存在“X-Forwarded-For”字段,则 $proxy_add_x_forwarded_for 变量等于 $remote_addr 变量。

Syntax: proxy_redirect default;
proxy_redirect off;
proxy_redirect redirect replacement;
Default:
proxy_redirect default;
Context: http, server, location

Sets the text that should be changed in the “Location” and “Refresh” header fields of a proxied server response. Suppose a proxied server returned the header field “Location: http://localhost:8000/two/some/uri/”. The directive

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

will rewrite this string to “Location: http://frontend/one/some/uri/”.

A server name may be omitted in the replacement string:

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

then the primary server’s name and port, if different from 80, will be inserted.

The default replacement specified by the default parameter uses the parameters of the location and proxy_pass directives. Hence, the two configurations below are equivalent:

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/;

The default parameter is not permitted if proxy_pass is specified using variables.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值