简体   繁体   English

来自代码的反向代理和HTTP请求

[英]Reverse proxy and HTTP request from code

I'm trying to figure out what is the "proper" way to make HTTP requests programatically from web application code when you don't know if you are or are not running behind reverse proxy (eg HTTPD). 我想弄清楚当你不知道你是否在反向代理(例如HTTPD)后面运行时,从Web应用程序代码以编程方式发出HTTP请求的“正确”方式是什么。

  • Web application runs on root "/" context on web server Web应用程序在Web服务器上的根“/”上下文中运行
  • Proxy runs with context "/proxy" that proxies this that web server 代理使用上下文“/ proxy”运行,该上下文代理该Web服务器

Accessing index.html from browser should be requested via /proxy/index.html. 应通过/proxy/index.html请求从浏览器访问index.html。 But what if there is some code in the web application (eg myscript.js) that sends HTTP request programatically (eg xhr.open("???/resource"). 但是,如果Web应用程序中有一些代码(例如myscript.js)以编程方式发送HTTP请求(例如xhr.open(“??? / resource”)。

And here comes the problem because the code sends this HTTP request to /resource instead of sending it to /proxy/resource. 这就出现了问题,因为代码将此HTTP请求发送到/ resource,而不是将其发送到/ proxy / resource。

In other words, the code of web application (that runs in the browser) does not know if there is any or there isn't a proxy. 换句话说,Web应用程序的代码(在浏览器中运行)不知道是否存在代理或代理。 Keep in mind that application can run behind proxy but there may not be any proxy at all. 请记住,应用程序可以在代理后面运行,但可能根本没有任何代理。 I have in mind 3 solutions: 我想到了3个解决方案:

1) Web application resolves context (eg /proxy) automatically by parsing it from the current window.location.path and send xhr according to it 1)Web应用程序通过从当前window.location.path解析它自动解析上下文(例如/ proxy)并根据它发送xhr

2) Enhance web application to require some additional configuration of proxy from user and it appends the context if it is set 2)增强Web应用程序以要求用户进行一些额外的代理配置,如果已设置,则附加上下文

3) Configure proxy somehow to also resend non-proxy like URLs to web server 1:1 (eg /proxy -> webserver/, / -> webserver/) 3)以某种方式配置代理也将非代理URL重新发送到Web服务器1:1(例如/ proxy - > webserver /,/ - > webserver /)

Which one is "the proper" one or there are any other options? 哪一个是“正确的”还是有其他选择?

Backend web applications should not be aware if there is proxy or not above or before them. 后端Web应用程序不应该知道上面或之前是否有代理。 They should ideally live in their own context path, eg. 理想情况下,他们应该生活在自己的背景路径中,例如。 /application/ and if they need to send redirects do so without using hostnames or url schemes in it, just URL-Path /application/* / application /如果他们需要发送重定向,那么不使用主机名或url方案,只需URL-Path / application / *

Then ideally you can do easy reverse proxy directives according to your number 3 scenario: 理想情况下,您可以根据您的3号场景执行简单的反向代理指令:

ProxyPass /XXX/ http://backend/application/
ProxyPassReverse /XXX/ http://backend/application/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM