不用cerbot和acme.sh讓nginx實現(xiàn)類似caddy一樣自動獲取和續(xù)期的ssl證書
內(nèi)容由cursor生成、
簡短結(jié)論:Nginx 本體沒有像 Caddy 那樣“內(nèi)置 ACME 自動簽發(fā)/續(xù)期”的官方功能;不使用 certbot/acme.sh 的情況下,最成熟可用的做法是用 OpenResty(帶 Lua)+ lua-resty-auto-ssl,在 Nginx 層內(nèi)自動申請和續(xù)期證書。
給你一個最小可用的方案(RHEL/OpenCloudOS 系):
- 安裝 OpenResty 和自動證書組件
# 安裝 OpenResty 官方倉庫 sudo dnf install -y yum-utils sudo yum-config-manager --add-repo https://openresty.org/package/openresty.repo sudo dnf install -y openresty openresty-resty # 安裝 lua-resty-auto-ssl(OPM) sudo /usr/local/openresty/bin/opm get ledgetech/lua-resty-auto-ssl
- 創(chuàng)建存儲目錄并授權(quán)
sudo mkdir -p /etc/resty-auto-ssl
sudo chown -R nobody:nobody /etc/resty-auto-ssl
- 示例配置(/usr/local/openresty/nginx/conf/nginx.conf)
僅示意關(guān)鍵段,按你的路徑/域名調(diào)整。
```nginx
worker_processes auto; events { worker_connections 1024; } http { lua_shared_dict auto_ssl 64m; lua_shared_dict auto_ssl_settings 64k; # 用于 OCSP/ACME 解析 resolver 8.8.8.8 1.1.1.1 ipv6=off; init_by_lua_block { local auto_ssl = require("resty.auto-ssl").new() auto_ssl:set("dir", "/etc/resty-auto-ssl") auto_ssl:set("allow_domain", function(domain) -- 白名單,避免被濫用。寫上你的域名: return (domain == "example1.com" or domain == "www.example1.com" or domain == "example2.com" or domain == "www.example2.com") end) auto_ssl:init() } init_worker_by_lua_block { require("resty.auto-ssl").init_worker() } # ACME 挑戰(zhàn)回調(diào)服務(wù)(必需) server { listen 127.0.0.1:8999; location / { content_by_lua_block { require("resty.auto-ssl").challenge_server() } } } # 80 端口:需放通給 ACME HTTP-01 挑戰(zhàn) server { listen 80 default_server reuseport; server_name _; location /.well-known/acme-challenge/ { content_by_lua_block { require("resty.auto-ssl").challenge_server() } } # 其他全部 301 到 https(可選) location / { return 301 https://$host$request_uri; } } # 443 端口:自動證書 server { listen 443 ssl http2 reuseport; server_name example1.com www.example1.com example2.com www.example2.com; # 由 auto-ssl 動態(tài)提供證書(關(guān)鍵) ssl_certificate_by_lua_block { require("resty.auto-ssl").ssl_certificate() } # 兜底自簽證書(首次握手前需要),auto-ssl 安裝包自帶: ssl_certificate /usr/local/openresty/nginx/conf/ssl/resty-auto-ssl-fallback.crt; ssl_certificate_key /usr/local/openresty/nginx/conf/ssl/resty-auto-ssl-fallback.key; # 你自己的站點 upstream/靜態(tài)根/反代等: # 站點1 if ($host = example1.com) { set $up backend1; } if ($host = www.example1.com) { set $up backend1; } if ($host = example2.com) { set $up backend2; } if ($host = www.example2.com) { set $up backend2; } # 示例:靜態(tài)根(按需切換成 proxy_pass/PHP 等) set $root ""; if ($up = backend1) { set $root /www/wwwroot/example1.com; } if ($up = backend2) { set $root /www/wwwroot/example2.com; } root $root; index index.html index.htm; location / { try_files $uri $uri/ =404; } } }
- 啟動/重載
```bash
sudo systemctl enable --now openresty
sudo systemctl status openresty | cat
要點
- 必須開放 80/443,并且域名 DNS 指向此機(jī),ACME 才能通過 HTTP-01 驗證。
- `allow_domain` 里務(wù)必列白名單,防止被人借你服務(wù)器代簽證書。
- 證書獲取與續(xù)期全自動,無需 certbot/acme.sh。
- 若你已有純 Nginx,可改為 OpenResty(完全兼容大多數(shù) Nginx 配置);或為現(xiàn)有 Nginx 編譯 Lua 模塊并接入,但工作量更大,建議直接用 OpenResty。
如果你貼出當(dāng)前 Nginx 的站點配置(靜態(tài)/反代/PHP),我可以把上面 443 段替換成與你站點匹配的版本。

浙公網(wǎng)安備 33010602011771號