nginx、花、Django、proxy_pass,并拒绝无效主机

nginx, flower, Django, proxy_pass, and rejecting invalid HOSTs

我在 AWS 运行 nginx/uWSGI/Django 上有一个服务器,带有 RabbitMQ、flower 和 Celery。

问题:使用nginx代理花而不开新端口,并且拒绝格式错误的请求,这会导致Django中的Invalid HTTP_HOST Header错误。

我可以做任何一个,但不能同时做两个,因为我对 nginx 不是很有经验。

我是 运行 花 0.9.4,因为我知道 0.9.5 中的错误。

在下面的配置文件中,如果我注释掉“reject_hosts.conf”行,flower 会工作,但我会停止拒绝主机。如果我保留它,Web 浏览器会超时发出对 /flower URL.

的请求

这是相关的配置文件:

nginx-app.conf

# the upstream component nginx needs to connect to
upstream django {
    server unix:/home/app.sock; # uwsgi socket
}

include redirect_ssl.conf; #301 SSL redirects

# actual webserver. Note that https is handled by AWS so no listen on 443
server {
    # default_server indicates that this server block is the block to use if no others match server_name
    listen      8080 default_server;
    listen      [::]:8080 default_server;
    charset     utf-8;

    # max upload size
    client_max_body_size 3M;   # adjust to taste
    include django_aliases.conf; # media and static directories

    include reject_hosts.conf; # return 444 if wrong HOSTs header
    include flower.conf; # proxy flower

    include django_root.conf; # django upstream
}

redirect_ssl.conf

## 301 redirect for HTTPS
server {
     listen 80 default_server;
    listen [::]:80 default_server;
    server_name _;
    return 301 https://$host$request_uri;
}

flower.conf - 如果 reject_hosts 不包括在内,则此方法有效。我尝试了大约一千种变体,以获得能够与 Flower 中的所有文件正常工作的变体。

location /flower/ {
    rewrite ^/flower/(.*)$ / break;
    proxy_pass http://localhost:5555;
    proxy_redirect off;
    proxy_set_header Host $host;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_http_version 1.1;
}

reject_hosts.conf

if ($host !~* ^(127.0.0.1|localhost|mydomain.com|myotherdomain.com|my.subdomain.com)$ ) {
    return 444;
}

django_aliases.conf

# Django media
location /media  {
    alias media;  # Media files, actual location removed for paranoia
}

location /static {
    alias static; # Static files, actual location removed for paranoia
}

django_root.conf

location / {
    uwsgi_pass  django;
    include     uwsgi_params; # location removed for paranoia
    uwsgi_read_timeout 600;
    uwsgi_send_timeout 600;
    uwsgi_connect_timeout 60;
    uwsgi_ignore_client_abort on;
}

最后,Flower 由 supervisord 启动,如下所示:

command = python3 -m celery -A myproj flower --url_prefix=flower --port=5555

您可以尝试通过服务器块过滤 HTTP 请求(如 nginx 教程 suggested):

server {
    listen 80;
    listen [::]:80;
    server_name 127.0.0.1 localhost mydomain.com myotherdomain.com my.subdomain.com;
    return 301 https://$host$request_uri;
}
server {
    # handle invalid requests with his one
    listen 80 default_server;
    listen [::]:80 default_server;
    return 444;
}
server {
    listen      8080;
    listen      [::]:8080;
    server_name 127.0.0.1 localhost mydomain.com myotherdomain.com my.subdomain.com;
    ... # rest of the configuration
}
server {
    # handle invalid requests with his one
    listen      8080 default_server;
    listen      [::]:8080 default_server;
    return 444;
}

这样你就根本不需要 reject_hosts.conf 文件了。

不知道大家有没有注意到。你只处理有效的请求,不听无效的请求。

只需在 nginx 的服务器上下文中添加此属性:

server_name example.com mysite.example.com

同样,您必须在 Django 的 settings.py 文件中输入这些主机。 我通常使用 '*' 进行输入,这样我就不必担心在 nginx 中设置的主机名 proxy_pass 或我的 celery 请求的主机,等等。所以只需在下面添加一行settings.py

ALLOWED_HOSTS=['*']