nginx 配置代理,根据 不同的请求头进行转发至不同的代理

解决场景:下载发票的版式文件,第三方返回的是url链接地址,但是服务是部署在内网环境,无法访问互联网进行下载。此时需要进行走反向代理出去,如果按照已有套路,就是根据不同的访问前缀,跳转不同的location,我们有四十几个路径,就需要配置40多个location ,比较繁琐。此时可以使用nginx里的map 属性,进行配置,配置如下:

nginx配置文件


#user  nobody;
worker_processes  6;#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;#pid        logs/nginx.pid;events {worker_connections 65535 ;
}http {include       mime.types;default_type  application/octet-stream;#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  logs/access.log  main;sendfile        on;#tcp_nopush     on;#keepalive_timeout  0;keepalive_timeout  120s;#gzip  on;# 增大 map_hash_bucket_size 的值,可以根据实际情况调整大小map_hash_bucket_size 200;  # 定义映射关系map $http_x_target_server $target_server {default         "";"pubapi.jcsk100.com"                 "https://pubapi.jcsk100.com";"rocgw.jcsk100.com"                  "https://rocgw.jcsk100.com/external/";"taxsapivip.jcsk100.com"             "https://taxsapivip.jcsk100.com/v1/api/s";"taxsapi.holytax.com"                "https://taxsapi.holytax.com/v1/api/s";"dppt.shanghai.chinatax.gov.cn"      "https://dppt.shanghai.chinatax.gov.cn:8443";"dppt.guangdong.chinatax.gov.cn"     "https://dppt.guangdong.chinatax.gov.cn:8443";"dppt.xiamen.chinatax.gov.cn"        "https://dppt.xiamen.chinatax.gov.cn:8443";"dppt.tianjin.chinatax.gov.cn"       "https://dppt.tianjin.chinatax.gov.cn:8443";"dppt.chongqing.chinatax.gov.cn"     "https://dppt.chongqing.chinatax.gov.cn:8443";"dppt.neimenggu.chinatax.gov.cn"     "https://dppt.neimenggu.chinatax.gov.cn:8443";"dppt.dalian.chinatax.gov.cn"        "https://dppt.dalian.chinatax.gov.cn:8443";"dppt.qingdao.chinatax.gov.cn"       "https://dppt.qingdao.chinatax.gov.cn:8443";"dppt.shaanxi.chinatax.gov.cn"       "https://dppt.shaanxi.chinatax.gov.cn:8443";"dppt.sichuan.chinatax.gov.cn"       "https://dppt.sichuan.chinatax.gov.cn:8443";"dppt.henan.chinatax.gov.cn"         "https://dppt.henan.chinatax.gov.cn:8443";"dppt.fujian.chinatax.gov.cn"        "https://dppt.fujian.chinatax.gov.cn:8443";"dppt.jilin.chinatax.gov.cn"         "https://dppt.jilin.chinatax.gov.cn:8443";"dppt.yunnan.chinatax.gov.cn"        "https://dppt.yunnan.chinatax.gov.cn:8443";"dppt.ningbo.chinatax.gov.cn"        "https://dppt.ningbo.chinatax.gov.cn:8443";"dppt.shenzhen.chinatax.gov.cn"      "https://dppt.shenzhen.chinatax.gov.cn:8443";"dppt.gansu.chinatax.gov.cn"         "https://dppt.gansu.chinatax.gov.cn:8443";"dppt.shanxi.chinatax.gov.cn"        "https://dppt.shanxi.chinatax.gov.cn:8443";"dppt.zhejiang.chinatax.gov.cn"      "https://dppt.zhejiang.chinatax.gov.cn:8443";"dppt.hainan.chinatax.gov.cn"        "https://dppt.hainan.chinatax.gov.cn:8443";"dppt.liaoning.chinatax.gov.cn"      "https://dppt.liaoning.chinatax.gov.cn:8443";"dppt.jiangsu.chinatax.gov.cn"       "https://dppt.jiangsu.chinatax.gov.cn:8443";"dppt.jiangxi.chinatax.gov.cn"       "https://dppt.jiangxi.chinatax.gov.cn:8443";"dppt.guangxi.chinatax.gov.cn"       "https://dppt.guangxi.chinatax.gov.cn:8443";"dppt.hebei.chinatax.gov.cn"         "https://dppt.hebei.chinatax.gov.cn:8443";"dppt.heilongjiang.chinatax.gov.cn"  "https://dppt.heilongjiang.chinatax.gov.cn:8443";"dppt.xinjiang.chinatax.gov.cn"      "https://dppt.xinjiang.chinatax.gov.cn:8443";"dppt.hubei.chinatax.gov.cn"         "https://dppt.hubei.chinatax.gov.cn:8443";"dppt.beijing.chinatax.gov.cn"       "https://dppt.beijing.chinatax.gov.cn:8443";"dppt.anhui.chinatax.gov.cn"         "https://dppt.anhui.chinatax.gov.cn:8443";"dppt.shandong.chinatax.gov.cn"      "https://dppt.shandong.chinatax.gov.cn:8443";"dppt.hunan.chinatax.gov.cn"         "https://dppt.hunan.chinatax.gov.cn:8443";"dppt.guizhou.chinatax.gov.cn"       "https://dppt.guizhou.chinatax.gov.cn:8443";"dppt.xizang.chinatax.gov.cn"        "https://dppt.xizang.chinatax.gov.cn:8443";"dppt.ningxia.chinatax.gov.cn"       "https://dppt.ningxia.chinatax.gov.cn:8443";"dppt.qinghai.chinatax.gov.cn"       "https://dppt.qinghai.chinatax.gov.cn:8443";}#代理地址server {listen  8444;location  / {# 响应头打印 $http_x_target_server 和 $target_server 的值add_header X-Target-Server $http_x_target_server;add_header Target-Server $target_server;# 默认不传给北京的地址if ($target_server = "") {set $target_server "https://dppt.beijing.chinatax.gov.cn:8443";}proxy_pass $target_server;proxy_cache off;proxy_redirect off;proxy_connect_timeout 5m;proxy_send_timeout 5m;proxy_read_timeout 5m;proxy_buffer_size 10m;proxy_buffers 256 10m;proxy_busy_buffers_size 10m;proxy_temp_file_write_size 10m;add_header Cache-Control no-cache;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}}# 添加 resolver 指令,使用 Google 的公共 DNS 服务器,根据项目上的情况进行配置,使用$target_server必须配置,写死地址则不需要resolver 8.8.8.8;
}

