2016-09-09 4 views
0

ショートバージョン:リクエストがパスに一致しない場合、Nginxは444を返すようにするにはどうすればよいですか?

私は公共の対面URLにアクセスして、クライアントがプロキシの背後に座っ内部Gunicornサーバから提供APIデータを取得するように、リバースプロキシとしてnginxのを使用したい:

external path (proxy) => internal app 
<static IP>/ABC/data => 127.0.0.1:8001/data 

私は正しい位置マッピングを取得していません。

ロングバージョン

私が初めてnginxのを設定していますし、Gunicornが提供するREST APIのためのリバースプロキシとしてそれを使用しようとしています。 APIは127.0.0.1:8001で提供されており、サーバーからアクセスして適切な応答を得ることができるので、正しく動作していると思います。 Supervisordを使用して永続的に実行しています。

APIエンドポイントの1つに外部から<static IP>/ABC/dataにアクセスしたいとします。 Gunicornサーバーでは、このエンドポイントはlocalhost:8001/dataにあります。最終的に私は<static IP>/foo,<static IP>/barなどの根を持つNGINXを介して他のWebアプリケーションを提供したいと思います。これらのWebアプリケーションのそれぞれは、独立したPythonアプリケーションのものです。しかし、現在、外部からエンドポイントにアクセスしようとすると、444エラーコードが表示されるため、NGINXを正しく設定していないと思います。

config posted on the Guincorn siteのNGINX設定で私の最初の試みをまとめました。単一の設定ではなく、グローバル設定とサイト固有の設定に分割しました。 etc/nginx/nginx.confでの私のグローバル設定は、次のようになります。

user ops; 
worker_processes 1; 
pid /run/nginx.pid; 
error_log /tmp/nginx.error.log; 

events { 
    worker_connections 1024; # increase if you have lots of clients 
    accept_mutex off; # set to 'on' if nginx worker_processes > 1 
    use epoll; 
    # 'use epoll;' to enable for Linux 2.6+ 
    # 'use kqueue;' to enable for FreeBSD, OSX 
} 

http { 
    include mime.types; 
    # fallback in case we can't determine a type 
    default_type application/octet-stream; 
    access_log /tmp/nginx.access.log combined; 
    sendfile on; 

    server_tokens off; 

    server { 
    # if no Host match, close the connection to prevent host spoofing 
    listen 80 default_server; 
    return 444; 
    } 

    gzip on; 
    gzip_disable "msie6"; 

    include /etc/nginx/conf.d/*.conf; 
    include /etc/nginx/sites-enabled/*; 
} 

次に/etc/nginx/sites-availableである(そして/etc/nginx/sites-enabledにシンボリックリンクされて)私のサイト固有の設定は次のとおりです。

upstream app_server { 
    # fail_timeout=0 means we always retry an upstream even if it failed 
    # to return a good HTTP response 

    # for UNIX domain socket setups 
    # server unix:/tmp/gunicorn_abc_api.sock fail_timeout=0; 

    # for a TCP configuration 
    server 127.0.0.1:8001 fail_timeout=0; 
} 

server { 
    # use 'listen 80 deferred;' for Linux 
    # use 'listen 80 accept_filter=httpready;' for FreeBSD 
    listen 80 deferred; 
    client_max_body_size 4G; 

    # set the correct host(s) for your site 
    server_name _; 

    keepalive_timeout 100; 

    # path for static files 
    #root /path/to/app/current/public; 

    location /ABC { 
    # checks for static file, if not found proxy to app 
    try_files $uri @proxy_to_app; 
    } 

    location @proxy_to_app { 
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
    # enable this if and only if you use HTTPS 
    # proxy_set_header X-Forwarded-Proto https; 
    proxy_set_header Host $http_host; 
    # we don't want nginx trying to do something clever with 
    # redirects, we set the Host: header above already. 
    proxy_redirect off; 
    proxy_pass http://app_server; 
    } 

    # error_page 500 502 503 504 /500.html; 
    # location = /500.html { 
    # root /path/to/app/current/public; 
    # } 
} 

のconfigsはservice nginx checkconfigを渡すが、私は見て終わります私のアクセスログに次の

XXX.XXX.X.XXX - - [09/Sep/2016:01:03:18 +0000] "GET /ABC/data HTTP/1.1" 444 0 "-" "python-requests/2.10.0" 

私は何とかルートを適切に構成されていませんしたと思います。任意の提案をいただければ幸いです。

UPDATE

私はそれはいくつかの変更を今働いています。私は次のブロックをコメントアウトしました:

server { 
    # if no Host match, close the connection to prevent host spoofing 
    listen 80 default_server; 
    return 444; 
    } 

有効なルートがない限り、444を返す動作を取得する方法を理解できません。私はしたいですが、私はまだこの部分に固執しています。このブロックは、すべての着信要求を食べているようです。基本的に私はserver_nameを設定しても、アプリケーションサーバへの正しいマッピングを取得するためにrewriteを使用して明示的に持っていたように見える

upstream app_server { 
    server 127.0.0.1:8001 fail_timeout=0; 
} 

server { 
    # use 'listen 80 deferred;' for Linux 
    # use 'listen 80 accept_filter=httpready;' for FreeBSD 
    listen 80 deferred; 
    client_max_body_size 100M; 

    # set the correct host(s) for your site 
    server_name $hostname; 

    keepalive_timeout 100; 

    location /ABC { 
    # checks for static file, if not found proxy to app 
    try_files $uri @proxy_to_app; 
    } 

    location @proxy_to_app { 
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
    # enable this if and only if you use HTTPS 
    # proxy_set_header X-Forwarded-Proto https; 
    proxy_set_header Host $http_host; 
    # we don't want nginx trying to do something clever with 
    # redirects, we set the Host: header above already. 
    proxy_redirect off; 
    rewrite ^/ABC/(.*) /$1 break; 
    proxy_pass http://app_server; 
    } 

} 

:私もアプリの設定を変更しました。

+1

あなただけのすべての要求にHTTPコード444を返すようにサーバーを頼みます。 「return 444;」を削除します。サーバーのセクションで: – vcarel

+0

@vcarel他のルートが一致しない場合は444しか返さないと思っていました。問題は '/ABC/data'と一致していないことです。 – JoshAdel

答えて

0

これが私のために正常に動作し、他のサーバー名が一致していない場合にのみ、(接続ハングアップ)444を返します。

server { 
    listen  80; 
    server_name ""; 
    return 444; 
} 
関連する問題