简体   繁体   English

413 请求实体太大 nginx django

[英]413 Request Entity Too Large nginx django

I am making a practice web service (client's artbook display web site) The client can upload artbook images to the server.我正在练习 web 服务(客户端的画册显示 web 站点)客户端可以将画册图像上传到服务器。

But I get the following error when the client uploads too many images但是当客户端上传太多图片时出现以下错误

413 Request Entity Too Large

I tried adding client_max_body_size 100M;我尝试添加client_max_body_size 100M; in nginx.conf在 nginx.conf

#user  nobody;
#Defines which Linux system user will own and run the Nginx server

worker_processes  1;

#error_log  logs/error.log; #error_log  logs/error.log  notice;
#Specifies the file where server logs.

#pid        logs/nginx.pid;
#nginx will write its master process ID(PID).

events {
    worker_connections  1024;
}


http {
    include       mime.types;

    default_type  application/octet-stream;

    #access_log  logs/access.log  main;

    sendfile        on;

    server {
        listen       80;

        server_name  xxxx.net;
        client_max_body_size 100M;
        keepalive_timeout 5;

        return 301 https://$server_name$request_uri;

    }

    # HTTPS server
    #
    server {
        listen       443 default_server ssl;
        server_name  xxx.net;

        ssl_certificate      /etc/letsencrypt/live/xxxx.net/fullchain.pem;
        ssl_certificate_key  /etc/letsencrypt/live/xxxx.net/privkey.pem;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;


        location / {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto https;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header HOST $http_host;
            proxy_set_header X-NginX-Proxy true;

            proxy_pass http://127.0.0.1:8000;
            proxy_redirect off;
        }
    }
}

and tried:并尝试:

sudo service nginx restart
sudo service nginx reload

and retry并重试

runserver 

but still get但仍然得到

413 Request Entity Too Large

Can anybody help?有人可以帮忙吗?

You've fixed the issue on your HTTP server, but your HTTP server is set to 301 redirect to your HTTPS server... your HTTPS server does not have client_max_body_size configured, so it is defaulting to 1M & causing this 413 (Request Entity Too Large) error. 您已在HTTP服务器上解决了该问题,但是HTTP服务器设置为301重定向到HTTPS服务器...。您的HTTPS服务器未配置client_max_body_size ,因此默认情况下为1M并导致此413(请求实体也大)错误。

To fix this issue, you simply need to add client_max_body_size to BOTH the HTTP server block and the HTTPS server block, as shown in the example below: 要解决这个问题,你只需要添加client_max_body_size两个 HTTP服务器块 HTTPS服务块,如下面的例子:

http {
    ...
    ######################
    # HTTP server
    ######################
    server {
        ...
        listen       80;
        server_name  xxxx.net;
        client_max_body_size 100M;
        ...
    }

    ######################
    # HTTPS server
    ######################
    server {
        ...
        listen       443 default_server ssl;
        server_name  xxxx.net;
        client_max_body_size 100M;
        ...
    }
}

More info on client_max_body_size here: http://nginx.org/en/docs/http/ngx_http_core_module.html#client_max_body_size 有关client_max_body_size更多信息,请client_max_body_sizehttp : //nginx.org/en/docs/http/ngx_http_core_module.html#client_max_body_size

Syntax: client_max_body_size size; 语法:client_max_body_size size;

Default: client_max_body_size 1m; 默认值:client_max_body_size 1m;

Context: http, server, location 上下文:http,服务器,位置

Sets the maximum allowed size of the client request body, specified in the “Content-Length” request header field. 设置客户端请求正文的最大允许大小,在“ Content-Length”请求标头字段中指定。 If the size in a request exceeds the configured value, the 413 (Request Entity Too Large) error is returned to the client. 如果请求中的大小超过配置的值,则会向客户端返回413(请求实体太大)错误。 Please be aware that browsers cannot correctly display this error. 请注意,浏览器无法正确显示此错误。 Setting size to 0 disables checking of client request body size. 将size设置为0将禁用对客户端请求主体大小的检查。

Read More about configuring HTTPS servers here: http://nginx.org/en/docs/http/configuring_https_servers.html 在此处阅读有关配置HTTPS服务器的更多信息: http : //nginx.org/en/docs/http/configuring_https_servers.html

Open the terminal for Ubuntu打开 Ubuntu 终端

Use nano text editor:使用 nano 文本编辑器:

$ $

sudo nano /etc/nginx/nginx.conf

set client body size to 100M将客户端主体大小设置为 100M

client_max_body_size 100M;

Like:喜欢:

http {

        ##
        # Basic Settings
        ##
        client_max_body_size 100M;
        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # SSL Settings
        ## More codes here ...

}

I know this has already been answered, but rather than having the "client_max_body_size 100M;"我知道这已经得到了回答,但没有“client_max_body_size 100M;” multiple times in your code under the servers, you can just add it once, under the http section - see line 2 below.在服务器下的代码中多次添加,您可以在 http 部分下添加一次 - 请参见下面的第 2 行。

http {
    client_max_body_size 100M;
    ...
    ######################
    # HTTP server
    ######################
    server {
        ...
        listen       80;
        server_name  xxxx.net;
        ...
    }

    ######################
    # HTTPS server
    ######################
    server {
        ...
        listen       443 default_server ssl;
        server_name  xxxx.net;
        ...
    }
}

Source: http://nginx.org/en/docs/http/ngx_http_core_module.html#client_max_body_size来源: http://nginx.org/en/docs/http/ngx_http_core_module.html#client_max_body_size

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM