图解 Docker、Nginx

2022-04-01 16:16:31 浏览数 (1)

引言

本文主要介绍:

  1. Nginx反向代理等
  2. Docker Compose 编排 Nginx、MySQL、Web
  3. 结合 ASP.NET Core

docker-compose.yml

代码语言:javascript复制
version: '3.4'

services:
  samplems.mysql:
    image: mysql
    container_name: samplems.mysql
    ports:
      - "3306:3306"
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD=mysql@samplems
      - TZ=Asia/Shanghai
    volumes:
      - /app/data/mysql:/var/lib/mysql
    networks:
      - samplems-net

  samplems.web:
    image: samplemsweb
    container_name: samplems.web
    restart: always
    depends_on:
      - samplems.mysql
    environment:
      - TZ=Asia/Shanghai
    networks:
      - samplems-net


  samplems.nginx:
    image: nginx
    container_name: samplems.nginx
    restart: always
    ports:
      - "80:80"
      - "443:443"
    environment:
      - TZ=Asia/Shanghai
    volumes:
      - /app/data/nginx/default.conf:/etc/nginx/conf.d/default.conf
      - /app/data/nginx/nginx.conf:/etc/nginx/nginx.conf
      - /app/logs/nginxlogs:/var/log/nginx
    depends_on:
      - samplems.web
    networks:
      - samplems-net

networks:
  samplems-net:
    driver: bridge

nginx.conf

代码语言:javascript复制
user  nginx;
worker_processes  1;

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


events {
    worker_connections  1024;
}


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

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

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


    #允许客户端请求的最大字节
    client_max_body_size 256m;
    #缓冲区最大字节
    client_body_buffer_size 256k;

    #代理服务器链接后端服务器的超时时间
    proxy_connect_timeout 30;
    #代理服务器等待后端服务器响应的超时时间
    proxy_read_timeout 60;
    #后端服务器返回数据给代理服务器的最大传输时间
    proxy_send_timeout 30;
    #代理服务器缓冲区大小,客户端的头信息会保存在这里
    proxy_buffer_size 64k;
    #代理服务器有几个缓冲区,最大是多大
    proxy_buffers 4 64k;
    #代理服务器繁忙可以申请更大的缓冲区,Nginx官方推荐为*2即可
    proxy_busy_buffers_size 128k;
    #代理服务器临时文件大小
    proxy_temp_file_write_size 256k;

	#websocket 启用
    map $http_upgrade $connection_upgrade {
        default upgrade;
        '' close;
    }

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

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

default.conf

代码语言:javascript复制
#设定负载均衡后台服务器列表
upstream composeserver {
	#指定支持的调度算法
	ip_hash;
    server samplems.web:80;
}

#虚拟主机的配置
server {
    listen 80;
    location / {
        proxy_pass http://composeserver;
		
        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;
    }
}

补充

Windows下查看80端口占用

参考:windows环境下80端口被占用怎么办_百度知道

代码语言:javascript复制
netstat -aon|findstr 80
代码语言:javascript复制
tasklist|findstr 4

原因:笔者的 Windows 开启了IIS

参考

感谢帮助!

  • docker 安装 nginx 并配置反向代理_李虹柏的博客-CSDN博客_docker安装nginx
  • Docker Nginx反向代理转发内网端口_Ch3nnn的博客-CSDN博客_docker nginx 端口转发
  • 通过 Docker 使应用程序容器化的教程 - .NET | Microsoft Docs
  • ASP.NET Core 的 Docker 映像 | Microsoft Docs
  • 配置 ASP.NET Core 以使用代理服务器和负载均衡器 | Microsoft Docs
  • Understanding Microsoft's Docker Images for .NET Apps
  • 反向代理为何叫反向代理? - 知乎
  • How nginx processes a request | Nginx Docs
  • docker部署ASP.NET Core、Nginx、MySQL - kasnti - 博客园
  • nginx - Docker Hub
  • Docker & ASP.NET Core (4):容器间的连接 - 软件工艺师 - 博客园
  • Docker & ASP.NET Core (5):Docker Compose - 软件工艺师 - 博客园

本文作者: yiyun

本文链接: https://cloud.tencent.com/developer/article/1970798

版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!

0 人点赞