Nginx Docker容器化安装的实现

 更新时间:2026年08月28日 09:13:23   作者:痕迹运维  
本文主要介绍Nginx Docker容器化安装的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

一、引言

Nginx的部署方式有多种:编译安装、YUM安装、RPM安装、Docker安装等。这里讲述常用的容器化Docker部署方式,也是推荐大家生产环境使用的部署方式,方便后续升级(替换镜像即可)。

二、详解

1、创建目录

>> mkdir -p /opt/software/nginx/{logs,conf.d,stream,html,web,certs}
>> cd /opt/software/nginx/

2、创建主配置文件nginx.conf

>> vim nginx.conf  # 主配置存放通用配置,无需修改,直接复制粘贴即可

user  nginx;
worker_processes auto;
worker_rlimit_nofile 10240;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    use epoll;
    worker_connections  10240;
}

stream {
   log_format proxy '$remote_addr [$time_local] '
                 '$protocol $status $bytes_sent $bytes_received '
                 '$session_time "$upstream_addr" '
                 '"$upstream_bytes_sent" "$upstream_bytes_received" "$upstream_connect_time"';
    access_log  /var/log/nginx/stream.log  proxy;
    include /etc/nginx/stream/*.conf;

}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" "$content_type"'
                      '$scheme "$ssl_protocol/$ssl_cipher"'
                      '$status $body_bytes_sent $request_body "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"'
                      '$upstream_addr $upstream_response_time $request_time "$scheme://$host$request_uri" $http_cookie';
                      
    log_format common '${time_local} | x-real-ip:${remote_addr} | x-forward-for:"$proxy_add_x_forwarded_for" '
                      '| server_name:"${server_name}" | URI:"$request" | statusCode:$status '
                      '| referer:"${http_referer}" | upstream_addr:"${upstream_addr}" '
                      '| upstream_connect_time: "$upstream_connect_time" | upstream_response_time: "$upstream_response_time"';

    access_log  /var/log/nginx/access.log  common;

    add_header Cache-Control  no-store;
    add_header Pragma no-cache;
    server_tokens off;
    add_header X-Frame-Options SAMEORIGIN;
    add_header X-Content-Type-Options nosniff;
    add_header autocomplete off;
    limit_req_zone $binary_remote_addr zone=mylimit:10m rate=300r/s;

    sendfile        on;

    keepalive_timeout  65;
    open_file_cache max=10240 inactive=20s;
    open_file_cache_valid 30s;
    open_file_cache_min_uses 1;

    client_body_timeout 60s;
    client_header_timeout 60s;
    send_timeout 60s;
    client_header_buffer_size 1m;
    large_client_header_buffers 4 1024k;
    client_max_body_size 150m;

    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 32k;
    gzip_http_version 1.1;
    gzip_comp_level 6;
    gzip_types text/css text/xml application/javascript;
    gzip_vary on;

    map $http_upgrade $connection_upgrade {
      default upgrade;
      '' close;
    }

     include /etc/nginx/conf.d/*.conf;
}

3、创建子配置文件

子配置文件用于配置实际的代理服务

>> vim conf.d/proxy.conf  # 通用配置,可根据实际修改

proxy_read_timeout 300s;
proxy_send_timeout 300s;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# 允许协议升级为 WebSocket
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;

>> vim conf.d/appserver.conf  # 存放实际的代理配置,根据具体项目调整即可,这里只是示例

# 定义负载均衡主机组。可配置多个后端主机
upstream gateway {
    ip_hash;
    server 192.168.25.131:8070 max_fails=2 fail_timeout=15s;  
}

upstream web {
    ip_hash;
    server 192.168.25.131:8090 max_fails=2 fail_timeout=15s;  
}

server {
	listen   8080;
    server_name  192.168.25.130;

    root /etc/nginx/html;
    index index.html index.htm;

    # 前端代理
    location /hospital-cloud/data-web {
        try_files  $uri $uri/  /hospital-cloud/data-web/index.html;
        root /var/www;
        index  index.html index.htm;
    }
   
    # 后端代理
    location /hospital-cloud/hospital-web/ {
        proxy_pass http://web/;
        include /etc/nginx/conf.d/proxy.conf;
    }

    location /hospital-ledger-gateway/ {
         proxy_pass http://gateway/;
         include /etc/nginx/conf.d/proxy.conf;
    }

    location / {
         proxy_pass http://gateway/;
         include /etc/nginx/conf.d/proxy.conf;
    }
}

4、创建docker-compose.yml

>> vim docker-compose.yml

---
services:
  nginx:
    image: nginx:1.31.0
    container_name: nginx
    restart: unless-stopped
    user: root
    network_mode: host

    volumes:
      - ./stream/:/etc/nginx/stream/:ro
      - ./nginx.conf:/etc/nginx/nginx.conf:ro
      - ./conf.d/:/etc/nginx/conf.d/:ro
      - ./certs/:/etc/nginx/certs/:ro
      - ./logs/:/var/log/nginx/
      - ./html/:/etc/nginx/html/
      - ./web/:/var/www/hospital-cloud/
      - /etc/localtime:/etc/localtime:ro
      
    environment:
      TZ: Asia/Shanghai
...

5、启动Nginx

>> docker-compose up -d

到此这篇关于Nginx Docker容器化安装的实现的文章就介绍到这了,更多相关Nginx Docker容器化 内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 深入浅析nginx部署及简单优化

    深入浅析nginx部署及简单优化

    Nginx是lgor Sysoev为俄罗斯访问量第二的rambler.ru站点设计开发的。本文重点给大家介绍nginx部署及简单优化方案,感兴趣的朋友一起看看吧
    2018-08-08
  • nginx设置上传目录无执行权限的方法

    nginx设置上传目录无执行权限的方法

    在windows+iis下,可以设置上传目录,类似:upload,uploadfile,attachments,这样的目录下面无脚本执行权限,从而防止非法用户上传脚本得到webshell
    2010-11-11
  • 一句简单命令重启nginx

    一句简单命令重启nginx

    最近我的多个VPS经常出现502错误,经常需要重启nginx,但网上的很多教程都需要繁琐的启动脚本,远不如apache的重启命令那么简单。
    2010-03-03
  • Nginx扩容的几种使用方法小结

    Nginx扩容的几种使用方法小结

    本文介绍了Nginx扩容的几种使用方法小结,从负载均衡器配置到自身扩容,详细讲解DNS轮询、云负载均衡器和LVS+Keepalived方案,并分享会话共享和健康检查等优化技巧,帮你构建高可用集群
    2026-08-08
  • Linux中Nginx反向代理如何实现不同路径访问不同的页面

    Linux中Nginx反向代理如何实现不同路径访问不同的页面

    这篇文章主要介绍了Linux中Nginx反向代理如何实现不同路径访问不同的页面方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-12-12
  • 本地HTTPS环境搭建的完整步骤记录

    本地HTTPS环境搭建的完整步骤记录

    这篇文章主要给大家介绍了关于本地HTTPS环境搭建的相关资料,文中通过示例代码将实现的步骤一步步介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友下面随着小编来一起学习学习吧
    2018-05-05
  • nginx多https证书配置实现

    nginx多https证书配置实现

    本文主要介绍了nginx多https证书配置实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-04-04
  • Nginx中部署Angular项目遇到的坑巨坑

    Nginx中部署Angular项目遇到的坑巨坑

    这篇文章主要介绍了Nginx中部署Angular项目遇到的坑巨坑,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-10-10
  • Nginx安装及启动全过程

    Nginx安装及启动全过程

    文章简要介绍了Nginx的安装、编译、配置及配置模块等内容,包括配置Nginx的访问地址、依赖组件、编译路径以及服务管理配置等,同时详细说明了Nginx模块的作用和配置选项,帮助用户顺利搭建Nginx服务器
    2025-09-09
  • 在Nginx中增加对OAuth协议的支持的教程

    在Nginx中增加对OAuth协议的支持的教程

    这篇文章主要介绍了在Nginx中增加对OAuth协议的支持的教程,OAuth协议如今广泛用于社交网络的API中,需要的朋友可以参考下
    2015-06-06

最新评论