前置工作需要完成:
安装完成docker
安装完成nginx镜像,命令:docker pull nginx
开始配置nginx
1、创建nginx的配置文件
在宿主机我喜欢用mydata存储挂载的文件,比如我nginx所有的挂载文件都在/mydata/nginx目录下
在/mydata/nginx/config 目录下 创建一个nginx.conf 配置文件,内容如下:
user nginx;
worker_processes 4;
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;
underscores_in_headers on;
client_max_body_size 1024m;
server {
listen 31000;
listen [::]:31000;
server_name _;
# 项目目录
root /home/nginx/dist;
# Load configuration files for the default server block.
#include /etc/nginx/default.d/*.conf;
location / {
root /home/nginx/dist;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
2、将打包好的vue项目放在/mydata/nginx/dist目录下
3、执行运行命令并挂载文件
docker run -it \
--name nginx \
--hostname=nginx \
--privileged=true -u root \
--restart=always \
-v /mydata/nginx/dist:/home/nginx \
-v /mydata/nginx/config/nginx.conf:/etc/nginx/nginx.conf \
-v /mydata/nginx/log:/var/log/nginx/ \
-p 31000:31000 -d nginx