1

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

それを修正する方法は?

4

1 に答える 1

0
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}";
}

正規表現も非常にクールなソリューションになると思います...必要なものによって異なります。

于 2012-10-10T06:15:26.947 に答える