ショートバージョン:リクエストがパスに一致しない場合、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;
}
}
:私もアプリの設定を変更しました。
あなただけのすべての要求にHTTPコード444を返すようにサーバーを頼みます。 「return 444;」を削除します。サーバーのセクションで: – vcarel
@vcarel他のルートが一致しない場合は444しか返さないと思っていました。問題は '/ABC/data'と一致していないことです。 –
JoshAdel