2017-08-23 3 views
1

私はWebサーバー上の.phpファイルへの直接アクセスを制限しようとしています。allow localhost;すべてを否定する。 "index.php"と "/"にかかわらずすべてのために

使用allow localhost;deny all;。ただし、これによってもindex.phpへのアクセスが制限されます。

この問題を解決するにはどうすればよいですか? IF conditionsのようなものはありますか?

マイ設定:一般的に行われているもの

if ($request_uri ~* "^(/)index\.php$") { 
    return 301 $1; 
} 

location/{ 
try_files $uri $uri/ /index.php?$args; 

    rewrite ^/(\w+)$  /?system=$1  break; 
    rewrite ^/(\w+)/(\w+)(/.)*$ /?system=$1&id=$2 break; 
    rewrite ^/(.*)/$ /$1 permanent; 

location ~ \.php$ { 
     try_files $uri =404; 

     fastcgi_split_path_info ^(.+\.php)(/.+)$; 
     #fastcgi_pass 127.0.0.1:9000; 
     fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; 
     fastcgi_index index.php; 
     include fastcgi_params; 

     fastcgi_param PHP_VALUE "auto_prepend_file=/usr/share/nginx/html/web/config.php"; 
    } 

} 
+0

あなたは 'localhost'を許可言うとき - あなたはURLのホストの面で何を意味するのですか? 'http:// localhost/something'ですか? – wick

+0

つまり、サーバー自体(POSTなど)で行われたクエリ以外のすべてのクエリを拒否します。 – Leeloo

+0

POSTここで、「http:// localhost/my.php」または「http://my.domain」。 com/my.php'? – wick

答えて

1

はあなたが.htaccessファイルにRewriteRuleを追加することです。これにより、すべてのトラフィックをindex.phpにリダイレクトすることができます。

このソリューションでは、要求されたURLをURLパラメータとして追加することもできるため、index.php内で変数$_GETとしてアクセスできます。

.htaccessは、Apacheサーバーの単なる解決策であるため、ここでは1対1で適用することはできません。 nginxのウェブサイト上で、このブログの記事は、それがnginxの上で行うの方法について説明します。https://www.nginx.com/blog/creating-nginx-rewrite-rules/

+0

追加された設定。私は何かを 'location〜\ 'に書くべきです。phpファイルへのアクセスを制限する部分です。使用された 'try_files' ... – Leeloo

0

代わりに、このように、試合だけのindex.phpと他のすべてを拒否し、すべてのPHPファイルを一致させる:

location = /index.php { 
    try_files $uri =404; 

    fastcgi_split_path_info ^(.+\.php)(/.+)$; 
    #fastcgi_pass 127.0.0.1:9000; 
    fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; 
    fastcgi_index index.php; 
    include fastcgi_params; 

    fastcgi_param PHP_VALUE "auto_prepend_file=/usr/share/nginx/html/web/config.php";  
} 

location ~ \.php$ { 
    return 301 $scheme://$http_host/index.php; 
} 

許可したい場合別の構成

:サーバーから自身への投稿はURI

location = /post.php { 
    allow 127.0.0.1/24; 
    deny all; 

    try_files $uri =404; 

    fastcgi_split_path_info ^(.+\.php)(/.+)$; 
    #fastcgi_pass 127.0.0.1:9000; 
    fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; 
    fastcgi_index index.php; 
    include fastcgi_params; 

    fastcgi_param PHP_VALUE "auto_prepend_file=/usr/share/nginx/html/web/config.php";  
} 

編集のために以下を追加します。 210の

server { listen 80; location = /index.php { try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; #fastcgi_pass 127.0.0.1:9000; fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; fastcgi_index index.php; include fastcgi_params; fastcgi_param PHP_VALUE "auto_prepend_file=/usr/share/nginx/html/web/config.php"; } location ~ \.php$ { return 301 $scheme://$http_host/index.php; } } server { listen 127.0.0.1:81; location/{ try_files $uri $uri/ /index.php?$args; } location ~ \.php$ { try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; #fastcgi_pass 127.0.0.1:9000; fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; fastcgi_index index.php; include fastcgi_params; fastcgi_param PHP_VALUE "auto_prepend_file=/usr/share/nginx/html/web/config.php"; } } 

ローカル要求は、すなわち、ポート81に向けられる必要があるでしょう:

curl http://localhost:81/myscript.php 
+0

post.phpとは何ですか? – Leeloo

+0

ちょっとした例では、サーバーはlocahost経由でデータを送信する必要があり、これを調整する必要があります。 – HostFission

+0

ファイルがたくさんありますが、私はそれらすべてのための敗北を書くことができません – Leeloo

関連する問題