2016-09-09 2 views
0

짧은 버전 :요청이 경로와 일치하지 않으면 Nginx가 444를 반환하도록하려면 어떻게해야합니까?

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

:

나는 대중 직면 URL에 액세스하는 클라이언트가 프록시 뒤에 앉아 내부 Gunicorn 서버에서 API 데이터를 제공 가도록 (듯이), 역방향 프록시로의 nginx를 사용하려면 위치 매핑이 정확하지 않습니다.

긴 버전 :

나는 처음의 nginx를 설정하고 있고 Gunicorn에서 제공하는 휴식 API에 대한 역방향 프록시로 사용하려고 시도하고있다. API는 127.0.0.1:8001에서 제공되며 서버에서 액세스하여 적절한 응답을 얻을 수 있으므로 올바르게 작동한다고 생각됩니다. Supervisord를 사용하여 지속적으로 실행됩니다.

외부에 API 끝점 중 하나 인 <static IP>/ABC/data에 액세스하고 싶습니다. Gunicorn 서버에서이 끝 점은 localhost:8001/data입니다. 궁극적으로 <static IP>/foo, <static IP>/bar 등과 같은 뿌리를 가진 NGINX를 통해 다른 웹 응용 프로그램을 제공하고 싶습니다.이 웹 응용 프로그램 각각은 독립적 인 Python 응용 프로그램에서 나왔습니다. 그러나 현재 외부 적으로 끝점에 액세스하려고하면 444 오류 코드가 표시되므로 NGINX를 올바르게 구성하지 않는 것으로 생각됩니다.

config posted on the Guincorn site의 NGINX 구성에서 첫 시도를했습니다. 단일 구성 대신 글로벌 구성과 사이트 고유 구성으로 분할했습니다. /etc/nginx/sites-available에 (그리고 /etc/nginx/sites-enabled에 심볼릭 링크되어)

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/*; 
} 

그런 다음 내 사이트의 특정 구성입니다 :

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을 통과,하지만 난보고 결국 etc/nginx/nginx.conf에서 내 글로벌 설정은 같다 내 액세스 로그에 다음과 같습니다 :

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를 반환하는 동작을 파악하는 방법을 알 수 없습니다. 나는하고 싶지만이 부분에 여전히 붙어 있습니다. 이 블록은 들어오는 모든 요청을 먹는 것 같습니다. 명시 적으로는 애플리케이션 서버에 대한 올바른 매핑을 얻을 수 rewrite를 사용하여도 server_name를 설정하고에

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

. "return 444;"를 제거하십시오. 서버 부분 :) – vcarel

+0

@vcarel 나는 다른 경로가 없다면 444 만 반환 할 것이라고 생각했다. 이것이 내가 원하는 것이다. 문제는 '/ABC/data'와 일치하지 않는다는 것입니다. – JoshAdel

답변

0

이 나를 위해 잘 작동 반환 (444)는 다른 서버 이름이 일치하지 않은 경우에만 (연결 끊) : 당신은 그냥 모든 요청에 ​​HTTP 코드 444을 반환하도록 서버를 물어

server { 
    listen  80; 
    server_name ""; 
    return 444; 
}