前一阵有一个项目是采用Vue.js作为前端技术栈开发的,在实际部署的时候采用的是Webpack打包,然后用Nginx作为代理服务器,然后采用Daocloud平台,将其作为Docker容器部署。
由于后台也是采用Docker部署,所以我们并不知道后台的IP地址,没有办法采用常见的将自己定义的Nginx配置文件default.conf替换默认文件,常见方式的实现是:
RUN rm /etc/nginx/conf.d/default.conf //删除nginx 默认配置
ADD default.conf /etc/nginx/conf.d/ //添加自己的配置 default.conf 在下面
COPY dist/ /usr/share/nginx/html/我采用的解决方式是将Nginx配置文件中的proxy_pass 的值设为自定义的环境变量{HOST_IP}(可自定义变量名),这样在生成Vue Docker时可以定义环境变量为后台的IP地址,就解决了未知后台地址的Vue Docker搭建。
Nginx的配置文件如下
server {
listen 9000;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#change
location /api/ {
proxy_pass ${HOST_IP};
}
#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 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;
#}
}
Dockerfile 如下
From nginx
RUN rm /etc/nginx/conf.d/default.conf
#ADD
default.conf /etc/nginx/conf.d/
#RUN
rm /etc/nginx/nginx.conf
COPY . /usr/share/nginx/html/
COPY mysite.template /etc/nginx
CMD envsubst '$HOST_IP' </etc/nginx/mysite.template > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'
转载请声明原创地址,联系方式见其他博文
版权声明:本文为qq_38486568原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。