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

Nginx与Docker配置教程:如何将请求转发到内部私有地址服务

配置方案

第一步:调整Docker Compose配置

按以下要求修改invoiceninja的docker-compose配置:

  • 端口映射指定绑定内网IP,避免公网暴露,不需要对外提供服务的场景下也可以直接删除ports配置,仅保留expose参数,让服务仅能被同Docker网络内的NGINX访问
  • 确保NGINX容器和invoiceninja容器加入同一个Docker网络,这样NGINX可以直接通过容器名访问invoiceninja服务
  • 修改invoiceninja环境变量内的APP_URL参数,避免服务内部跳转异常

修改后的参考配置:

invoiceninja:
  container_name: invoiceninja
  image: invoiceninja/invoiceninja:latest
  # 不需要直接对外暴露端口的话可以删除ports配置,仅保留expose
  expose:
    - 80
  # 若需要直接通过Docker端口映射访问,保留以下配置,绑定内网IP避免公网暴露
  # ports:
  #   - 192.168.123.1:10000:80
  restart: always
  volumes:
    - /storage/appdata/invoiceninja/public:/var/app/public
    - /storage/appdata/invoiceninja/storage:/var/app/storage
  networks:
    - invoiceninja
    # 把NGINX所在的Docker网络也添加到此处,或者在NGINX的配置里添加invoiceninja网络
    - nginx-network
  env_file:
    - .secrets/invoiceninja.env
  depends_on:
    - invoiceninja-db

同时打开.secrets/invoiceninja.env文件,修改APP_URL参数:

APP_URL=http://192.168.123.1:10000

第二步:调整NGINX配置

在你的nginx.conf中新增一个单独的server块,专门处理内网10000端口的转发请求,不需要配置真实域名:

# 内网Invoice Ninja服务
server {
    # 绑定内网IP监听,避免公网暴露该端口,不需要限制的话也可以直接写listen 10000;
    listen 192.168.123.1:10000;
    server_name _;

    client_max_body_size 100M;

    location / {
        proxy_send_timeout 300s;
        proxy_read_timeout 300s;
        proxy_buffering off;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        # 直接通过Docker容器名转发到invoiceninja的80端口
        proxy_pass http://invoiceninja:80;
    }
}

第三步:生效验证

  • 重启invoiceninja容器:docker compose up -d invoiceninja
  • 校验NGINX配置合法性:nginx -t
  • 重载NGINX配置:nginx -s reload
  • 在内网环境访问http://192.168.123.1:10000即可正常使用服务

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.02 17:15:03