栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

nginx 内网转发

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

nginx 内网转发

对外暴露虚假的地址,真实地址限制为内部调用。当对外地址转发到内部服务器,可做拦截、验证等等,校验通过后,再做静态转发。代码如下:

#nginx 配置

#对外暴露的地址
location /public/api {
      proxy_pass http://192.168.0.100:10086;
      index index.html index.htm;
}

#真实服务地址
location /private/api {
    # 限制为内部调用
    internal;
    proxy_pass http://192.168.0.200:10010;
    index index.html index.htm;
}

// 192.168.0.100:10086端口后台服务


import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
@RestController
public class TestController {

    
    @RequestMapping(value = "/**")
    public void auth (@RequestBody String body, HttpServletRequest request, HttpServletResponse response) {
        
        String requestURI = request.getRequestURI();
        
        // TODO 完善自己的验证规则
        if(requestURI.startsWith("/public/")){
            response.setStatus(500);
            return;
        }
        
        // TODO 完善自己的路由规则
        String interForwardURL = requestURI.replaceAll("/public/", "/private/");
        response.addHeader("X-Accel-Redirect",interForwardURL);
        response.setStatus(200);
    }
}
// 192.168.0.200:10010 后台服务

@RestController
@RequestMapping("/private")
public class TestController {

    @RequestMapping(value ="/api",method = {RequestMethod.GET,RequestMethod.POST})
    public void testMethod(@RequestBody TestDTO testDTO){
        System.out.println("success");
    }
    
}

http://192.168.0.200:10010/private/api 请求失败(404 Not Found)

http://192.168.0.100:10086/public/api 请求成功(200 OK)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/467500.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号