Java代码:

            // header 传入nginx里的key即可 就会指定到对应的目标server里URL url = new URL(urlString);URLConnection connection = url.openConnection();connection.setRequestProperty("X-Target-Server", header);

验证方式:
在这里插入图片描述
可以在响应头中,查看具体访问的代理地址。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/67287.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

设计一个流程来生成测试模型安全性的问题以及验证模型是否安全

要使用 Ollama 运行 llama3.3:70b 模型,并设计一个流程来生成测试模型安全性的问题以及验证模型是否安全,可以按照以下步骤进行设计和实现。整个过程包括环境配置、设计安全测试提示词、执行测试以及分析结果。以下是详细的步骤和指导: 1. 环…

iOS - TLS(线程本地存储)

从源码中,详细总结 TLS (Thread Local Storage) 的实现: 1. TLS 基本结构 // TLS 的基本结构 struct tls_data {pthread_key_t key; // 线程本地存储的键void (*destructor)(void *); // 清理函数 };// 自动释放池的 TLS class Autorelease…

docker在不删除容器的情况下修改端口映射

注意:必须先停止docker服务!!!! 1) 停止容器 2) 停止docker服务(systemctl stop docker) 3) 修改这个容器的hostconfig.json和config.v2.json文件中的端口 先查看容器id docker inspect jenkins 进入该目录 hostcon…

【js进阶】设计模式之单例模式的几种声明方式

单例模式&#xff0c;简言之就是一个类无论实例化多少次&#xff0c;最终都是同一个对象 原生js的几个辅助方式的实现 手写forEch,map,filter Array.prototype.MyForEach function (callback) {for (let i 0; i < this.length; i) {callback(this[i], i, this);} };con…

专题 - STM32

基础 基础知识 STM所有产品线&#xff08;列举型号&#xff09;&#xff1a; STM产品的3内核架构&#xff08;列举ARM芯片架构&#xff09;&#xff1a; STM32的3开发方式&#xff1a; STM32的5开发工具和套件&#xff1a; 若要在电脑上直接硬件级调试STM32设备&#xff0c;则…

-bash: /java: cannot execute binary file

在linux安装jdk报错 -bash: /java: cannot execute binary file 原因是jdk安装包和linux的不一致 程序员的面试宝典&#xff0c;一个免费的刷题平台

【MySQL】使用C语言链接

&#x1f308; 个人主页&#xff1a;Zfox_ &#x1f525; 系列专栏&#xff1a;MySQL 目录 一&#xff1a;&#x1f525; MySQL connect &#x1f98b; Connector / C 使用&#x1f98b; mysql 接口介绍&#x1f98b; 完整代码样例 二&#xff1a;&#x1f525; 共勉 一&#…

平滑算法 效果比较

目录 高斯平滑 效果对比 移动平均效果比较: 高斯平滑 效果对比 右边两个参数是1.5 2 代码: smooth_demo.py import numpy as np import cv2 from scipy.ndimage import gaussian_filter1ddef gaussian_smooth_array(arr, sigma):smoothed_arr = gaussian_filter1d(arr, s…

通过ssh连接debian

使用方法 ssh usernameipaddress [inputpasswd]root用户默认无法由ssh连接&#xff0c; 可以通过修改配置 sudo vim /etc/ssh/sshd_config去掉PermitRootLogin前的‘#’,并修改为 PermitRootLogin yes 重启sshd服务 sudo systemctl restart sshd参考 https://linuxconfig.or…

Outlook 无网络连接[2604] 错误解决办法

Outlook 是微软公司开发的一款广泛使用的电子邮件客户端&#xff0c;广泛应用于个人用户和企业办公环境中。然而&#xff0c;许多用户在使用 Outlook 时可能会遇到“无网络连接”或者“错误代码 [2604]”等问题。这个错误通常会导致 Outlook 无法正常连接到邮件服务器&#xff…

.NET 9.0 的 Blazor Web App 项目中 Hash 变换(MD5、Pbkdf2) 使用备忘

一、生成 string 对应的 MD5 码 /// <summary>/// 生成 string 对应的 MD5 码/// </summary>/// <param name"str">需要转换的字符串 string&#xff1a;用于登录认证时&#xff0c;str username 线下传递的key DateTime.Now.Ticks.ToString() …

“UniApp的音频播放——点击视频进入空白+解决视频播放器切换视频时一直加载的问题”——video.js、video-js.css

今天&#xff0c;又解决了一个单子“UniApp的音频播放——点击视频进入空白解决视频播放器切换视频时一直加载的问题” 一、问题描述 在开发一个基于 video.js 的视频播放器时&#xff0c;用户通过上下滑动切换视频时&#xff0c;视频一直处于加载状态&#xff0c;无法正常播放…

P3数据结构、数据类型、数字编码、字符编码:保姆级图文详解

文章目录 前言1、数据结构分类1.1、逻辑结构&#xff1a;线性与非线性1.2、物理结构&#xff1a;连续与分散1.3、数据结构的实现方式1.4、数据结构的选择依据 2、基本数据类型2.1、定义与分类2.2、存储形式 3、数字编码3.1、原码、反码与补码3.2、浮点数编码3.3、整数与浮点数区…

【JavaScript】基础内容,HTML如何引用JavaScript, JS 常用的数据类型

HTML 嵌入 Javascript 的方式 引入外部 js 文件 <head> <script Language "javaScript" src"index.js"/> </head>内部声明 <head> <script language"javascript">function hello(){alert("hello word&qu…

解密AIGC三大核心算法:GAN、Transformer、Diffusion Models原理与应用

在当今数字化时代&#xff0c;人工智能生成内容&#xff08;AIGC&#xff09;技术正以前所未有的速度改变着我们的生活和工作方式。从创意无限的文本生成&#xff0c;到栩栩如生的图像创作&#xff0c;再到动听的音乐旋律&#xff0c;AIGC的魔力无处不在。而这一切的背后&#…

Web前端------HTML表格

一.表格标签介绍 表格&#xff0c;类似操作的软件excel一样&#xff0c;通过规范的行列方式展示数据的一种视图&#xff01; 网页中&#xff08;初级开发&#xff09;&#xff0c;对于这种规范的数据&#xff0c;使用表格标签最方便的&#xff1b; 实际开发&#xff08;高级开…

关于AWS网络架构的思考

目录&#xff1a; AWS概述 EMR Serverless AWS VPC及其网络 关于AWS网络架构的思考 在AWS K8S中部署的业务&#xff0c;有不同的流量路径。 流量进入 客户端请求 普通的客户端流量流向从前到后是: 客户端公司网关(endpoint)业务的Endpoint ServiceLoad Balancers(监听80和…

玩转随机数:用 JavaScript 掌控不可预测的魔力!

玩转随机数&#xff1a;用 JavaScript 掌控不可预测的魔力&#xff01; 当计算机遇上“随机”&#xff0c;我们能做什么&#xff1f; 你曾想过在生活中拥有“超能力”吗&#xff1f;比如&#xff0c;可以预测下一个天气变化&#xff0c;或是猜中下一个彩票号码&#xff1f;虽…

ThreeJs功能演示——几何体操作导入导出

1、内部创建几何体导出编辑能力 1&#xff09;支持内部创建的面、正方体、球体 内部创建物体时&#xff0c;如果是三维物体&#xff0c;要创建集合形状geometry&#xff0c;和对应的材质material。再一起创建一个三维物体。 // 存储创建的几何体列表const geometries [];cre…

nginx 配置域名前缀访问 react 项目

说明一下&#xff1a;我是使用域名转发访问的&#xff0c;访问流程如下&#xff1a; 浏览器 》 服务器1 》 服务器2 由于服务器1已经为 https 的访问方式做了 ssl 证书等相关配置&#xff0c;然后转发到服务器2&#xff0c; 所以在服务器2中不需要再配置 ssl 证书相关的东西了&…