# Nginx 问题集

#### &#x20;博客作者：联系请[点击](https://hezhiqiang.gitbook.io/about-the-author/lian-xi-zuo-zhe)，搬运不易，希望请作者喝咖啡，可以点击[联系博客作者](https://hezhiqiang.gitbook.io/about-the-author/lian-xi-zuo-zhe)

## Nginx 出现大量 TIME\_WAIT

### 检测TIME\_WAIT状态的语句

```
$ netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'  
SYN_RECV 7
ESTABLISHED 756
FIN_WAIT1 21
SYN_SENT 3
TIME_WAIT 2000
```

状态解析：

* `CLOSED` - 无连接是活动的或正在进行
* `LISTEN` - 服务器在等待进入呼叫
* `SYN_RECV` - 一个连接请求已经到达，等待确认
* `SYN_SENT` - 应用已经开始，打开一个连接
* `ESTABLISHED` - 正常数据传输状态
* `FIN_WAIT1` - 应用说它已经完成
* `FIN_WAIT2` - 另一边已同意释放
* `ITMED_WAIT` - 等待所有分组死掉
* `CLOSING` - 两边同时尝试关闭
* `TIME_WAIT` - 另一边已初始化一个释放
* `LAST_ACK` - 等待所有分组死掉

### 解决方法

执行 `vim /etc/sysctl.conf`，并添加下面字段

```
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_fin_timeout = 30
```

执行 /`sbin/sysctl -p` 让修改生效。

## 上传文件大小限制

### 问题现象

显示错误信息：**413 Request Entity Too Large**。

意思是请求的内容过大，浏览器不能正确显示。常见的情况是发送 `POST` 请求来上传大文件。

### 解决方法

* 可以在 `http` 模块中设置：`client_max_body_size 20m;`
* 可以在 `server` 模块中设置：`client_max_body_size 20m;`
* 可以在 `location` 模块中设置：`client_max_body_size 20m;`

三者区别是：

* 如果文大小限制设置在 `http` 模块中，则对所有 Nginx 收到的请求。
* 如果文大小限制设置在 `server` 模块中，则只对该 `server` 收到的请求生效。
* 如果文大小限制设置在 `location` 模块中，则只对匹配了 `location` 路由规则的请求生效。

## 请求时间限制

### 问题现象

请求时间较长，链接被重置页面刷新。常见的情况是：上传、下载大文件。

### 解决方法

修改超时时间


---

# Agent Instructions: 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://hezhiqiang.gitbook.io/about-the-author/zhong-jian-jian-jiao-cheng-xue-xi/nginx-wen-ti-ji.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.
