gpt4 book ai didi

nginx - 我如何在 nginx 中将子域作为 proxy_pass 值传递?

转载 作者:行者123 更新时间:2023-12-02 15:24:07 25 4
gpt4 key购买 nike

我目前处于需要获取/捕获子域并将该子域值传递给 Nginx 配置中的 proxy_pass 的情况。

例如

如果用户输入

http://google.com.mydomain.com

那么它应该做代理传递

proxy_pass http://www.google.com/;

在上面的例子中 google.comsub-domain

这可行吗?我怎样才能在 nginx 中实现类似的东西?

目前我使用的配置是在配置文件中硬编码子域值,但是有很多子域,所以我需要这样做,但不知道正确的语法。

server {
listen 80;
server_name subdomain.domain.com;
charset utf-8;

location / {
proxy_pass http://www.subdomain/;
}
}

我使用 * 作为 A 记录将所有子域重定向到我的网络主机,即通配符 DNS。

更新:

我从 https://stackoverflow.com/a/22774520/1642018 中找到了代码片段

server {          
listen 80;
# this matches every subdomain of domain.
server_name .domain.com;

location / {

set $subdomain "";

if ($host ~* "^(.+)\.domain.com$") {
set $subdomain $1;
}

proxy_pass http://$subdomain;

}
}

但请求显示的是我的默认页面,该页面位于我的默认 Web 根目录中。

最佳答案

两件事。

1- 一个解析器(一个用于你的 nginx 的 DNS 服务器来解析 google.com,你可以在你的主机上添加或者你可以添加解析器语句)

2- 您需要解决您的客户将如何处理不同的域,我的意思是 google.com 与 google.com.ar 或 google.fr 不同)

在这个例子中,我为你的例子 google.com 做了它

worker_processes 4;

error_log /var/log/nginx/error.log;

events {
worker_connections 1024;
}

http {

server {
listen 80;

location / {
set $subdomain "";

if ($host ~* "^(.+)\.domain.com$") {
set $subdomain $1;
}
resolver 8.8.8.8;
proxy_pass "http://$subdomain.com";

}
}
}

希望这个配置对你有帮助。

关于nginx - 我如何在 nginx 中将子域作为 proxy_pass 值传递?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31793556/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com