一、安裝和編譯證書模塊
[root@master nginx]# wget https://nginx.org/download/nginx-1.25.3.tar.gz
[root@master nginx]# tar -zxvf nginx-1.25.3.tar.gz
[root@master nginx]# cd nginx-1.25.3
[root@master nginx]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-stream --with-stream_ssl_module
[root@master nginx]# make && make install#查看編譯是否成功
nginx -V
二、上傳證書到指定的路徑下
3.1、上傳證書到/usr/local/nginx/conf/ssl目錄下
#創建存放證書的路徑方便管理
mkdir -p /usr/local/nginx/conf/ssl#上傳證書
3.2、查看證書的詳情
openssl x509 -in /usr/local/nginx/conf/ssl/gateway.crt -text -noout
三、Nginx配置HTTPS和TCPS
(一)、HTTPS配置
4.1、HTTP配置
server {listen 8888;server_name 192.168.72.130;location / {root /opt/sumscope;}
}
4.2、HTTPS配置
#編輯server添加如下內容
server {listen 8888 ssl;ssl_certificate /usr/local/nginx/conf/ssl/gateway.crt;ssl_certificate_key /usr/local/nginx/conf/ssl/gateway.key;server_name 192.168.72.130;location / {root /opt/sumscope;}
}#執行reload
nginx -s reload
繼續以HTTP訪問出現400報錯
因為配置了HTTPS通過HTTP訪問就會報錯,要想解決HTTP訪問自動跳轉到HTTPS,需要將HTTP請求重定向到HTTPS。
400報錯原因:
當使用 Nginx 時遇到 400 錯誤(Bad Request),意味著客戶端發送的請求存在問題,Nginx 無法理解該請求。下面為你詳細分析可能的原因:
請求頭相關問題
- 請求頭過長:如果客戶端發送的請求頭包含過多的信息,超過了 Nginx 配置中允許的最大請求頭長度,Nginx 就會返回 400 錯誤。可以通過調整?
http
?塊或?server
?塊中的?large_client_header_buffers
?指令來解決,示例如下:
http {large_client_header_buffers 4 8k;...
}
上述配置將最大請求頭緩沖區數量設置為 4,每個緩沖區大小為 8KB。
- 請求頭格式錯誤:若請求頭的格式不符合 HTTP 協議規范,例如缺少必要的字段、字段值格式錯誤或包含非法字符等,Nginx 會拒絕該請求。比如,請求頭中的?
Host
?字段缺失或格式錯誤,就可能導致 400 錯誤。 - 請求頭編碼問題:如果請求頭使用了不支持的字符編碼,Nginx 可能無法正確解析請求,從而返回 400 錯誤。
請求 URI 相關問題
- URI 過長:當客戶端發送的請求 URI 長度超過 Nginx 配置中允許的最大值時,會觸發 400 錯誤。可以通過修改?
http
?塊或?server
?塊中的?client_header_buffer_size
?和?large_client_header_buffers
?指令來調整最大 URI 長度。 - URI 包含非法字符:請求 URI 中包含不允許的字符,如未經過正確編碼的特殊字符,可能會導致 Nginx 無法解析請求。例如,在 URI 中直接使用空格而未進行編碼,就會引發問題。
客戶端或代理問題
- 客戶端軟件故障:某些客戶端軟件(如瀏覽器、爬蟲程序等)可能存在漏洞或配置錯誤,導致發送的請求不符合 HTTP 協議規范。例如,瀏覽器插件可能會修改請求頭,從而引發 400 錯誤。
- 代理服務器配置錯誤:如果使用了代理服務器,代理服務器的配置錯誤可能會影響請求的正常轉發。例如,代理服務器可能會修改請求頭或 URI,導致請求在到達 Nginx 時出現問題。
其他問題
- SSL/TLS 握手問題:在使用 HTTPS 協議時,SSL/TLS 握手過程中出現錯誤,如客戶端和服務器支持的 SSL/TLS 版本不兼容、加密算法不匹配等,可能會導致 Nginx 返回 400 錯誤。
- 服務器過載:當服務器負載過高時,可能無法及時處理客戶端的請求,從而返回 400 錯誤。可以通過優化服務器配置、增加硬件資源或使用負載均衡器來解決。
4.3、配置將HTTP請求重定向到HTTPS
#HTTPS配置
server {listen 443 ssl;ssl_certificate /usr/local/nginx/conf/ssl/gateway.crt;ssl_certificate_key /usr/local/nginx/conf/ssl/gateway.key;server_name 192.168.72.130;location / {root /opt/sumscope;}
}#將HTTP請求重定向到HTTPS
server {listen 80;server_name 192.168.72.130;return 301 https://$server_name$request_uri;
}#執行reload
nginx -s reload
?
4.3.1、訪問測試
瀏覽器輸入:192.168.72.130?
跳轉到如下頁面
(二)、TCPS配置
分別配置沒有證書(TCP)和有證書(TCPS)場景
#負載均衡場景1:配置證書
stream{upstream gateway{#hash $remote_addr consistent;server 192.168.72.130:2000;server 192.168.72.131:2000 backup;}server {listen 444 ssl;proxy_pass gateway;ssl_certificate /usr/local/nginx/conf/ssl/gateway.crt;ssl_certificate_key /usr/local/nginx/conf/ssl/gateway.key;proxy_connect_timeout 10s;proxy_timeout 300s;}#負載均衡場景2:沒有配置證書upstream stctcp{server 192.168.72.130:2000;server 192.168.72.131:2000 backup;}server {listen 9092;proxy_connect_timeout 10s;proxy_timeout 300s;proxy_pass stctcp;}
}