Nginx 相关配置
1、文件大小配置
在Nginx中,可以使用client_max_body_size指令来限制请求体的大小,也就是限制文件的上传大小。例如,如果你想要限制文件大小为10MB,你可以在Nginx配置文件中的http、server或者location块中添加以下配置:
client_max_body_size 10M;
这将会为所有的请求设置最大请求体大小为10MB。如果你想针对特定的server或location进行设置,你可以将这个指令放在相应的块中。
以下是一个配置文件的示例,它将在server块中设置文件大小限制:
http {
# ...
server {
# ...
# 设置最大上传文件大小为10MB
client_max_body_size 10M;
# ...
location /upload {
# ...
}
}
}
在这个配置中,任何指向/upload location的请求将被允许最大上传文件大小为10MB。如果你需要针对不同的location设置不同的文件大小限制,你可以在location块中单独设置client_max_body_size指令。
2、超时设置
超时设置应该放在Nginx配置文件的http、server或location块中。
2.1 客户端超时设置(如何长时间等待客户端发送请求头和主体):
client_header_timeout 10s; # 等待客户端请求头的超时时间
client_body_timeout 10s; # 等待客户端请求主体的超时时间
2.2 保持连接的超时设置(长连接超时时间):
keepalive_timeout 30s; # 长连接超时时间,超过这个时间将关闭连接
2.3 发送超时设置(发送响应到客户端的超时时间):
send_timeout 10s; # 向客户端发送响应的超时时间
2.4 代理超时设置(向后端代理请求的超时时间):
proxy_connect_timeout 10s; # 连接到代理服务器的超时时间
proxy_send_timeout 10s; # 向代理发送请求的超时时间
proxy_read_timeout 10s; # 从代理接收响应的超时时间
3、代理转发
3.1 基础设置
http {
server {
listen 80;
location /api {
proxy_pass http://backend_server_ip:backend_server_port;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
}
3.2 详细策略
-
第一种
location /api/ { proxy_pass http://127.0.0.1:8080/; } ## 请求是:http://127.0.0.1/api/hello,会转发到:http://127.0.0.1:8080/hello -
第二种
location /api/ { proxy_pass http://127.0.0.1:8080; } ## 请求是:http://127.0.0.1/api/hello,会转发到:http://127.0.0.1:8080/api/hello -
第三种
location /api/ { proxy_pass http://127.0.0.1:8080/self/; } ## 请求是:http://127.0.0.1/api/hello,会转发到:http://127.0.0.1:8080/self/hello -
第四种
location /api/ { proxy_pass http://127.0.0.1:8080/self; } ## 请求是:http://127.0.0.1/api/hello,会转发到:http://127.0.0.1:8080/selfhello -
第五种
location /api { proxy_pass http://127.0.0.1:8080/; } ## 请求是:http://127.0.0.1/api/hello,会转发到:http://127.0.0.1:8080//hello -
第六种
location /api { proxy_pass http://127.0.0.1:8080; } ## 请求是:http://127.0.0.1/api/hello,会转发到:http://127.0.0.1:8080/api/hello

浙公网安备 33010602011771号