You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Nginx环境下HTTP转HTTPS重定向配置问题咨询

我来帮你梳理下需要调整的几个关键配置,搞定HTTP到HTTPS的重定向,同时避开循环跳转和应用内部链接的坑:

1. 修复Nginx配置中的循环重定向问题

你当前的443 server块里,proxy_pass http://localhost:80;会导致无限循环——因为Nginx容器内的localhost指向容器自身,请求会再次触发80端口的重定向规则。我们需要把代理目标改成Docker网络内Bookstack服务的地址(同一网络下可以直接用服务名访问)。

修改nginx.conf的443 server块中location /部分:

location / {
    # 替换原localhost:80为Bookstack服务的容器端口
    proxy_pass http://bookstack:8080;
    # 移除不必要的proxy_ssl配置(目标是HTTP服务,无需SSL验证)
    # proxy_ssl_certificate /etc/nginx/ssl.crt;
    # proxy_ssl_certificate_key /etc/nginx/ssl.key;
    # proxy_ssl_verify off;
    allow all;
    proxy_redirect off;
    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-Host $server_name;
    proxy_set_header X-Forwarded-Proto https;
    # 保留原有的超时、缓冲配置
    client_max_body_size 0;
    client_body_buffer_size 128k;
    proxy_connect_timeout 1200s;
    proxy_send_timeout 1200s;
    proxy_read_timeout 1200s;
    proxy_buffers 32 4k;
}
2. 更新Bookstack的APP_URL为HTTPS地址

APP_URL决定了应用生成的所有内部链接(页面跳转、资源引用)的协议,必须改成HTTPS,否则会出现混合内容错误,或者应用内部跳转回HTTP。

修改docker-compose.yml中bookstack服务的环境变量:

environment:
  # 替换原HTTP地址为HTTPS地址
  - APP_URL=https://localhost
3. 完善Nginx容器的Docker Compose配置

原nginx服务缺少端口映射和配置/证书挂载,导致无法对外提供80/443服务,也无法读取自定义配置和SSL证书。补充以下配置(假设你本地有./nginx目录,存放nginx.conf、ssl.crt、ssl.key):

nginx:
  image: nginx:latest
  container_name: bookstack-nginx
  restart: always
  # 映射宿主机80/443端口到容器
  ports:
    - "80:80"
    - "443:443"
  # 挂载自定义配置和SSL证书到容器内对应路径
  volumes:
    - ./nginx/nginx.conf:/etc/nginx/nginx.conf
    - ./nginx/ssl.crt:/etc/nginx/ssl.crt
    - ./nginx/ssl.key:/etc/nginx/ssl.key
  # 加入Bookstack所在的网络,才能访问bookstack服务
  networks:
    - bookstack-bridge
4. 生成本地测试用的SSL证书(可选)

如果是本地测试,你需要生成自签名SSL证书放在./nginx目录下:

# 在本地./nginx目录执行
openssl req -x509 -newkey rsa:4096 -keyout ssl.key -out ssl.crt -days 365 -nodes -subj "/CN=localhost"

最后重启所有服务让配置生效:

docker-compose down && docker-compose up -d

这样修改后,访问http://localhost会被301重定向到https://localhost,Nginx会代理请求到Bookstack容器,同时应用内部所有链接都会使用HTTPS,避免混合内容问题。

内容的提问来源于stack exchange,提问作者Jon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 10:22:55