2012-03-15 47 views
2

私はほとんどうまく機能していますが、それが正しく実装されているかどうかは不明です。 (私はnginxのに新たなんだ!)このNginxサーバブロックをフロントコントローラパターンで正しく動作させるにはどうすればよいですか?

私のテストでは、このディレクトリ構造ました:

/index.php (root directory front controller) 
/test/index.php (sub directory front controller) 
/include/include.php (some PHP include) 

ファイルはindex.phpという名前は、フロントコントローラです。

nginxのサーバーブロック:

server { 
    listen 80; 
    server_name mysite.com; 

    root /var/www/mysite.com/http; 
    index index.php; 

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

    location ~ \.php$ { 
     try_files $uri @rewrite; 
     fastcgi_pass 127.0.0.1:9000; 
     fastcgi_index index.php; 
     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
     include fastcgi_params; 
    } 

    location @rewrite { 
     rewrite^/index.php last; 
    } 
} 

これらの例は、現在動作し、このように動作する必要があります。

1. http://mysite.com/ => /index.php (exists) 
2. http://mysite.com/test => /test/index.php (exists) 
3. http://mysite.com/test/index.php => /test/index.php (exists) 
4. http://mysite.com/test/foobar => /index.php (/test/foobar didn't exist) 
5. http://mysite.com/asdf => /index.php (/asdf didn't exist) 
6. http://mysite.com/asdf.php => /index.php (/asdf.php didn't exist) 
7. http://mysite.com/asdf/index.php => /index.php (didn't exist; the try_files @rewrite fixes this, otherwise I get "No input file specified.") 

私はまだ持っていない場合は、次のとおりです。

8. http://mysite.com/include/include.php => /index.php 

現在、フロントコントローラではないPHPファイルにアクセスすると、そのスクリプトが処理されています。なぜそれがそれをやっているのか分かりますが、それはtry(ing)_files $uri @rewriteであり、include.phpを見つけることです。私は書き直しで修正しようとしましたが、ダイスはありませんでした。または、場所ブロック/ try_filesでより良い方法がありますか?

一般的に私は意図的にこのサーバーブロックを正しく考えていますか?あなたの質問は、あなたが欲しい示唆するものである

答えて

2

どの程度...

server { 
    listen 80; 
    server_name mysite.com; 

    root /var/www/mysite.com/http; 
    index index.php; 

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

    location ~ \.php$ { 
     location ~ /index\.php$ { 
      if (!-e $request_filename) { 
        rewrite^/index.php break; 
      } 
      fastcgi_pass 127.0.0.1:9000; 
      fastcgi_index index.php; 
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
      include fastcgi_params; 
     } 
     rewrite^/index.php last; 
    } 
} 

はこれがサービスを提供します...

yoursite/any_path/ => yoursite/any_path/index.php 
yoursite/any_path/index_file.php => yoursite/any_path/index.php 
yoursite/any_path/any_non_index_file.php => yoursite/index.php 

は...

(最後のものは私には奇妙に思えます)
+0

それは#8で修正されましたが、#7を修正するために次のように書き直さなければならなかった:http://pastebin.com/RmFaeTHF 奇妙に思えるかもしれませんが、index.phpというのはユーザーが唯一のページです今まで通りREQUEST_URIをインクルードして解析することで、他のものがバックグラウンドで発生します。ありがとう! – velo9

+0

私は 'try_files $ uri @ rewrite'は#7を防ぎますが、try_filesの後も処理が行われると嘘をつきました。 'if'を使っているようです。..? http://paste.ubuntu.com/885844/ – velo9

+0

修正。編集アドレス#7 =>件名のサブディレクトリにindex.phpファイルがない場合。私は将来の調査者の利益のために答えを更新します。 – Dayo

関連する問題