nginx服务器应用中遇到的两个问题

1》首先是413的错误!

1 client_max_body_size
2 Context: http, server, location
3 It is the maximum size of a client request body. If this size is exceeded, Nginx
4 returns a 413 Request entity too large HTTP error. This setting is particularly
5 important if you are going to allow users to upload files to your server over HTTP.
6 Syntax: Size value
7 Default value: 1m

这种错误,意思是说客户端请求的数据量太大。nginx的配置中,若没有专门指定这个选项的内容,则默认是1m的大小。但是,对于请求body过大的HTTP请求,则需要给相应的配置配置大一些的适合自己的web站点的body大小。

这个参数的配置适合的位置有 http,server,location三个地方都可以。

2》504 Gateway Time-out错误

这个错误,是和时间配置相关的。大家可能会有这种场景, 比如一个http请求到后端服务,后端服务的时间会比较长才给予前端响应,这个时候就要考虑是否会出现这种504的错误了。

我的应用,是在我们开发的CMS系统中,前端请求后台执行发布13000篇文章的发布。这个就会花点时间了,我们的系统,13000片文章,大概花3分钟。

我们的服务器架构很简单,前端nginx负责静态资源响应,nginx作为反向代理实现负载均衡(tomcat作为后端服务器),处理后端动态http请求,例如发布页面这种后端服务的http请求也在其中。

这种错误,相应的nginx的错误日志中,会看到下面的内容:

1 2016/08/05 18:47:06 [error] 31490#0: *48123 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 10.90.9.20, server: localhost, request: "GET /CMS/page/articleCenter/deployeeAll?type=41 HTTP/1.1", upstream: "http://10.130.202.135:8080/CMS/page/articleCenter/deployeeAll?type=41", host: "10.130.202.136", referrer: "http://10.130.202.136/CMS/page/pageTreeMgmt"

那么,如何解决这种错误呢?我们先看看nginx的服务器配置文档说明吧!

proxy_read_timeout: Sets read timeout for backend communications
proxy_send_timeout: Sets write timeout for backend communications

这个错误,是由于上述的两个timeout的时间没有设置,默认值比较小,60s,此处,我们将其修改的相对比较大,都改为3000了。

上述两个问题的解决办法很简单,将配置修改后的内容也附在这里作为参考吧!

 1 http {
 2     include       mime.types;
 3     default_type  application/octet-stream;
 4 
 5     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
 6     #                  '$status $body_bytes_sent "$http_referer" '
 7     #                  '"$http_user_agent" "$http_x_forwarded_for"';
 8 
 9     #access_log  logs/access.log  main;
10 
11     sendfile        on;
12     #tcp_nopush     on;
13 
14     #keepalive_timeout  0;
15     keepalive_timeout  1000;
16     #gzip  on;
17 
18     upstream cms {
19       server xx.xx.xx.xx:8080;
20     }
21     client_max_body_size 10M;#解决第一个问题的配置
22 
23     server {
24         listen       80;
25         server_name  localhost;
26         #下面两行是解决第二个错误的配置
27         proxy_send_timeout   3000;
28         proxy_read_timeout   3000;
29         location /CMS{
30 
31              proxy_pass http://cms;
32              proxy_set_header Host $host:$server_port;
33              proxy_set_header Remote_Addr $remote_addr;
34              proxy_set_header X-Real-IP $remote_addr;
35              proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
36           }
37 
38          location /resource {
39              root html/TK_ROOT;
40              allow all;
41           }
42       }
43 }
原文地址:https://www.cnblogs.com/shihuc/p/5748058.html