2012-02-10 28 views
12

私はApache 2からnginxに移行しました。サブドメインの制御に手を付けてしまいました。私が欲しいもの :私が持っている問題は、nginxのは、いつもにリダイレクトするブラウザを伝えることでページをリダイレクトすることです/ Xnginxサーバの設定:サブドメインからフォルダへ

をdomain.tldはするx.domain.tldが要求されると、内部書き換え。しかし、私が本当に望むのは、Apache 2のようにこれを内部的に行うことです。 はまた、私は唯一のx.domain.tldを要求した場合、nginxのは404を返し、私はx.domain.tld/index.phpを

を行う場合にのみ動作しますここに私の設定です:

server { 
     listen  80 default; 
     server_name _ domain.tld www.domain.tld ~^(?<sub>.+)\.domain\.tld$; 

     root /home/domain/docs/; 

     if ($sub) { 
       rewrite (.*) /$sub; 
     } 

     # HIDDEN FILES AND FOLDERS 
     rewrite ^(.*)\/\.(.*)$ @404 break; 

     location = @404 { 
       return 404; 
     } 

     # PHP 
     location ~ ^(.*)\.php$ { 
       if (!-f $request_filename) { 
         return 404; 
       } 

       include  /etc/nginx/fastcgi_params; 
       fastcgi_pass unix:/etc/nginx/sockets/domain.socket; 
     } 
} 

ありがとう!

答えて

0

http://wiki.nginx.org/IfIsEvilをご覧ください。あなたはこの設定ファイルで間違っています。

あなたが望むものが内部に保持されていれば、それを書き換えることはできません。定義上、クロスサイト書き換えをブラウザに送り返す必要があります。リクエストをプロキシする必要があります。

server { 
    server_name "~^(?<sub>.+)*\.(?<domain>.*)$"; 
    proxy_pass http://$domain/$sub$request_uri; 
} 

あなたはNginx wikiを読むべきです。このすべてについて深く説明します。

+0

がエラー '失敗原因:。(後に<' – vladkras

+0

が、私はそれが私のために、このソリューションのdoesntの仕事nginxの1.10.1 – vladkras

6

GoogleでこのQ & Aが見つかりましたが、同じ問題の解決策を探していましたが、私は最終的に使用したソリューションを投稿したかったのです。


はMTeckによる最初のサーバブロックはかなり良さそうに見えますが、サブドメインのためにあなたは、単に次の操作を行うことができ別れる:

server { 
    listen 80; 
    server_name "~^(?<sub>.+)\.domain\.tld$"; 

    root /path/to/document/root/$sub; 

    location/{ try_files $uri $uri/ /index.php; } 

    location ~ \.php { 
    include fastcgi_params; 
    fastcgi_pass unix:/etc/nginx/sockets/domain.socket; 
    } 
} 

これは、サブドメインに依存root設定ディレクティブになります。

+0

だった言及する必要が認識されない文字を – fdrv

2

私は壁に頭を打つ時間を過ごし、これはこれはまた、WWWのために働く私のためにどのような作品

server { 
    listen  80; 

    server_name ~^(?P<sub>.+)\.example\.com$; #<-- Note P before sub, it was critical for my nginx 
    root /var/www/$sub; #<-- most important line cause it defines $document_root for SCRIPT_FILENAME 

    location/{ 
      index index.php index.html; #<-- try_files didn't work as well 
    } 

    location ~ \.php$ { 
      fastcgi_pass 127.0.0.1:9000; #<-- probably you have another option here e.g. fastcgi_pass unix:/var/run/php5-fpm.sock; 
      fastcgi_index index.php; 
      fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name; 
      include  fastcgi_params; 
    } 
} 
+0

また、私の作品、ありがとうございます。私はwww、いくつかの例外のサポートを追加する方法を知っていますか?今すぐwww.mydomain.comを使用しようとすると、サブドメインを使用してみてください – fdrv

+0

また、wpが必要とする/ wordpressとして動作しないようです.php?$ args – fdrv

+0

@fdrvあなたは自分のルールを 'location/{...}'セクションに入れなければなりません。 'rewrite ^(。*?)$ /index.php?rt=$1 last;' – vladkras

0

です。 ?あなたの例でnginxの再起動しようとして

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

    index index.php index.html index.htm index.nginx-debian.html; 
    server_name ~^www\.(?P<sub>.+)\.domain\.com$ ~^(?P<sub>.+)\.domain\.com$; 
    root /var/www/html/$sub; 

    location/{ 
     try_files $uri $uri/ /index.php?$args; 
    } 
} 
関連する問題