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
相关推荐
烬羽3 小时前
Dockerfile 是"做奶茶的配方"?用 todos 全栈项目看懂 Docker 构建与发布
nginx·docker·nestjs
meilindehuzi_a4 小时前
从浏览器输入 URL 到页面显示:DNS、Nginx、后端与 CDN 全链路解析
运维·nginx
NGINX开源社区4 小时前
使用 F5 NGINX Gateway Fabric 实现 AI 交付标准化
nginx·gateway·fabric
翔云1234561 天前
在nginx中,proxy_next_upstream off是什么含义
nginx
虎王物联1 天前
Nginx反向代理实现IoT设备HTTPS接入:从证书配置到MQTT over WebSocket实战
运维·物联网·nginx·https
Linux-lucky2 天前
28-Linux学习之旅之Maven与Nexus制品库
linux·运维·学习·nginx·tomcat·maven
小波波啊2 天前
nginx 转发超时故障排查实录
运维·nginx
陈皮波比茶2 天前
Nginx
nginx
mengge.cloud2 天前
0824LAMP项目实战:部署WordPress博客平台小白教程
android·linux·运维·服务器·学习·nginx