NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
michelh
V2EX  ›  NGINX

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

  •  
  •   michelh · Mar 29, 2014 · 5508 views
    This topic created in 4462 days ago, the information mentioned may be changed or developed.
    在nginx下,用泛域名反代泛域名,
    类似这样的:
    it.x.123.com 反代 it.y.abc.com
    news.x.123.com 反代 news.y.abc.com
    ......
    *.x.123.com 反代 *.y.abc.com
    这样的规则应该怎么写呢?
    谢谢大家!
    15 replies    2014-05-06 18:14:03 +08:00
    maga
        1
    maga  
       Mar 30, 2014 via iPhone   ❤️ 1
    rewrite 写个正则匹配?
    xoxo
        2
    xoxo  
       Mar 30, 2014   ❤️ 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  
       Mar 30, 2014   ❤️ 3
    首先 server_name ~^it\.(?<subdomain>[^\.]+)\.123\.com$;
    然后 proxy_pass http://it.$subdomain.abc.com/;

    大功告成
    xsziran
        4
    xsziran  
       Mar 30, 2014   ❤️ 1
    https://code.google.com/p/7ghost/
    这个应该可以 不过好像停止开发了
    michelh
        5
    michelh  
    OP
       Mar 30, 2014
    @xoxo 500错误了
    ysjdx
        6
    ysjdx  
       Mar 31, 2014   ❤️ 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
       Mar 31, 2014
    @ysjdx 也不行,500错误
    michelh
        8
    michelh  
    OP
       Mar 31, 2014
    @xsziran Nginx反代效率高一些 :)
    ysjdx
        9
    ysjdx  
       Apr 3, 2014
    @michelh 没道理啊 500时候的日志是什么?这个我以前测试过。
    michelh
        10
    michelh  
    OP
       Apr 3, 2014
    @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
       Apr 3, 2014
    @ysjdx 现在测试是502
    coolloyal
        12
    coolloyal  
       Apr 13, 2014   ❤️ 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  
       May 3, 2014
    @michelh 问题解决了么?
    ysjdx
        14
    ysjdx  
       May 3, 2014
    @coolloyal 我以前测试是可以用的啊
    michelh
        15
    michelh  
    OP
       May 6, 2014
    @ysjdx 没有,以前也这么设置过,不记得是怎么写的
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5239 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 62ms · UTC 08:14 · PVG 16:14 · LAX 01:14 · JFK 04:14
    ♥ Do have faith in what you're doing.