> For the complete documentation index, see [llms.txt](https://yangsx95.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://yangsx95.gitbook.io/notes/distributed/web-fu-wu-qi/nginx/dai-li-fu-wu.md).

# 代理服务

Nginx可以作为多种代理服务：

![1556094884828](/files/gmcDulamhwFmKvApbudo)

## proxy\_pass

```nginx
# 进入到location后，发现此配置，将会把请求转发给指定的URL，再由nginx接收响应，再响应给客户端
Syntax: proxy_pass URL;
Default:
Context: location, if in location, limit_except

# 支持的URL：
# http://localhost:8080/uri/
# https://test.com/uri/
# http://unix:/tmp/backend.socket:/uri/
```

## 反向代理

反向代理，比如nginx代理内网中的某个tomcat，代理的是服务端

```nginx
# 将admin.html的请求反向代理交给服务器的tomcat处理(8080是tomcat端口)
location ~ /admin.html$ {
    proxy_pass http://127.0.0.1:8080;
}
```

## 正向代理

正向代理，比如VPN，代理的是客户端

```nginx
# 定义DNS server，这是google的dns server
resolver 8.8.8.8;
# 请求这台代理服务器，这台服务器会代替我们请求，并响应
location / {
    proxy_pass http://$http_host$request_uri;
}
```

可以通过代理软件，设置代理。

## http\_proxy\_module

`http_proxy_module`是代理模块，包含了很多代理指令，详细可以了解[官方文档](http://nginx.org/en/docs/http/ngx_http_proxy_module.html)。下面是需要了解的常用配置：

```nginx
#### 信息修改

# 重写客户端请求的请求头
Syntax:	proxy_set_header field value;
Default:	
proxy_set_header Host $proxy_host;
proxy_set_header Connection close;
Context:	http, server, location

# 重写客户端请求的请求体
Syntax:	proxy_set_body value;
Default:	—
Context:	http, server, location

### 超时时间

# 与代理服务器连接的超时时间，通常不会超过75s
Syntax:	proxy_connect_timeout time;
Default:	proxy_connect_timeout 60s;
Context:	http, server, location

# 接收服务器响应的数据的超时时间	
Syntax:	proxy_send_timeout time;
Default:	proxy_send_timeout 60s;
Context:	http, server, location

# 读取代理服务器响应超时时间
Syntax:	proxy_read_timeout time;
Default:	proxy_read_timeout 60s;
Context:	http, server, location


### 缓冲区配置
# 开启后，当nginx接收到目标服务器的响应时，会将其保存到 proxy_buffers 设置的缓冲区中
# 缓冲区的大小由proxy_buffer_size设定
# 当响应过大，缓冲区装不下时，可以将一部分放入到磁盘的临时文件中
# 这个临时文件由proxy_max_temp_file_size和proxy_temp_file_write_size配置项配置。

# 开启，关闭代理服务器缓冲区
Syntax:	proxy_buffering on | off;
Default:	proxy_buffering on;
Context:	http, server, location

# 配置缓冲区大小	
Syntax:	proxy_buffer_size size;
Default:	
proxy_buffer_size 4k|8k;
Context:	http, server, location

### 跳转重定向
# 当后端返回301重定向的地址是需要代理的地址的时候，就可以使用此种方式重写跳转地址的url，防止客户端访问不到目标地址
# 默认使用default即可
Syntax:	proxy_redirect default;
	proxy_redirect off;
	proxy_redirect redirect replacement;
Default:	proxy_redirect default;
Context:	http, server, location
```

## 企业应用nginx作为代理通常所要配置的参数

```nginx
location / {
    proxy_pass http://127.0.0.1:8080;
    proxy_redirect default;
    
	# 代理添加的头信息,需要服务端直到客户端的真实ip
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;

    # 超时时间设置
    proxy_connect_timeout 30;
    proxy_send_timeout 60;
    proxy_read_timeout 60;

    # 缓冲配置，用以减少频繁的IO
    proxy_buffer_size 32k; # 页面通常不会超过32k
    proxy_buffering on;
    proxy_buffers 4 128k;
    proxy_busy_buffers_size 256k;
    proxy_max_temp_file_size 256k; # 当buffer与busy_buffers都用完的时候，就需要文件缓冲 
    # 可以使用 nginx -V 查看 --http-proxy-temp-path 变量，查看临时文件位置
}
```

通常proxy的配置是很长很冗余的，可以将其拆分为文件，并引入：

```nginx
# proxy_params 文件
proxy_redirect default;

proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;

proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;

proxy_buffer_size 32k;
proxy_buffering on;
proxy_buffers 4 128k;
proxy_busy_buffers_size 256k;
proxy_max_temp_file_size 256k;

# conf文件
location / {
    proxy_pass http://127.0.0.1:8080;
    include proxy_params;
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://yangsx95.gitbook.io/notes/distributed/web-fu-wu-qi/nginx/dai-li-fu-wu.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
