当前位置: 代码网 > it编程>开发工具>Docker > Nginx Docker容器化安装的实现

Nginx Docker容器化安装的实现

2026年08月28日 Docker 我要评论
一、引言nginx的部署方式有多种:编译安装、yum安装、rpm安装、docker安装等。这里讲述常用的容器化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容器化 内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2026  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com