Docker部署Nginx HTTPS服務的實現(xiàn)步驟
更新時間:2026年01月09日 09:02:49 作者:tzhou64452
本文主要介紹了Docker部署Nginx HTTPS服務,實現(xiàn)HTTP到HTTPS的自動跳轉(zhuǎn),并解決中文亂碼、證書掛載、鏡像檢測等問題的步驟,具有一定的參考價值,感興趣的可以了解一下
一、文檔說明
1.1 適用環(huán)境
- 操作系統(tǒng):CentOS 7(64位)
- 核心目標:基于 Docker 部署 Nginx 服務,實現(xiàn) HTTP 自動跳轉(zhuǎn) HTTPS,解決中文亂碼、證書掛載、鏡像檢測等問題
- 前置條件:服務器以 root 用戶登錄,且能訪問外網(wǎng)(已手動拉取
nginx:1.21鏡像)
1.2 最終效果
- 訪問
http://192.168.10.110自動跳轉(zhuǎn)至https://192.168.10.110 - HTTPS 頁面正常顯示中文,無亂碼
- Nginx 容器開機自啟,配置/證書/日志持久化存儲
二、詳細部署步驟
步驟1:環(huán)境檢查與基礎準備
1.1 檢查用戶權限(必須 root)
# 驗證當前用戶是否為 root id -u # 輸出 0 表示為 root 用戶,否則切換 root:su root
1.2 安裝基礎工具(解決依賴)
# 安裝 lsof(端口檢測)、openssl(證書生成)等工具 yum install -y lsof openssl net-tools wget curl &>/dev/null
1.3 檢查端口可用性(80/443 必須空閑)
# 檢查 80 端口 lsof -i:80 # 檢查 443 端口 lsof -i:443 # 若端口被占用,關閉占用進程(示例:關閉 80 端口占用進程) kill -9 $(lsof -t -i:80)
步驟2:創(chuàng)建工作目錄(統(tǒng)一管理資源)
# 創(chuàng)建核心目錄(配置/證書/頁面/日志)
mkdir -p /opt/nginx-https/{conf,cert,wwwroot,logs}
# 設置目錄權限(避免容器掛載后權限不足)
chmod -R 755 /opt/nginx-https
步驟3:生成 SSL 自簽名證書
# 進入證書目錄 cd /opt/nginx-https/cert # 1. 生成 2048 位私鑰(無密碼) openssl genrsa -out nginx.key 2048 &>/dev/null # 2. 生成證書請求文件(無交互,適配主機 IP) openssl req -new -key nginx.key -out nginx.csr -subj "/C=CN/ST=Beijing/L=Beijing/O=Test/OU=IT/CN=192.168.10.110" &>/dev/null # 3. 生成自簽名證書(有效期 365 天) openssl x509 -req -days 365 -in nginx.csr -signkey nginx.key -out nginx.crt &>/dev/null # 4. 驗證證書文件(必須存在 nginx.key 和 nginx.crt) ls -l /opt/nginx-https/cert/ # 輸出如下則成功: # -rw-r--r-- 1 root root 1027 12月 28 23:00 nginx.crt # -rw-r--r-- 1 root root 1086 12月 28 23:00 nginx.csr # -rw-r--r-- 1 root root 1675 12月 28 23:00 nginx.key # 5. 設置證書權限(Nginx 需讀取權限) chmod 644 /opt/nginx-https/cert/nginx.key /opt/nginx-https/cert/nginx.crt
步驟4:編寫 Nginx HTTPS 配置文件
4.1 創(chuàng)建配置文件
vim /opt/nginx-https/conf/nginx.conf
4.2 粘貼完整配置(含 UTF-8 編碼,解決中文亂碼)
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
# 核心:解決中文亂碼,全局指定 UTF-8 編碼
charset utf-8;
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
# HTTP 服務:自動跳轉(zhuǎn) HTTPS
server {
listen 80;
server_name 192.168.10.110;
return 301 https://$host$request_uri;
}
# HTTPS 核心配置
server {
listen 443 ssl;
server_name 192.168.10.110;
# 證書掛載路徑(容器內(nèi)路徑,對應宿主機 /opt/nginx-https/cert)
ssl_certificate /etc/nginx/cert/nginx.crt;
ssl_certificate_key /etc/nginx/cert/nginx.key;
# SSL 優(yōu)化配置
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
# 靜態(tài)頁面目錄(掛載宿主機 /opt/nginx-https/wwwroot)
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
# 錯誤頁面配置
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
}
4.3 驗證配置語法(關鍵:掛載證書目錄驗證)
# 用容器內(nèi) Nginx 驗證配置(同時掛載配置和證書目錄) docker run --rm \ -v /opt/nginx-https/conf/nginx.conf:/etc/nginx/nginx.conf \ -v /opt/nginx-https/cert:/etc/nginx/cert \ nginx:1.21 nginx -t # 輸出如下則配置正確: # nginx: the configuration file /etc/nginx/nginx.conf syntax is ok # nginx: configuration file /etc/nginx/nginx.conf test is successful
步驟5:創(chuàng)建測試頁面(解決中文亂碼)
# 編輯測試頁面,指定 UTF-8 編碼 vim /opt/nginx-https/wwwroot/index.html
粘貼以下內(nèi)容:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<h1>Nginx HTTPS 部署成功!</h1>
<p>主機IP:192.168.10.110</p>
</body>
</html>
設置頁面權限:
chmod 644 /opt/nginx-https/wwwroot/index.html
步驟6:啟動 Nginx 容器
6.1 清理舊容器(避免沖突)
# 停止并刪除同名舊容器(若存在) docker stop nginx-https &>/dev/null docker rm nginx-https &>/dev/null
6.2 啟動新容器(完整掛載所有目錄)
docker run -d \ --name nginx-https \ --privileged=true \ -p 80:80 \ -p 443:443 \ -v /opt/nginx-https/conf/nginx.conf:/etc/nginx/nginx.conf \ -v /opt/nginx-https/cert:/etc/nginx/cert \ -v /opt/nginx-https/wwwroot:/usr/share/nginx/html \ -v /opt/nginx-https/logs:/var/log/nginx \ --restart=always \ nginx:1.21
參數(shù)說明:
| 參數(shù) | 作用 |
|---|---|
| --name nginx-https | 容器命名為 nginx-https |
| --privileged=true | 提升容器權限,解決掛載目錄權限不足問題 |
| -p 80:80/-p 443:443 | 宿主機端口映射到容器端口 |
| -v 宿主機路徑:容器路徑 | 目錄掛載,實現(xiàn)配置/證書/頁面/日志持久化(修改宿主機文件無需重建容器) |
| --restart=always | 容器隨 Docker 開機自啟 |
6.3 驗證容器啟動狀態(tài)
# 查看容器是否運行(狀態(tài)為 Up 則成功) docker ps | grep nginx-https # 輸出示例: # abc123456789 nginx:1.21 "/docker-entrypoint.…" 10 seconds ago Up 9 seconds 0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp nginx-https
三、驗證部署結(jié)果
3.1 命令行驗證
3.1.1 驗證 HTTP 自動跳轉(zhuǎn)
curl -I http://192.168.10.110 # 輸出 301 跳轉(zhuǎn)則成功: # HTTP/1.1 301 Moved Permanently # Location: https://192.168.10.110/
3.1.2 驗證 HTTPS 訪問(忽略自簽名證書)
curl -k https://192.168.10.110 # 輸出測試頁面 HTML 內(nèi)容則成功(無亂碼)
3.2 瀏覽器驗證
- 打開瀏覽器,訪問
https://192.168.10.110; - 忽略“不安全連接”警告(自簽名證書正常現(xiàn)象);
- 頁面顯示“Nginx HTTPS 部署成功!主機IP:192.168.10.110”,中文無亂碼則完成。

