Nginx Client_max_body_size无法解决

时间:2017-09-12 15:15:09

标签: nginx configuration

我有nginx问题。我使用NGINX作为反向代理,nodejs作为后端,Mongodb作为DB。从几天开始,我得到一个奇怪的错误,说太大的身体。我插入client_max-body_size到10m,但仍然无法解决问题。请告诉我我做错了什么 2017/09/11 23:46:40 [error] 39158#39158: *156474 client intended to send too large body: 3356812 bytes, client: 10.150.98.84, server: example.com, request: "POST /api/sensors HTTP/1.1", host: "example.com"

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
    worker_connections 1024;
}
http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  /var/log/nginx/access.log  main;
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    index   index.html index.htm;
upstream nodejs {
    server localhost:3000;
}
    server {
        listen       80;
        server_name  example.com;


location / {
if ($http_x_forwarded_proto != 'https') {
            return 301 https://$server_name$request_uri;
        }
        try_files $uri $uri/ @nodejs;
    }

    location @nodejs {
        proxy_redirect off;
        proxy_http_version 1.1;
        proxy_pass http://nodejs;
        proxy_set_header Host $host ;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        client_max_body_size 10M;
    }
location ~ (/images|/img|/javascript|/js|/css|/stylesheets|/flash|/media|/static|robots.txt|humans.txt|favicon.ico){
root /var/www/app/public;
client_max_body_size 10M;
}
        # redirect server error pages to the static page /40x.html
        #
        error_page 404 /404.html;
            location = /40x.html {
        }
        # redirect server error pages to the static page /50x.html
        #
        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
}
}

1 个答案:

答案 0 :(得分:0)

/api/sensors添加特定的位置块,并增加相同

的限制
location /api/sensors {
    client_max_body_size 10M;
    try_files dummy_file_not_exists @nodejs;
}

这样,您只会使用希望接收它的大型主体公开端点。在投入生产之前,请在本地进行测试。