Nginx Docker容器化安装

一、引言

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

二、详解

1、创建目录

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

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

bash 复制代码
>> vim nginx.conf  # 主配置存放通用配置,无需修改,直接复制粘贴即可
ini 复制代码
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、创建子配置文件

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

bash 复制代码
>> vim conf.d/proxy.conf  # 通用配置,可根据实际修改
ini 复制代码
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;
bash 复制代码
>> vim conf.d/appserver.conf  # 存放实际的代理配置,根据具体项目调整即可,这里只是示例
ini 复制代码
# 定义负载均衡主机组。可配置多个后端主机
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

bash 复制代码
>> vim docker-compose.yml
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

bash 复制代码
>> docker-compose up -d
相关推荐
lpfasd1231 天前
WinSW在Win7上失败真相-实测与修复
windows·nginx
lpfasd1231 天前
nginx服务化五套方案原理解剖与选型
运维·nginx
ue星空1 天前
ALS V4 中文拆解手册
运维·nginx
吴声子夜歌2 天前
Nginx应用与运维——Nginx集群负载与配置管理(二)
运维·nginx
cpolar技术支持2 天前
外网测试机的异常传不回内网?自托管 Sentry,用 cpolar 打通错误上报链路
python·nginx·docker·cpolar·sentry
吴声子夜歌2 天前
Nginx应用与运维——Nginx在Kubernetes中的应用(一)
运维·nginx·kubernetes
螺蛳粉 螺蛳粉2 天前
第一篇:Keepalived 高可用实战:VIP 漂移与 Nginx 主备切换完整指南
运维·nginx·负载均衡·keepalived·高可用
吴声子夜歌2 天前
Nginx应用与运维——Nginx在Kubernetes中的应用(三)
运维·nginx·kubernetes
吴声子夜歌2 天前
Nginx应用与运维——Nginx在Kubernetes中的应用(二)
运维·nginx·kubernetes
Wang's Blog3 天前
Java 项目实战: 外卖平台优化-Nginx配置文件结构与块层级
java·开发语言·nginx