Skip to content

24、容器化部署Vue应用挂载Nginx配置文件

上一讲中我们使用docker容器运行的Nginx,nginx.conf是在容器内部的,我们修改及其不方便

1、准备Nginx配置文件

如果提前有nginx.conf则不需要准备

1、随便启动一个 nginx 实例

sh
docker run -p 88:80 --name nginx-temp -d registry.cn-hangzhou.aliyuncs.com/xx_blog/nginx:1.27.2

2、将配置文件复制出来

sh
docker container cp nginx-temp:/etc/nginx/nginx.conf .

image-20250730130903178

3、将nginx.conf放入项目根目录

image-20250730131054418

nginx

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
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;
}

2、修改Dockerfile

dockerfile
# 基础镜像
FROM registry.cn-hangzhou.aliyuncs.com/xx_blog/nginx:1.27.2

# author
MAINTAINER maintainer="xx@qq.com"

# 复制nginx.conf
COPY ./conf/nginx.conf /etc/nginx/nginx.conf

# 复制html文件到路径
COPY dist/ /usr/share/nginx/html

EXPOSE 80

3、加上监听配置

可以试着修改一下配置是否生效

nginx

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
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;

   server {
    listen       80;
    server_name  localhost;

    location / {
       root   /usr/share/nginx/html;
        index  index.html index.htm;
        try_files $uri $uri/ /index.html;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
 }


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