前提条件:

前端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/

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