简体   繁体   English

Nginx 反向代理到多个设备映射主机名

[英]Nginx reverse proxy to multiple devices maping hostname

Good morning everyone, I have a rather peculiar configuration that I can't run with Nginx.大家早上好,我有一个相当特殊的配置,我不能用 Nginx 运行。

The webserver is dedicated to this job and is invoked with a descriptive domain based on a location, and consequently it has to do a reverse proxy to the specific IP of a device that is in that determinate Store, using VPN. Web 服务器专用于这项工作,并使用基于位置的描述性域进行调用,因此它必须使用 VPN 对特定商店中设备的特定 IP 进行反向代理。 The.networking part has already been implemented but unfortunately the device responds only in HTTP and is incompatible with the software that works in HTTPS, with this proxy server we would like to bypass the problem and create a kind of middleware that converts requests from HTTPS to HTTP, obviously using a wildcard certificate. .networking 部分已经实现,但不幸的是,设备仅在 HTTP 中响应,并且与在 HTTPS 中运行的软件不兼容,使用此代理服务器,我们想绕过该问题并创建一种中间件,将请求从 HTTPS 转换为HTTP,显然使用了通配符证书。

I'll get into practice with an example:我将通过一个例子来实践:

store21c.example.com -> 10.0.21.253
store21s.example.com -> 10.0.21.251

in practice nginx should read the domain part after store and before the dot, the numeric part corresponds to the 3rd entry of the IP of the device, while the letter if it is the device with final ip 253 or if it is final 251.实际上 nginx 应该读取存储之后和点之前的域部分,数字部分对应于设备 IP 的第 3 个条目,而如果它是最终为 ip 253 的设备或者如果它是最终 251,则为字母。

The result is to create 1 or 2 NGNIX configurations (avoiding making one per case) that respond to all the variables, and be ready for future stores.结果是创建 1 或 2 个响应所有变量的 NGNIX 配置(避免为每个案例创建一个),并为将来的存储做好准备。 I remember that the server is internal and not reachable from the inte.net and only needs to reverse proxy to that specific IPs.我记得服务器是内部的,无法从 inte.net 访问,只需要反向代理到特定的 IP。

Now we had this configurations that work great, but is for only one device:现在我们有了这个运行良好的配置,但只适用于一个设备:

server_name store21c.example.com;

location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://10.0.21.253;
}

Thank you for your support and suggestions in advance, Max提前感谢您的支持和建议,Max

I found a solution testing and google searching, if help here the results:我找到了一个解决方案测试和谷歌搜索,如果对这里有帮助,结果:

    server_name ~^store(?<store_id>[0-9]+)(?<type_id>[a-z]+)\.example\.com$;

    location / {
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
    if ($type_id = c) {
            proxy_pass http://10.0.$store_id.253;
            }
    if ($type_id = s) {
            proxy_pass http://10.0.$store_id.251;
            }
    }

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

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