【问题标题】:How can I make nginx redirect subdomain to a folder?如何使 nginx 将子域重定向到文件夹?
【发布时间】:2020-04-06 22:25:04
【问题描述】:

如何让 nginx 将所有对我的子域的请求重定向到一个文件夹?

例子:

http://sub2.sub1.domain.com/

这应该表明 sub2 是 sub1.domain.com/sub2 中的一个文件夹

我该怎么做?

主要目标是向用户隐藏文件夹。所以,它应该继续 http://sub2.sub1.domain.com/

我的愿望是在 sub2 中使用通配符。

更新:

我试过了:

server {
    listen 80;
    listen [::]:80;

    server_name ~^(.*)\.sis\..*$;

location / {
    proxy_pass http://sis.mydomain.com/$1$request_uri;
    }
}

但它也没有工作,有什么错误吗?

【问题讨论】:

    标签: nginx proxy reverse-proxy reverse


    【解决方案1】:

    sub2.sub1.domain.comnginx 指令中,您可以输入:

    server {
        listen 80;
        server_name sub2.sub1.domain.com;
    
    location / {
        proxy_pass https://sub1.domain.com/sub2;
        }
    }
    

    因此,任何发往 sub2.sub1.domain.com 的请求都会被代理到 → sub1.domain.com/sub2(同时被屏蔽为 sub2.sub1.domain.com);这种方式也不需要重定向或重写。

    通配符方法

    server {
        listen 80;
        server_name ~^(.*)\.sub1\.domain\.com;
    
    location / {
        proxy_pass https://sub1.domain.com/$1;
        }
    }
    

    *上面的通配符方法未经测试。

    【讨论】:

    • 有没有办法在 sub2 中使用通配符?
    • @user3249427:见编辑。我假设你的意思是 *.sub1.domain.com 被代理到 sub1.domain.com/*
    猜你喜欢
    • 2017-05-10
    • 2016-11-11
    • 2012-12-03
    • 2016-11-09
    • 2015-05-27
    • 1970-01-01
    • 1970-01-01
    • 2013-12-03
    • 1970-01-01
    相关资源
    最近更新 更多