如果请求与路径不匹配,如何让Nginx返回444?

时间:2016-09-09 13:31:32

标签: nginx reverse-proxy gunicorn

简短版:

我想将NGINX用作反向代理,以便访问面向公众的URL的客户端从位于代理后面的内部Gunicorn服务器获取API数据:

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

我没有得到正确的位置映射。

长版

我第一次设置NGINX并试图将其用作Gunicorn服务的休息api的反向代理。 api在127.0.0.1:8001提供,我可以从服务器访问它并获得相应的响应,因此我认为这件事工作正常。它使用Supervisord持续运行。

我想在<static IP>/ABC/data外部访问其中一个API端点。在Gunicorn服务器上,此端点可在localhost:8001/data处获得。最后,我想通过NGINX提供其他网络应用程序,例如<static IP>/foo<static IP>/bar等。这些网络应用程序中的每一个都来自一个独立的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;
  # }
}

配置通过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"

我想我以某种方式没有正确配置路线。任何建议将不胜感激。

更新

我现在正在进行一些改动。我评论了以下块:

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

除非有有效的路线,否则我无法弄清楚如何获得返回444的行为。我想,但我仍然坚持这一部分。这个块似乎吃了所有传入的请求。我还将应用配置更改为:

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

}

基本上我似乎必须明确设置server_name并使用rewrite来获取到应用服务器的正确映射。

1 个答案:

答案 0 :(得分:1)

这对我来说很好,只有在没有匹配其他服务器名称时才返回444(挂断连接):

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