IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    nginx部署 ( docker )

    John Doe发表于 2023-08-23 02:17:42
    love 0

    拉取镜像

    1
    docker pull nginx:1.19.8

    创建关联文件

    1
    2
    3
    4
    5
    6
    7
    8
    #创建文件夹
    mkdir /data/{conf,conf.d,html,logs}
    #编辑nginx.conf配置 配置内容在文后
    cd conf
    vi nginx.conf
    #编辑default.conf配置 配置内容在文后
    cd conf.d
    vi default.conf

    创建docker-compose.yml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    version: '3'
    services:
    nginx:
    # 镜像名
    image: nginx:1.19.8
    # 容器名
    container_name: matt-nginx
    # 重启策略
    restart: always
    privileged: true
    # 端口映射
    ports:
    - 80:80
    - 8080:8080
    # 挂载目录
    volumes:
    - /home/docker-container/nginx/conf/nginx.conf:/etc/nginx/nginx.conf
    - /home/docker-container/nginx/conf.d:/etc/nginx/conf.d
    - /home/docker-container/nginx/html:/usr/share/nginx/html
    - /home/docker-container/nginx/logs:/var/log/nginx
    environment:
    - TZ=Asia/Shanghai

    启动

    将需要代理的前端vue项目放入html文件夹中, 在conf.d文件夹中加入nginx配置文件,重启nginx即可

    1
    docker-compose up -d

    nginx.conf

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    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;

    sendfile on;
    #tcp_nopush on;

    keepalive_timeout 65;

    #gzip on;

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

    default.conf

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    server {
    listen 80;
    server_name localhost;

    #charset koi8-r;
    #access_log /var/log/nginx/host.access.log main;

    location / {
    root /usr/share/nginx/html;
    index index.html index.htm;
    }

    #error_page 404 /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
    root /usr/share/nginx/html;
    }

    }

    nginx代理配置文件示例

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    server {
    listen 8080;
    server_name 127.0.0.1;

    location / {
    #此处填写的地址是docker容器的html地址,因为我们已经做了挂载,例如宿主机/data/nginx/html/demo,则下面root应该填写/usr/share/nginx/html/demo
    root /usr/share/nginx/html/demo;
    try_files $uri $uri/ /index.html;
    index index.html index.htm;
    }
    location /api/{
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header REMOTE-HOST $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass http://172.19.20.129:8090/;
    }
    }


沪ICP备19023445号-2号
友情链接