2017-03-16 7 views
1

"/ directory /"の中にウェブサイトがあり、 "/ directory/subdirectory /"の中に特別なバリエーションがあり、いくつかの特定のIPアドレスでしかアクセスできない。ただし、サブディレクトリは仮想URLです。この例では、Webサイトのコードは "directory"という親ディレクトリに残ります。だから私は書き直しが必要です。拒否後の書き換えを防ぐ方法

これは私の現在のnginxの設定ファイルの該当部分である:

location /directory { 
    # here are various rewrites for my website 

    location /directory/subdirectory/ { 
     allow 10.0.0.0/8; # example range of IP addresses allowed 
     deny all; # disable access for others 
     rewrite ^/directory/subdirectory/(.*)$ /directory/$1 last; # the website location is still in the parent folder but the PHP code knows this specific version is to be used when the user surfs into subdirectory, which is why there is this rewrite 
    } 

    try_files $uri $uri/ /directory/index.php?q=$uri&$args; 
} 

location ~ \.php$ { 
    fastcgi_split_path_info ^(.+\.php)(/.+)$; 
    fastcgi_pass unix:/var/run/php5-fpm.sock; 
    fastcgi_index index.php; 
    include fastcgi.conf; 
    fastcgi_read_timeout 43200; 
} 

問題私が直面している:私は「すべてを拒否」ヒットするとすぐに実行を停止する方法を知りません。サブディレクトリにサーフィンしている場合は、上のディレクティブによってIPが許可されているかどうかにかかわらず、書き換えは常に実行されます。誰かがIPアドレスが許可されていないサブディレクトリにサーフィンしている人は、サブディレクトリ内のウェブサイトを見ることができます。これはもちろん私が望むものではありません。書き換えをコメントアウトすると、allow/denyディレクティブは想定通りに動作します。

So:このリライトはビジターが拒否されていない場合にのみ実行するように指定するにはどうすればよいですか? 「error_page 403 /403.html;」を追加しようとしました。その場所に "すべてを拒否する"の後にディレクティブを変更するだけですが、403ページが変更されます(サブディレクトリの書き換えルールにコメントを付ける場合にのみ再度表示されます)。これを実現させるために、さまざまな検索条件ですでにネットを検索していましたが、設定のバリエーションを手にしていましたが、役に立たなかったのです。

また、「deny all」は、サブディレクトリ内のURLにサーフィンし、「.php」で終わるとすぐには機能しなくなります。私はそれが "location〜.php $"ディレクティブのためであると仮定し、上記のネストされたロケーションディレクティブより何らかの理由で優先順位が高くなります。今、「場所^〜/ディレクトリ/サブディレクトリ/」ディレクティブは仕事「すべてを拒否」作る「場所〜の.php $」指示よりも優先権を得る

location ^~ /directory/subdirectory/ { 
    allow 10.0.0.0/8; # example range of IP addresses allowed 
    deny all; # disable access for others 
    rewrite ^/directory/subdirectory/(.*)$ /directory/$1 last; 
} 

location /directory { 
    # here are various rewrites for my website 

    try_files $uri $uri/ /directory/index.php?q=$uri&$args; 
} 

location ~ \.php$ { 
    fastcgi_split_path_info ^(.+\.php)(/.+)$; 
    fastcgi_pass unix:/var/run/php5-fpm.sock; 
    fastcgi_index index.php; 
    include fastcgi.conf; 
    fastcgi_read_timeout 43200; 
} 

を私:私は実行してこの問題を回避できその場所ブロックから「ディレクトリ/」の場所から何かを移動するため、最も「読みやすい」ソリューションのようには見えないので、これが道のりであるかどうかはわかりません。また、それでも私の主な問題は解決しません( "rewrite ^/directory/subdirectory /(.*)$/directory/$ 1 last"を許可IPアドレスに対して実行し、 "deny" 。すべての」

はありがとう

答えて

1

あなたが直面している問題は、nginxのは、それのディレクティブを処理する優先/オーダーに関連して、詳細な説明はhere (hats off to the author!)を見つけることができます:。。

その実行でサーバの書き換え、サーバの書き換え後の順序を順番に並べ替えます。 find-config、rewrite、post-rewrite、preaccess、access、post-access、 試行ファイル、コンテンツ、最後にログ

"rewrite"は "access"の前に来るので、 "rewrite"指示が最初に処理されます。

"場合" は "書き換え" にも-phaseで扱われているので、一つは書くことができます:

location ~ \.php$ { 
if ($remote_addr != "172.17.0.1") { 
    return 403; 
} 
rewrite^/your-redirect last; 
} 
関連する問題