V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
michelh
V2EX  ›  NGINX

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

  •  
  •   michelh · 2014-03-29 21:23:11 +08:00 · 4506 次点击
    这是一个创建于 3675 天前的主题,其中的信息可能已经有所发展或是发生改变。
    在nginx下,用泛域名反代泛域名,
    类似这样的:
    it.x.123.com 反代 it.y.abc.com
    news.x.123.com 反代 news.y.abc.com
    ......
    *.x.123.com 反代 *.y.abc.com
    这样的规则应该怎么写呢?
    谢谢大家!
    15 条回复    2014-05-06 18:14:03 +08:00
    maga
        1
    maga  
       2014-03-30 10:30:39 +08:00 via iPhone   ❤️ 1
    rewrite 写个正则匹配?
    xoxo
        2
    xoxo  
       2014-03-30 16:05:00 +08:00   ❤️ 2
    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
        3
    hourui  
       2014-03-30 16:09:48 +08:00   ❤️ 3
    首先 server_name ~^it\.(?<subdomain>[^\.]+)\.123\.com$;
    然后 proxy_pass http://it.$subdomain.abc.com/;

    大功告成
    xsziran
        4
    xsziran  
       2014-03-30 23:06:53 +08:00   ❤️ 1
    https://code.google.com/p/7ghost/
    这个应该可以 不过好像停止开发了
    michelh
        5
    michelh  
    OP
       2014-03-30 23:50:24 +08:00
    @xoxo 500错误了
    ysjdx
        6
    ysjdx  
       2014-03-31 10:41:48 +08:00   ❤️ 1
    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
        7
    michelh  
    OP
       2014-03-31 15:45:15 +08:00
    @ysjdx 也不行,500错误
    michelh
        8
    michelh  
    OP
       2014-03-31 15:46:05 +08:00
    @xsziran Nginx反代效率高一些 :)
    ysjdx
        9
    ysjdx  
       2014-04-03 13:01:25 +08:00
    @michelh 没道理啊 500时候的日志是什么?这个我以前测试过。
    michelh
        10
    michelh  
    OP
       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
        11
    michelh  
    OP
       2014-04-03 16:04:04 +08:00
    @ysjdx 现在测试是502
    coolloyal
        12
    coolloyal  
       2014-04-13 22:57:31 +08:00   ❤️ 1
    @ysjdx
    我这也是这个情况502错误,请问他的问题解决了吗?
    server {
    listen 80;
    server_name ~^(www\.)?(?<domain>.+)\..+$;
    index index.php index.html index.htm;

    location / {
    proxy_pass http://$domain.xxx.com/;
    }
    }
    ysjdx
        13
    ysjdx  
       2014-05-03 00:16:46 +08:00
    @michelh 问题解决了么?
    ysjdx
        14
    ysjdx  
       2014-05-03 00:17:09 +08:00
    @coolloyal 我以前测试是可以用的啊
    michelh
        15
    michelh  
    OP
       2014-05-06 18:14:03 +08:00
    @ysjdx 没有,以前也这么设置过,不记得是怎么写的
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2922 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 13:30 · PVG 21:30 · LAX 06:30 · JFK 09:30
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.