Skip to content
View in the app

A better way to browse. Learn more.

PHP论坛人

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

nginx.conf 優化 / 413 Request Entity Too Large 錯誤訊息

Featured Replies


413 Request Entity Too Large 這個問題的核心原因只有一個,因為 client_max_body_size 太小

搜尋 client_max_body_size

並修改為 client_max_body_size 200m;





--------------------------------------------------
優化 nginx.conf 適用於 VPS 2C / 2GB
--------------------------------------------------


修改
vi /etc/nginx/nginx.conf



刪除原有內容,貼上以下內容



user www-data;
worker_processes auto;
worker_cpu_affinity auto;
pid /run/nginx.pid;
worker_rlimit_nofile 65535;
# 單個worker可開啟的最大檔案描述符數,需與系統層級 /etc/security/limits.conf 的nofile一致


# 載入動態模組
include /etc/nginx/modules-enabled/*.conf;

events {
    use epoll;
    worker_connections 2048;
    multi_accept off;
    accept_mutex off;
}

http {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    server_tokens off;
    # 隱藏Nginx版本號,避免洩漏版本資訊

    # 最大上傳檔案大小,需與 PHP 的 post_max_size / upload_max_filesize 對應
    client_max_body_size 200m;

    # 逾時設定
    client_body_timeout     60s;
    client_header_timeout   30s;
    keepalive_timeout  30s;
    send_timeout           60s;

    # 緩衝區優化
    types_hash_max_size 2048;
    server_names_hash_bucket_size 128;
    client_header_buffer_size 32k;
    large_client_header_buffers 4 32k;
    sendfile_max_chunk 512k;

    # File Cache
    open_file_cache max=10000 inactive=30s;
    open_file_cache_valid 60s;
    open_file_cache_min_uses 2;
    open_file_cache_errors on;

    # 真實IP還原(使用 CDN 或反向代理時啟用)
    # 若無使用 CDN,請保持以下區塊為註解
    # 若使用 Cloudflare,需填入 Cloudflare 的IP範圍
    # 最新 Cloudflare IPv4:https://www.cloudflare.com/ips-v4
    # 最新 Cloudflare IPv6:https://www.cloudflare.com/ips-v6
    # set_real_ip_from 10.0.0.0/8;
    # set_real_ip_from 172.16.0.0/12;
    # set_real_ip_from 192.168.0.0/16;
    # real_ip_header CF-Connecting-IP;
    # real_ip_recursive on;

    # FastCGI 優化(PHP-FPM)
    fastcgi_intercept_errors on;
    fastcgi_hide_header X-Powered-By;

    # 逾時設定(論壇有時需要較長的PHP執行時間,如批次發文、重建索引)
    fastcgi_connect_timeout 300s;
    fastcgi_send_timeout 300s;
    fastcgi_read_timeout 300s;

    # 緩衝區設定
    fastcgi_buffer_size 128k;
    fastcgi_buffers 8 256k;
    fastcgi_busy_buffers_size 512k;
    fastcgi_temp_file_write_size 256k;

    # Gzip 壓縮(有效減少網頁傳輸流量,提升載入速度)
    gzip on;
    gzip_min_length 1024;
    gzip_buffers 4 16k;
    gzip_http_version 1.1;
    gzip_comp_level 2;
    gzip_vary on;
    gzip_proxied expired no-cache no-store private auth;
    gzip_disable "MSIE [1-6]\.";
    gzip_types
        text/plain
        text/css
        application/javascript
        application/json
        application/xml
        image/svg+xml;

    # MIME 類型
    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    # SSL 全域設定
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_prefer_server_ciphers on;
    ssl_ciphers 'TLS_AES_256_GCM_SHA384:TLS_AES_128_GCM_SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256';
    ssl_session_timeout 1d;
    ssl_session_cache shared:SSL:10m;
    ssl_session_tickets off;

    # 日誌格式(Fail2ban 解析依賴此格式)
    # $remote_addr 在CDN環境下需確認為真實用戶IP (依靠上方 real_ip 設定)
    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 buffer=32k flush=5s;

    # 錯誤日誌
    error_log /var/log/nginx/error.log warn;

    # 虛擬主機載入
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*.conf;
}






儲存檔案並離開vi編輯器
按 Esc,輸入 :wq,按 Enter




說明:worker_rlimit_nofile 65535 若系統預設的 nofile 值較低,Nginx 啟動時可能無法套用

需同步確認 systemd override.conf 中的 LimitNOFILE 設定







-------------------
測試語法
-------------------

測試語法
nginx -t



重新載入
systemctl reload nginx

Edited by Jack

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.