cdredfox
V2EX  ›  问与答

求普及反向代理?

  •  
  •   cdredfox · Sep 26, 2012 · 4352 views
    This topic created in 5163 days ago, the information mentioned may be changed or developed.
    谁能帮我普及一下反向代理呀?google了一把,大概明白是怎么一回事。但是我有个具体的Case,不知道是否可以做到:

    假设我有个站 test.abc.com 是个二级域名,这个不支持或名绑定,我有一个域名,比如www.test.com我现在想把www.test.com反向代理到test.abc.com上面,即使用www.test.com上面访问的是test.abc.com是否可以?类似于cname的效果。
    7 replies  •  1970-01-01 08:00:00 +08:00
    TONYHEAD
    TONYHEAD
    Sep 26, 2012
    1
    可以的,apache的话可以把下面的代码写入www.test.com下的.htaccess文件

    RewriteEngine On
    RewriteBase /
    RewriteRule ^(.*)$ http://test.abc.com/$1 [P]
    cdredfox
    cdredfox
    OP
    Sep 26, 2012
    2
    反向代理是不是还需要 test.abc.com 这个站支持才行?如果不支持,也不能从 www.test.com 上面反向代理过去是吗?
    feiandxs
    feiandxs
    Sep 26, 2012
    3
    不用
    在nginx里可以做到的
    默认nginx转发$host ,包含域名
    你可以把$host改成test.abc.com
    这样nginx就会接收到www.test.com,但是给后端发送test.abc.com,返回的时候再给用户发送www.test.com
    无需后端服务器的支持。
    cdredfox
    cdredfox
    OP
    Sep 26, 2012
    4
    @feiandxs 但是很奇怪,我刚刚试了一下,貌似不行,不知道是否我的配置是否有问题,我是在cloudfoundry上的一个应用,就不能代理过去,直接报404了,如果我改成转发到twitter就可以,所以我刚刚才问是不是需要后端支持。
    cdredfox
    cdredfox
    OP
    Sep 26, 2012
    5
    @feiandxs 附上我的配置
    server
    {
    listen 80;
    server_name test.com;
    location / {
    proxy_pass http://zhekoujie.cloudfoundry.com/;
    proxy_redirect off;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
    }
    yangg
    yangg
    Sep 26, 2012
    6
    加上proxy_set_header Host zhekoujie.cloudfoundry.com;
    试试
    cdredfox
    cdredfox
    OP
    Oct 24, 2012
    7
    我加上这个设置后,还是一样的,404找不到的错
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1726 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 16:01 · PVG 00:01 · LAX 08:01 · JFK 11:01
    ♥ Do have faith in what you're doing.