如何在Nginx服务器上增加上传图片的大小?

时间:2018-08-22 21:57:29

标签: nginx server nginx-config

我已经在Django中开发了一个应用程序。我陷入了这样的问题:某些图像(大小:2MB)已成功上传,而某些图像(大小:4,5MB)在上传后产生了错误。经过研究,我发现此问题是由于我的Nginx服务器文件上传大小引起的。

这是我的nginx.conf文件

user www-data;
worker_processes auto;
pid /run/nginx.pid;


events {
        worker_connections 768;
        # multi_accept on;
}

http {

        ##
        # Basic Settings
        ##



       
        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
        ##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        ##
        # Gzip Settings
        ##

        gzip on;
        gzip_disable "msie6";

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
         # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}


#mail {
#       # See sample authentication script at:
#       # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
#       # auth_http localhost/auth.php;
#       # pop3_capabilities "TOP" "USER";
#       # imap_capabilities "IMAP4rev1" "UIDPLUS";



        # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}


#mail {
#       # See sample authentication script at:
#       # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
#       # auth_http localhost/auth.php;
#       # pop3_capabilities "TOP" "USER";
#       # imap_capabilities "IMAP4rev1" "UIDPLUS";
# 
#       server {
#               listen     localhost:110;
#               protocol   pop3;
#               proxy      on;
#       }
# 
#       server {
#               listen     localhost:143;
#               protocol   imap;
#               proxy      on;
#       }
#}

我应如何编辑此文件以将图像的上传大小增加到大约6MB。

当我遵循这个问题How to edit nginx.conf to increase file size upload

我遇到以下错误:

● nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Wed 2018-08-22 21:44:46 UTC; 19min ago
  Process: 10043 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=1/FAILURE)
 Main PID: 9254 (code=exited, status=0/SUCCESS)

Aug 22 21:44:46 ip-172-31-24-109 systemd[1]: Starting A high performance web server and a reverse proxy server...
Aug 22 21:44:46 ip-172-31-24-109 nginx[10043]: nginx: [emerg] could not build server_names_hash, you should increase server_names_hash_bucket_size
Aug 22 21:44:46 ip-172-31-24-109 nginx[10043]: nginx: configuration file /etc/nginx/nginx.conf test failed
Aug 22 21:44:46 ip-172-31-24-109 systemd[1]: nginx.service: Control process exited, code=exited status=1
Aug 22 21:44:46 ip-172-31-24-109 systemd[1]: Failed to start A high performance web server and a reverse proxy server.
Aug 22 21:44:46 ip-172-31-24-109 systemd[1]: nginx.service: Unit entered failed state.
Aug 22 21:44:46 ip-172-31-24-109 systemd[1]: nginx.service: Failed with result 'exit-code'.

所以请为我编辑文件。我知道可能只有一个行代码。

2 个答案:

答案 0 :(得分:0)

您的域名可能太长。 您是否尝试取消注释/更改此设置?

http {
    server_names_hash_bucket_size 64;
    ...
}

然后测试配置:

nginx -t

然后重新加载nginx:

systemctl status nginx

答案 1 :(得分:0)

转到nginx文件:/etc/nginx/nginx.conf

在http部分的下面添加行。

http { ......... ........ client_max_body_size maxSize(say 500M); }