Docker中的Nginx配置代理实现不同url访问vue和springboot项目

我的需求:已经通过docker配置好了vue和springboot项目,通过http://101.201.210.12:7777 和http://101.201.210.12:9999 分别已经可以访问,现在我希望通过直接访问http://101.201.210.12/qianduan 进入vue项目,http://101.201.210.12/houduan 进入后端项目

思路:通过Nginx的反向代理(自定义服务器后面的/qianduan 或/houduan),配置默认的80端口(这样可以不用输入ip的端口)

1.docker拉取一下nginx:

docker pull nginx

2.进入/root/nginx

cd /root/nginx

3.在该文件夹下面创建一个nginx.conf文件,内容输入:标红的改为自己的项目url地址

user nginx;

worker_processes auto;

error_log /var/log/nginx/error.log;

pid /run/nginx.pid;

include /usr/share/nginx/modules/*.conf;

events {

    worker_connections 1024;

}

http {

    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;

    tcp_nodelay         on;

    keepalive_timeout   65;

    types_hash_max_size 2048;

    include /etc/nginx/mime.types;

    default_type        application/octet-stream;

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

   

    server {

        listen       80 default_server;

        listen       [::]:80 default_server;

        server_name  101.201.210.12;

        location /houduan/ {   #这个是代理到后端的接口

            proxy_pass http://101.201.210.12:9999/;

        }

        location /qianduan/ {  #这个是前端的接口

             proxy_pass http://101.201.210.12:7777/;

        }

        location /assets/ { #这个就是解决找不到静态文件

            proxy_pass http://101.201.210.12:7777/assets/;

        }

       

        error_page 404 /404.html;

        location = /40x.html {}

        error_page 500 502 503 504 /50x.html;

        location = /50x.html {}

    }

}

4.创建容器:b690f5f0a2d5是镜像id,端口一定是80:80,不要改

docker run --name nginx -v /root/nginx/nginx.conf:/etc/nginx/nginx.conf  -p 80:80 -d  b690f5f0a2d5

5.运行

http://101.201.210.12/qianduan

注意:当vue项目配置完毕后页面什么都没有,一片空白,控制台也没有报错,就将代码中的router中的createWebHistory改为createWebHashHistory  

  • 12
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值