如何設定 laradock nginx 上傳檔案限制 (error 413 Request Entity Too Large)

在 laradock 包含的 nginx 預設最大上傳檔案限制在 20Mb

假設需要將限制調整到 40Mb

作法如下:

首先開啟 nginx.conf

vim laradock/nginx/nignx.conf

接著,設定以下幾個參數,將上傳限制調整為 40Mb,以及延長上傳時間

keepalive_timeout 120;
client_max_body_size 200M;
user www-data;
worker_processes 8;
pid /run/nginx.pid;
daemon off;

events {
  worker_connections  4096;
  multi_accept on;
  use epoll;
}

http {
  server_tokens off;
  sendfile on;
  tcp_nopush on;
  tcp_nodelay on;
  keepalive_timeout 120;
  types_hash_max_size 4096;
  client_max_body_size 200M;
  include /etc/nginx/mime.types;
  default_type application/octet-stream;
  access_log /dev/stdout;
  error_log /dev/stderr;
  gzip on;
  gzip_disable "msie6";

設定 laravel.ini

vim laradock/php-fpm/laravel.ini

date.timezone=UTC
display_errors=Off
log_errors=On

; Maximum amount of memory a script may consume (128MB)
; http://php.net/memory-limit
memory_limit = 512M
; Maximum allowed size for uploaded files.
; http://php.net/upload-max-filesize
upload_max_filesize = 40M
; Sets max size of post data allowed.
; http://php.net/post-max-size
post_max_size = 40M
max_execution_time=1200
default_socket_timeout=3600
request_terminate_timeout=1200

接著,停止運行中的 laradock

docker stop $(docker ps -a -q)

重新安裝 php-fpm 及 nginx containers ,避免有 cache 存在

docker-compose build --no-cache php-fpm
docker-compose build --no-cache nginx

備註

如果遇到 error code: 414, uri too large 可以增加以下項目:

    client_header_buffer_size 10M;
    large_client_header_buffers 4 10M;