darkgoldblade01's avatar

[NGINX Conf] - 413 Error - client_max_body_size doesn't work

Hey everyone,

I am using Forge, obviously, and I am having a 413 issue. I have PHP set to 500M upload_max_filesize and post_max_size. It is showing as set to that in the PHP Info page.

HOWEVER, when I go to add client_max_body_size to the nginx.conf http section, it ends up creating an error that I can't restart NGINX.

The full nginx.conf file is:

user forge;
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_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/*;
        #client_max_body_size 0; # <-- This line causes an issue when I uncomment it.
}

I am getting the error:

Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.

When I run systemctl status nginx.service, I get the following:

● 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 Tue 2016-10-25 14:47:24 MST; 4s ago
Process: 3813 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited, status=2)
Process: 23182 ExecReload=/usr/sbin/nginx -g daemon on; master_process on; -s reload (code=exited, status=0/SUCCESS)
Process: 23168 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Process: 5307 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=1/FAILURE)
Main PID: 23171 (code=exited, status=0/SUCCESS)

Any insight on why I can't increase the client size, or even add it to the config files?

UPDATE #1

I found one thing, under the /etc/nginx/conf.d/ folder there is an uploads.conf file, that has the client_max_body_size variable set in it. I changed it to 0, which should make it not care how large of a request it is. It is still giving me a 413 error.

0 likes
4 replies
willvincent's avatar

This is the nginx config I use on my server (not forge, but only significant difference is the user, and sites-enabled include directory..)

user www-data;
worker_processes 4;
worker_rlimit_nofile 8192;
events {
  worker_connections 8000;
}

http {
  add_header                    X-Frame-Options SAMEORIGIN;
  add_header                    X-XSS-Protection "1; mode=block";
  server_tokens                 off;

  include                       mime.types;
  default_type                  application/octet-stream;
  client_max_body_size          2000M;
  client_body_buffer_size       128k;
  sendfile                      on;
  tcp_nopush                    on;
  keepalive_timeout             65;
  tcp_nodelay                   on;

  gzip                          on;
  gzip_disable                  "MSIE [1-6]\.(?!.*SV1)";
  gzip_buffers                  16 8k;
  gzip_comp_level               6;
  gzip_http_version             1.1;
  gzip_min_length               10;
  gzip_types                    text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
  gzip_vary                     on;
  gzip_proxied                  any; # Compression for all requests.

  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;
  error_log /var/log/nginx_error.log debug;

  include /opt/nginx/sites-enabled/*;
}

These settings have worked for me for the past 10yrs, no upload issues regardless of filesize, so long as the file is less than whatever size is configured to be allowed by php.. ;)

1 like
darkgoldblade01's avatar
Level 1

I found the issue. I am using a load balancer, made through forge, so you have to create an uploads.conf file under /etc/nginx/conf.d/ on that server too, and set it to the same as the main hosting servers. Confusing, but finally figured it out!

Would be nice if we could set that in the application layer, and have it replicate out as needed, or even set it on the load balancer, and have it handle going to the server's it is over, and doing it that way. Just a couple of thoughts.

1 like
willvincent's avatar

That's one of the drawbacks to forge and similar services, makes you less familiar with all the moving pieces. :)

harryth3hopp3r's avatar

Everything can actually be done inside Forge to resolve this.

If you are using a LB inside Forge simply update the max upload size inside the "Meta" tab on forge. Do the same two all servers within the LB.

Please or to participate in this conversation.