【问题标题】:How to set up Nginx config to rewrite my subdomain如何设置 Nginx 配置来重写我的子域
【发布时间】:2012-09-30 13:09:00
【问题描述】:

我正在尝试使用 Nginx 设置子域,但出现了一些错误。以下是我的配置:

server {
    listen 80;
    server_name dimain.com *.domain.com;
    root /path/to/fuelphp_project/public;

    location / {
        index  index.php index.html index.htm;
        if ($host ~ (.*)\.(.*) ) {
                set $sub_name $1;
                rewrite ^/(.*)$ /index.php/$sub_name/$1 last;
                break;
        }

        if (!-e $request_filename) {
            rewrite ^.*$ /index.php last;
            break;
        }
    }

    ...
}

我想要这样的结果:

sub1.domain.com/c1/a1 -> domain.com/sub1/c1/a1
sub2.domain.com/c2/a2 -> domain.com/sub2/c2/a2

如何纠正?

【问题讨论】:

    标签: nginx url-rewriting subdomain


    【解决方案1】:
    server {
      server_name sub1.domain.com;
      return 301 "http://domain.com/sub1${uri}";
    }
    

    这对你有用吗?

    我刚刚注意到这里有一个答案:https://serverfault.com/questions/426673/nginx-redirect-subdomain-to-sub-directory

    server {
      server_name ~^(?<sub>[a-zA-Z0-9-]+)\.domain\.com$; # will cause the sub-domain to be placed in $sub
      return 301 "http://domain.com/${sub}${uri}";
    }
    

    我认为正则表达式也可以是一个非常酷的解决方案......取决于你需要什么。

    【讨论】:

    • 修改.conf后需要重启nginx。
    猜你喜欢
    • 2019-09-05
    • 2012-04-11
    • 2012-09-19
    • 1970-01-01
    • 1970-01-01
    • 2021-03-02
    • 2016-07-04
    • 1970-01-01
    • 2012-01-28
    相关资源
    最近更新 更多