背景:事情的起因是我在国外的 VPS 上绑定了一个域名,但该域名在联通网络环境下,全国范围内都无法正常解析。但是呢...域名用的三级域名,不能托管到CF做cdn。
措施:手上呢有另一台vps,可以做中转用。
材料:德国VPS(api.*.*.asia)这台ip是185.*、香港VPS(app.*.*.asia)这台是154.*
在域名解析里相同域名配置两个解析值

在154这台vps上配置反向代理,代理到185这台机器。

那么配置到这一步,已经可以使用app.*.*.asia访问到api.*.*.asia了。但是,因为我有些项目是硬编码api地址的,改起来很麻烦,所以干脆一不做二不休,直接让流量多跑一圈。
国内运营商->app.*.*.asia->api.*.*.asia。增加一层:国内运营商 -> api.*.*.asia -> app.*.*.asia(中转代理)-> 154.*(目标服务器)
于是就有了这个踩坑笔记。
使用 OpenResty Docker 容器配置泛域名证书与反向代理
背景
我在 1Panel 平台中,使用 Docker 部署了 OpenResty(Nginx 变体),希望通过泛域名证书支持两个子域名:
app.*.*.asia—— 正常网站服务api.*.*.asia—— 反向代理到内网服务185.*:30000
两者均要求使用 HTTPS 访问,证书文件均存放于宿主机路径映射的 /www/sites/api.1p.25612.asia/ssl/ 目录。
遇到的问题
1. 访问 api.*.*.asia 提示证书错误,浏览器报 HSTS 错误
错误内容大致为:
api.*.*.asia 使用加密来保护你的信息。
网站发回了不正常和不正确的凭据。
由于 HSTS 策略,浏览器拒绝继续访问。
2. OpenResty 启动时报错:
cannot load certificate "/www/sites/api.*.asia/ssl/fullchain.pem": BIO_new_file() failed
提示无法找到指定路径的证书文件。
诊断思路
错误指向
/www/sites/api.25612.asia/ssl/fullchain.pem路径,但实际证书在/www/sites/api.*.*.asia/ssl/。说明 OpenResty 配置中某些地方仍引用旧路径,导致启动失败。
多个配置文件监听同一端口 443,导致配置冲突警告。
需要确认所有证书路径配置、日志目录路径是否正确且存在。
关键排查步骤
1. 进入 Docker 容器,检查配置文件
docker exec -it <container_id> /bin/sh
grep -r "api.*.asia" /usr/local/openresty/nginx/conf/conf.d/
找到所有引用旧路径的配置文件。
2. 批量替换旧路径为正确路径
sed -i 's|/www/sites/api.*.asia/|/www/sites/api.*.*.asia/|g' /usr/local/openresty/nginx/conf/conf.d/*.conf
3. 确认日志目录存在
确保路径 /www/sites/api.*.*.asia/log/ 存在且有写权限,防止日志写入失败。
4. 避免监听端口冲突
检查是否有多个配置监听 listen 443 ssl:
grep -r "listen 443 ssl" /usr/local/openresty/nginx/conf/conf.d/
如果存在冲突,考虑合并配置或者修改监听端口。
5. 测试配置并重载
openresty -t
openresty -s reload
确认配置语法正确且服务正常启动。
最终 nginx 配置示例
# HTTP 跳转到 HTTPS
server {
listen 80;
server_name app.*.*.asia api.*.*.asia;
return 301 https://$host$request_uri;
}
# app.1p.25612.asia 网站配置
server {
listen 443 ssl;
http2;
server_name app.1p.25612.asia;
ssl_certificate /www/sites/api.*.*.asia/ssl/fullchain.pem;
ssl_certificate_key /www/sites/api.*.*.asia/ssl/privkey.pem;
root /www/sites/app.*.*.asia/html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ^~ /.well-known {
allow all;
root /usr/share/nginx/html;
}
access_log /www/sites/app.*.*.asia/log/access.log main;
error_log /www/sites/app.*.*.asia/log/error.log;
add_header Strict-Transport-Security "max-age=31536000" always;
}
# api.1p.25612.asia 反向代理配置
server {
listen 443 ssl;
http2;
server_name api.*.*.asia;
ssl_certificate /www/sites/api.*.*.asia/ssl/fullchain.pem;
ssl_certificate_key /www/sites/api.*.*.asia/ssl/privkey.pem;
location / {
proxy_pass http://185.*.*.*:30000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_http_version 1.1;
proxy_set_header Connection "";
}
access_log /www/sites/api.*.*.asia/log/access.log main;
error_log /www/sites/api.*.*.asia/log/error.log;
add_header Strict-Transport-Security "max-age=31536000" always;
}
经验总结
证书路径一定要准确,容器内路径和宿主机挂载路径需严格对应。
多个配置文件监听同端口时,避免重复定义协议选项(
http2)导致警告。日志目录不存在会导致 Nginx 启动失败,务必提前创建。
使用
openresty -t测试配置文件语法,避免启动时报错。利用
grep和sed命令批量定位和修正配置文件中的路径错误。