写在前面
最近写一个 php 接口,接受上传的文件,发现文件只要超过 5m 以上就会无响应失败,最后发现是 * 的timeout设置问题(我全程开了全局的 VPN),但一开始并不知晓,把 nginx 和 php 的相关配置都看了个遍,干脆记录一下,以后遇到此类问题可以按照这个逻辑顺序去排查。
零、环境
PHP Version 7.0.10
一、php
通过修改php.ini:
1、上传相关
| options | type | desc | remark |
|---|---|---|---|
| file_uploads | boolean | 是否允许HTTP文件上传 | 'On' or 'Off' |
| upload_tmp_dir | string | 上传时存储文件的临时目录。 | 如果未指定,将使用系统的默认值(/tmp)。 |
| upload_max_filesize | integer | 上传文件的最大大小。 | 是所有文件字段的大小总和。 |
| post_max_size | integer | post_max_size = upload_max_filesize + 所有其他字段的长度(跟文件比微不足道) | post_max_size 一般略大于upload_max_filesize,但为了省事可以直接让它们相等。 |
| max_file_uploads | integer | 允许同时上传的最大文件数。(提交时保留空白的上传字段不计入此限制) |
注:
upload_max_filesize和post_max_size可以用速记表达:K (for Kilobytes), M (for Megabytes) and G (for Gigabytes; available since PHP 5.1.0) ,不区分大小写。
2、时间相关
| options | type | desc | remark |
|---|---|---|---|
| max_input_time | integer | 脚本解析输入数据允许的最大时间,单位是秒。 它从接收所有数据到开始执行脚本进行测量的。 | |
| max_execution_time | integer | 这设置了脚本被解析器中止之前允许的最大执行时间,单位秒。 这有助于防止写得不好的脚本占尽服务器资源。 | 你的 web 服务器也可以有其他超时设置。 |
先
max_input_time再max_execution_time。
3、内存相关
| options | type | desc | remark |
|---|---|---|---|
| memory_limit | integer | 这将设置允许脚本分配的最大内存量(以字节为单位)。这有助于防止编写糟糕的脚本占用服务器上的所有可用内存。 | 要没有内存限制,请将此指令设置为-1。 |
二、nginx
通过修改nginx.conf:
| options | type | desc | remark |
|---|---|---|---|
| client_max_body_size | integer |
返回413(请求实体太大)错误。
三、*
通过修改preferences:
| options | type | desc | remark |
|---|---|---|---|
timeout |
integer | 超时时间 |
返回net::ERR_CONNECTION_RESET错误。