首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 綜合教學

Nginx concat 模組 400 Bad Request 根因與修復:MIME 類型不匹配導致

     編輯:Java 綜合教學

現象

Nginx 1.5.x 版本啟用 nginx-http-concat 模組後,合併請求返回 400 Bad Request,1.4.x 正常。

根因分析

Nginx 1.5+ 標準化 JS MIME 類型為 application/javascript,但模組源碼 ngx_http_concat_module.c 僅識別舊類型 application/x-javascript,導致類型匹配失敗,拒絕處理請求。

修復方案

修改模組源碼

文件:nginx-http-concat-master/ngx_http_concat_module.c 第 14 行左右

// 原代碼
static ngx_str_t ngx_http_concat_default_types[] = {
    ngx_string("application/x-javascript"),
    ngx_string("text/css"),
    ngx_null_string
};

方案一:替換為新標準類型

static ngx_str_t ngx_http_concat_default_types[] = {
    ngx_string("application/javascript"),
    ngx_string("text/css"),
    ngx_null_string
};

方案二:兼容雙類型(推薦)

static ngx_str_t ngx_http_concat_default_types[] = {
    ngx_string("application/x-javascript"),
    ngx_string("application/javascript"),
    ngx_string("text/css"),
    ngx_null_string
};

重新編譯驗證

./configure --prefix=/usr/local/nginx --add-module=../nginx-http-concat-master
make && make install
/usr/local/nginx/sbin/nginx -s reload

驗證結果

合併請求 /static/js/a.js??b.js 正常返回 200,響應頭 Content-Type: application/javascript。

避坑提示

  • 修改模組源碼後必須重新編譯 Nginx
  • 生產環境建議保留雙類型兼容,避免舊客戶端問題
  • 同理,CSS 類型 text/css 穩定無需變更
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved