目录
✅ Why Use Nginx in Docker?
?️ Basic Setup: Static Site Nginx
? Reverse Proxy to Another Container (e.g., Node.js App)
? Pro Tips
✅ TL;DR
首页 运维 nginx 将nginx与Docker一起使用

将nginx与Docker一起使用

Jul 28, 2025 am 01:16 AM

使用Docker中的Nginx可实现隔离、可重复、可扩展和灵活的部署;2. 通过自定义nginx.conf并COPY到镜像中,可快速运行静态网站;3. 在docker-compose中用服务名作为upstream主机,Nginx能反向代理到其他容器(如Node.js);4. 动态更新配置时应挂载文件、先测试再重载以确保安全。搞定后即可构建高效稳定的容器化Web服务。

Using Nginx with Docker

Using Nginx with Docker is a common and powerful setup — whether you're serving static files, reverse proxying to backend apps (like Node.js, Python, or PHP), or load balancing across multiple containers. Here's how to do it right, simply and effectively.

Using Nginx with Docker

✅ Why Use Nginx in Docker?

  • Isolation: Nginx runs in its own container, separate from your app.
  • Reproducibility: Same config everywhere — dev, staging, prod.
  • Scalability: Easily scale your app containers behind Nginx.
  • Flexibility: Swap backends, add SSL, or tweak configs without touching host systems.

?️ Basic Setup: Static Site Nginx

1. Create your static site folder

project/
├── nginx.conf
├── Dockerfile
└── html/
    └── index.html

2. Custom nginx.conf (optional but recommended)

Using Nginx with Docker
events {
    worker_connections 1024;
}

http {
    server {
        listen 80;
        root /usr/share/nginx/html;
        index index.html;

        location / {
            try_files $uri $uri/ =404;
        }
    }
}

3. Dockerfile

FROM nginx:alpine
COPY nginx.conf /etc/nginx/nginx.conf
COPY html /usr/share/nginx/html

4. Build & Run

Using Nginx with Docker
docker build -t my-nginx-site .
docker run -d -p 8080:80 my-nginx-site

Now visit http://localhost:8080 — your static site is live!


? Reverse Proxy to Another Container (e.g., Node.js App)

Say you have a Node.js app on port 3000 in another container.

Update nginx.conf:

http {
    upstream nodeapp {
        server node-app:3000;  # 'node-app' is the service name in docker-compose
    }

    server {
        listen 80;

        location / {
            proxy_pass http://nodeapp;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
        }
    }
}

Use docker-compose.yml:

version: '3.8'
services:
  nginx:
    build: .
    ports:
      - "80:80"
    depends_on:
      - node-app

  node-app:
    image: my-node-app:latest
    expose:
      - "3000"

This lets Nginx route traffic to your Node.js app — all inside Docker, no host ports needed except 80.


? Pro Tips

  • Use named volumes or bind mounts for dynamic config updates without rebuilding:
    docker run -v ./nginx.conf:/etc/nginx/nginx.conf:ro nginx
  • Test config before reload:
    docker exec <nginx-container> nginx -t
  • Reload config without restart:
    docker exec <nginx-container> nginx -s reload

    ✅ TL;DR

    • Use a custom nginx.conf for control.
    • Mount or copy static files into the image.
    • In docker-compose, use service names as upstream hosts.
    • Always test and reload Nginx config safely in containers.

    Basically just: build → link → run → profit. Nginx Docker = ? when done right.

    以上是将nginx与Docker一起使用的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

热门话题

PHP教程
1596
276
配置NGINX超时 配置NGINX超时 Aug 03, 2025 pm 04:25 PM

proxy_connect_timeout设为5–10秒,确保快速失败;2.proxy_send_timeout设为10–30秒,适应慢速上传;3.proxy_read_timeout匹配应用最长响应时间,避免504错误;4.若负载均衡,可设proxy_next_upstream_timeout限制重试时长——正确配置这些值能显着减少网关超时、提升用户体验,并需结合实际日志和监控持续调优。

Server_name指令做什么? Server_name指令做什么? Aug 02, 2025 pm 04:49 PM

Nginx中的server_name指令用于根据客户端发送的Host头选择处理请求的虚拟主机。具体来说:1.server_name通过精确匹配、通配符或正则表达式匹配Host头,决定使用哪个server块;2.未匹配时会回退到默认server块,通常是第一个或显式标记为default_server的块;3.正确配置server_name有助于避免内容重复、提升SEO并增强性能;4.复杂匹配和通配符应谨慎使用,以保持清晰性和效率。因此,合理设置server_name能确保流量正确路由并简化服务器维

Nginx URL重写和重定向 Nginx URL重写和重定向 Aug 01, 2025 am 03:48 AM

Redirects(301/302)changethebrowserURLandareSEO-friendlyformovedcontent;rewritesinternallymapURLswithoutbrowserredirection.2.Usereturn301forfast,clearredirectslikeforcingHTTPS,redirectingwww,ormovingoldpathstonewones.3.Userewritewithlastorbreakinlocat

nginx for node.js应用程序 nginx for node.js应用程序 Aug 01, 2025 am 04:13 AM

nginxactsasAsareVerseProxy,HidingInternalportsandalling MultiplipliplipsonOnserver; 2. Ithlesssl/tlstermination效果效率lyvialet'sencrypt,offloadingIncryption fromNode fromnode; 3.ITServestTicaticFilesFilesFilesFasticFasterFasterFasterThannAnnOdeByDirectLyectlyectlyectlyectlyectlyectlymanagingRouteSlike/static/Static/Static/Statatic/Static;

nginx入口控制器的Kubernetes nginx入口控制器的Kubernetes Aug 02, 2025 am 09:21 AM

NginxIngressController是Kubernetes中实现HTTP/HTTPS路由、负载均衡、SSL终止、重写和限流的核心组件,1.可基于主机名或路径将请求转发到对应Service;2.支持通过Secret配置TLS/SSL实现HTTPS;3.利用ConfigMap和annotations提供灵活配置如重写和限流;4.部署推荐Helm或官方YAML;5.注意pathType匹配规则、后端服务健康状态、全局配置与日志监控,它是生产环境中稳定可靠的流量入口方案。

NGINX安装指南 NGINX安装指南 Jul 31, 2025 am 08:50 AM

在Ubuntu/Debian上安装Nginx需更新包列表(sudoaptupdate)、安装Nginx(sudoaptinstallnginx-y)、启动并启用服务(sudosystemctlstart/enablenginx);2.在CentOS/RHEL上需启用EPEL源(sudodnfinstallepel-release-y)、安装Nginx、启动服务,并开放防火墙HTTP/HTTPS端口(firewall-cmd命令);3.安装后应验证配置语法(sudonginx-t)、检查默认站点目

使用SystemD管理NGINX 使用SystemD管理NGINX Aug 01, 2025 am 07:15 AM

使用systemctlstatusnginx检查Nginx服务状态,确认是否运行及开机自启;2.掌握start、stop、restart、reload、enable、disable等核心命令,优先用reload避免连接中断;3.用journalctl-unginx.service查看日志,-f参数可实时监控,便于排查启动失败问题;4.修改配置前务必运行sudonginx-t测试语法,防止reload失败;5.如需自定义配置,使用sudosystemctleditnginx创建安全覆盖文件而非直接

nginx中的动态模块 nginx中的动态模块 Aug 03, 2025 am 12:49 AM

DynamicModules是Nginx从1.9.11引入的特性,允许运行时加载.so模块而非重编译;1.确认模块支持动态编译(如--add-dynamic-module);2.在nginx.conf顶部用load_module指令加载.so文件;3.验证配置并reload生效;优势为热插拔、易升级、适配容器化,需注意版本匹配、路径正确、无法热卸载及第三方模块安全问题。

See all articles