求一个 Nginx 下泛域名反代的规则

2014-03-29 21:23:11 +08:00
 michelh
在nginx下,用泛域名反代泛域名,
类似这样的:
it.x.123.com 反代 it.y.abc.com
news.x.123.com 反代 news.y.abc.com
......
*.x.123.com 反代 *.y.abc.com
这样的规则应该怎么写呢?
谢谢大家!
4517 次点击
所在节点    NGINX
15 条回复
maga
2014-03-30 10:30:39 +08:00
rewrite 写个正则匹配?
xoxo
2014-03-30 16:05:00 +08:00
server{

server_name ~^(?<prefix>.+)\.x\.123\.com$;
...

location / {
set $hostbase '.y.abc.com';
set $host2proxy $prefix$hostbase ;
proxy_pass $host2proxy;
....
}

}


i will be glad if works...
hourui
2014-03-30 16:09:48 +08:00
首先 server_name ~^it\.(?<subdomain>[^\.]+)\.123\.com$;
然后 proxy_pass http://it.$subdomain.abc.com/;

大功告成
xsziran
2014-03-30 23:06:53 +08:00
https://code.google.com/p/7ghost/
这个应该可以 不过好像停止开发了
michelh
2014-03-30 23:50:24 +08:00
@xoxo 500错误了
ysjdx
2014-03-31 10:41:48 +08:00
server {
listen [::]:80;
server_name *.x.123.com;
location / {
if ($host ~* ^(.*).x.123.com$) {
set $domain $1;
proxy_pass http://$domain.y.abc.com;
}
}
}
michelh
2014-03-31 15:45:15 +08:00
@ysjdx 也不行,500错误
michelh
2014-03-31 15:46:05 +08:00
@xsziran Nginx反代效率高一些 :)
ysjdx
2014-04-03 13:01:25 +08:00
@michelh 没道理啊 500时候的日志是什么?这个我以前测试过。
michelh
2014-04-03 16:03:46 +08:00
@ysjdx 113.110.xxx.xxx - - [03/Apr/2014:16:03:01 +0800] "GET /favicon.ico HTTP/1.1" 502 568 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36"
michelh
2014-04-03 16:04:04 +08:00
@ysjdx 现在测试是502
coolloyal
2014-04-13 22:57:31 +08:00
@ysjdx
我这也是这个情况502错误,请问他的问题解决了吗?
server {
listen 80;
server_name ~^(www\.)?(?<domain>.+)\..+$;
index index.php index.html index.htm;

location / {
proxy_pass http://$domain.xxx.com/;
}
}
ysjdx
2014-05-03 00:16:46 +08:00
@michelh 问题解决了么?
ysjdx
2014-05-03 00:17:09 +08:00
@coolloyal 我以前测试是可以用的啊
michelh
2014-05-06 18:14:03 +08:00
@ysjdx 没有,以前也这么设置过,不记得是怎么写的

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/106449

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX