6

サーバーをApacheからNginxに移行しており、次の非常に単純な.htaccessルールがあります。

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

その背後にある考え方は、すべての要求をフロントコントローラー(index.php)に送信することです。Nginxでも同じことをしようとしています。オンラインコンバーターを使用して、このNginxロケーションブロックを作成しました。

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

しかし、それを自分のサイトの構成に追加すると、NginxはPHPファイルのソースコードをダウンロードとして吐き出します。参考までに、構成ファイル全体を次に示します。

http://pastebin.com/tyKtM1iB

私は、ロケーションブロックを削除して、それを使用してファイルを作成するかのように、PHPが機能することを知っています<?php phpinfo();

どんな助けでもいただければ幸いです。

4

2 に答える 2

13

これは、サブディレクトリリクエスト、HTTP引数など、すべてをindex.phpにルーティングする方法です。

location / {
    try_files $uri $uri/ /index.php?$args; #if doesn't exist, send it to index.php
}

location ~ \.php$ {
    include fastcgi_params;
    fastcgi_intercept_errors on;
    # By all means use a different server for the fcgi processes if you need to
    fastcgi_pass   127.0.0.1:9000;
 }

したがって、たとえば、これらはindex.phpに送信されます。

http://foo.bar/something/
http://foo.bar/something/?something=1

これらは直接ファイルに移動しますが

http://foo.bar/someotherphp.php
http://foo.bar/assets/someimg.jpg
于 2013-03-01T20:51:35.500 に答える
1

正規表現の終わりをスキップします:

location / {
        index index.html index.php;
        if (!-e $request_filename) {
            rewrite ^.* /index.php break;
            fastcgi_pass 127.0.0.1:9001;
        }
}
于 2013-03-01T20:38:54.183 に答える