四、常見問題排查
4.1 鏡像檢測失敗
現(xiàn)象:提示“nginx:1.21 鏡像不存在”,但已拉??;
解決:用格式化命令驗證鏡像:
docker images --format "{{.Repository}}:{{.Tag}}" | grep nginx:1.21
# 輸出 nginx:1.21 則鏡像存在,重新執(zhí)行啟動命令即可
4.2 證書文件不存在
現(xiàn)象:Nginx 配置測試提示“cannot load certificate”;
解決:
# 檢查證書文件是否存在 ls -l /opt/nginx-https/cert/ # 重新生成證書(步驟3),確保 nginx.key 和 nginx.crt 存在
4.3 中文亂碼
現(xiàn)象:頁面中文顯示為方框/亂碼;
解決:
- 確認測試頁面添加
<meta charset="UTF-8">; - 確認 Nginx 配置
http塊內(nèi)添加charset utf-8; - 重啟容器:
docker restart nginx-https。
4.4 端口占用
現(xiàn)象:容器啟動失敗,提示端口被占用;
解決:
# 查找占用端口的進程 lsof -i:80 # 關閉進程(示例) kill -9 $(lsof -t -i:80)
五、日常運維命令
5.1 容器啟停/重啟
# 啟動 docker start nginx-https # 停止 docker stop nginx-https # 重啟(配置修改后生效) docker restart nginx-https
5.2 查看日志
# 查看容器啟動日志 docker logs nginx-https # 查看 Nginx 訪問日志 cat /opt/nginx-https/logs/access.log # 查看 Nginx 錯誤日志 cat /opt/nginx-https/logs/error.log
5.3 修改配置后生效
# 1. 修改宿主機配置文件 vim /opt/nginx-https/conf/nginx.conf # 2. 驗證配置語法 docker run --rm -v /opt/nginx-https/conf/nginx.conf:/etc/nginx/nginx.conf -v /opt/nginx-https/cert:/etc/nginx/cert nginx:1.21 nginx -t # 3. 重啟容器 docker restart nginx-https
5.4 刪除容器(如需重裝)
docker stop nginx-https && docker rm nginx-https # 可選:刪除工作目錄(謹慎操作) rm -rf /opt/nginx-https
到此這篇關于Docker部署Nginx HTTPS服務的實現(xiàn)步驟的文章就介紹到這了,更多相關Docker部署Nginx HTTPS服務內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:
相關文章
使用Docker部署打包發(fā)布springboot項目
本文主要介紹了使用Docker部署打包發(fā)布springboot項目,從安裝docker到多種方式打包發(fā)布,編譯,鏡像,容器等問題,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03

