前后端分离项目服务代理配置的三种组合方式(前端nginx+gateway+后端服务)

前提条件:

前端config.js访问后端使用相对路径,baseurl为空

前端页面wlgl:localhost:2021

后端portal:localhost:8055

后端代理gateway:localhost:2000

nginx+gateway+后端服务的代理模式就出现了三种组合方式:

1、nginx再最前面,同时代理静态资源和gateway,gateway代理后端

这种情况适合鉴权体系不需要拦截前端页面的情况,好处的静态资源直接被客户机访问,效率较高。

具体配置如下:

nginx关键配置:

        location / {
            root /opt/w_portal/nginx/dist/  ;
            index  index.html index.htm;
        }
		
		location /gateway/{
          proxy_pass http://localhost:2000;
        }

gateway关键配置:

routes: 
    portal:
      path: /portal/**
      stripPrefix: false
      serviceId: portal

访问路径: http://localhost:2021

2、gateway在前,同时代理nginx和后端,nginx只代理静态资源,这种方式有利于对静态资源的鉴权处理,特别是集成单点登录时使用。关键配置如下:

nginx关键配置:

    location / {
            root /opt/w_portal/nginx/dist/  ;
            index  index.html index.htm;
        }

gateway关键配置

  routes: 
    wlgl:
      path: /**
      url: http://localhost:2021/
      stripPrefix: false
      sensitiveHeaders: 
    protal:
      path: /protal/**
      stripPrefix: false
      serviceId: protal   #或者 url: http://localhost:8055

访问路径:http://localhost:2000

3、gateway在前,只代理nginx,nginx同时代理静态资源和后端(这里增加了一个前端有前缀的情况配置,上面同样适用),这种方法适用于需要多个访问地址的情况。

nginx关键配置(注,这里的静态资源路径前要用alias而不是root):

	location /wlgl/ {
            alias /opt/w_portal/nginx/dist/  ;
            index  index.html index.htm;
        }
		
		location /portal/{
          proxy_pass http://localhost:8055;
        }

gateway关键配置

  routes: 
    wlgl:
      path: /wlgl/**
      url: http://localhost:2021/
      stripPrefix: false
      sensitiveHeaders: 

访问路径:http://localhost:2000/wlgl/和http://localhost:2021/wlgl/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值