使用docker部署vue项目(nginx基础之上)
1. 打包Vue工程
去除 vue.config.js 中的网络代理代码
_npm run build_
:命令将项目打包为dist文件
将dist文件上传到服务器
2. 使用Dockerfile构建nginx镜像
在dist同级目录下创建Dockerfile文件进行写入
_docker build -f ContentManagementSystemDockerFile -t docker_vue:1.0 ._
将Dockerfile构建为镜像
3. 使用自定义镜像创建容器
_mkdir nginx/conf.d_
创建挂载文件夹,用作自定义nginx容器配置挂载
_docker run -p 80:80 -v /user/NginxStaticResources/nginx/conf.d/:/etc/nginx/conf.d/ --name cms docker_vue:1.0_
创建容器并对配置信息进行挂载
4. 修改nginx配置文件
_nginx/conf.d/default.conf : /etc/nginx/conf.d/default.conf_
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
# 表示允许这个域跨域调用(客户端发送请求的域名和端口)
# $http_origin动态获取请求客户端请求的域 不用*的原因是带cookie的请求不支持*号
add_header Access-Control-Allow-Origin $http_origin;
# 指定允许跨域的方法,*代表所有
add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,OPTIONS always;
# 带cookie请求需要加上这个字段,并设置为true
add_header Access-Control-Allow-Credentials true always;
# 表示请求头的字段 动态获取
add_header Access-Control-Allow-Headers $http_access_control_request_headers;
# 预检命令的缓存,如果不缓存每次会发送两次请求
add_header Access-Control-Max-Age 1728000 always;
# OPTIONS预检命令,预检命令通过时才发送请求
# 检查请求的类型是不是预检命令
if ($request_method = OPTIONS) {
return 304;
}
# 将/api/开头的请求反向代理到http://192.168.31.240:8080
location /api/ {
rewrite ^/api/(.*) /$1 break;
proxy_pass http://192.168.31.240:8080;
#root /usr/share/nginx/html;
#index index.html index.htm;
}
# 默认请求则访问容器内 /usr/share/nginx/html 下的资源
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;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
_docker restart cms_
重新启动容器,打开后台服务