千家信息网

Nginx参数proxy_pass细说

发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,Syntax:proxy_pass URL;Default:-Context:location, if in location, limit_exceptSets the protocol and a
千家信息网最后更新 2026年11月01日Nginx参数proxy_pass细说

Syntax:

proxy_pass URL;

Default:

-

Context:

location, if in location, limit_except

Sets the protocol and address of a proxied(代理) server and an optional URI to which a location should be mapped. As a protocol, "http" or "https" can be specified. The address can be specified as a domain name or IP address, and an optional port:

proxy_pass http://localhost:8000/uri/;

or as a UNIX-domain socket path specified after the word "unix" and enclosed in colons:

proxy_pass http://unix:/tmp/backend.socket:/uri/;

If a domain name resolves to several addresses, all of them will be used in a round-robin fashion(流行). In addition, an address can be specified as a server group.

Parameter value can contain variables. In this case, if an address is specified as a domain name, the name is searched among the described server groups, and, if not found, is determined(确定) using a resolver(解析器).

A request URI is passed to the server as follows:

· If the proxy_pass directive(指令) is specified with a URI, then when a request is passed to the server, the part of a normalized request URI matching the location(location后匹配的路径) is replaced by a URI specified in the directive:

解释:如果在proxy_pass指令后的地址带有uri,同时location后也带有uri,这时在客户端发出请求时,proxy_pass后的uri将覆盖location后的uri向后端web服务器发出请求。

location /name/ {

proxy_pass http://127.0.0.1/remote/;

}

例如这个示例中请求过程如下:

1、客户请求 curl http://NG_IP:port/name/index.html -->2、Nginx代理服务器 http://WEB_IP:port/remote/index.html --> 3、web服务器

· If proxy_pass is specified without a URI, the request URI is passed to the server in the same form as sent by a client when the original request is processed, or the full normalized request URI is passed when processing the changed URI:

解释:如果proxy_pass没有带有uri,则使用原始的客户端URI或location后指定的uri向后端web服务器发出请求。

location /some/path/ {

proxy_pass http://127.0.0.1;

}

例如这个示例中请求过程如下:

1、客户请求 curl http://NG_IP:port/some/path/index.html --> 2、Nginx代理服务器 http://WEB_IP:port/some/path/index.html --> 3、web服务器

Before version 1.1.12, if proxy_pass is specified without a URI, the original request URI might be passed instead of the changed URI in some cases.

In some cases, the part of a request URI to be replaced cannot be determined(确定):

· When location is specified using a regular expression, and also inside(里面) named locations.

In these cases, proxy_pass should be specified without a URI.

解释:如果location使用了正则,那么proxy_pass后就不能指定uri.

· When the URI is changed inside a proxied location using the rewrite directive, and this same configuration will be used to process a request (break):

location /name/ {

rewrite /name/([^/]+) /users?name=$1 break;

proxy_pass http://127.0.0.1;

}

In this case, the URI specified in the directive is ignored and the full changed request URI is passed to the server.

解释:当在location中使用rewrite规则修改URI时,proxy_pass后的URI就会被忽略,而使用rewrite修改后的uri来作为实际的请求。

· When variables are used in proxy_pass:

· location /name/ {

· proxy_pass http://127.0.0.1$request_uri;

· }

In this case, if URI is specified in the directive, it is passed to the server as is, replacing the original request URI.

WebSocket proxying requires special configuration and is supported since version 1.3.13.

服务器 服务 客户 解释 代理 客户端 指令 示例 过程 原始 同时 地址 实际 正则 规则 路径 参数 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 手磨咖啡 中智农互联网科技 科技赋能下的互联网金融 互联网和科技创新 朗新科技与工业互联网 互联网与科技金融数据 苏州大垚互联网科技 甘肃拓世互联网科技有限公司 天速互联网络科技有限公司 互联网科技博主是什么意思 宁波青枫互联网科技有限公司 恒银科技与互联网金融 互联网科技公司有风险吗 互联网媒体平台算高科技产业 开一家科技互联网公司需要多少钱 特约风豹互联网科技为什么扣费 工业互联网山东科技大学 皮皮虾互联网科技有限公司 互联网与数字科技关系 互联网科技机房 期待互联网科技 西安互联网科技公司 安控科技rtu产品工业互联网 玖二柒互联网科技公司待遇 南平创想时代互联网科技有限公司 广州新华互联网科技学校电竞方面 互联网科技数字货币 福建互联网展示科技馆设计 瑞安市蟠桃会互联网科技有限公司 科技发展第三阶段互联网时代
0