【问题标题】:AttributeError when attempting to deploy gunicorn with HTTPS尝试使用 HTTPS 部署 gunicorn 时出现 AttributeError
【发布时间】:2015-12-30 03:16:37
【问题描述】:

我正在尝试使用 Gunicorn over https 部署我的服务器。但是,无论我使用什么 nginx 配置,我总是在 Gunicorn 中得到一个属性错误。我认为问题不在于 Nginx,而在于 gunicorn。但我不知道如何解决它。这是我用来启动服务器的命令:

gunicorn -b 0.0.0.0:8000 --certfile=/etc/ssl/cert_chain.crt --keyfile=/etc/ssl/server.key pyhub2.wsgi

这是我的 nginx 配置文件:

  server {
    # port to listen on. Can also be set to an IP:PORT
    listen 80;
    server_name www.xxxxx.co;
    rewrite ^ https://$server_name$request_uri? permanent;
  include "/opt/bitnami/nginx/conf/vhosts/*.conf";
  }

  server {
    # port to listen on. Can also be set to an IP:PORT
    listen 443;
    ssl on;
    ssl_certificate /etc/ssl/cert_chain.crt;
    ssl_certificate_key /etc/ssl/server.key;
    server_name www.xxxx.co;
    access_log /opt/bitnami/nginx/logs/access.log;
    error_log /opt/bitnami/nginx/logs/error.log;
    location /xxxx.txt {
        root /home/bitnami;
    }

    location / {
      proxy_set_header X-Forwarded-For $scheme;
      proxy_buffering off;
      proxy_pass https://0.0.0.0:8000;
    }
    location /status {
      stub_status on;
      access_log   off;
      allow 127.0.0.1;
      deny all;
    }
    # PageSpeed
    #pagespeed on;
    #pagespeed FileCachePath /opt/bitnami/nginx/var/ngx_pagespeed_cache;
    #  Ensure requests for pagespeed optimized resources go to the pagespeed
    #  handler and no extraneous headers get set.
    #location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" { add_header "" ""; }
    #location ~ "^/ngx_pagespeed_static/" { }
    #location ~ "^/ngx_pagespeed_beacon$" { }
    #location /ngx_pagespeed_statistics { allow 127.0.0.1; deny all; }
    #location /ngx_pagespeed_message { allow 127.0.0.1; deny all; }
    location /static/ {
        autoindex on;
        alias /opt/bitnami/apps/django/django_projects/PyHub2/static/;
    }
    location /admin {
        proxy_pass https://127.0.0.1:8000;
        allow 96.241.66.109;
        deny all;
    }
    location /robots.txt {
        root /opt/bitnami/apps/django/django_projects/PyHub2;
    }

  include "/opt/bitnami/nginx/conf/vhosts/*.conf";
  }

以下是我尝试连接时遇到的错误:

Traceback (most recent call last):
  File "/opt/bitnami/python/lib/python2.7/site-packages/gunicorn/arbiter.py", line 515, in spawn_worker
    worker.init_process()
  File "/opt/bitnami/python/lib/python2.7/site-packages/gunicorn/workers/base.py", line 126, in init_process
    self.run()
  File "/opt/bitnami/python/lib/python2.7/site-packages/gunicorn/workers/sync.py", line 119, in run
    self.run_for_one(timeout)
  File "/opt/bitnami/python/lib/python2.7/site-packages/gunicorn/workers/sync.py", line 66, in run_for_one
    self.accept(listener)
  File "/opt/bitnami/python/lib/python2.7/site-packages/gunicorn/workers/sync.py", line 30, in accept
    self.handle(listener, client, addr)
  File "/opt/bitnami/python/lib/python2.7/site-packages/gunicorn/workers/sync.py", line 141, in handle
    self.handle_error(req, client, addr, e)
  File "/opt/bitnami/python/lib/python2.7/site-packages/gunicorn/workers/base.py", line 213, in handle_error
    self.log.exception("Error handling request %s", req.uri)
AttributeError: 'NoneType' object has no attribute 'uri'
[2015-12-29 22:12:26 +0000] [1887] [INFO] Worker exiting (pid: 1887)
[2015-12-30 03:12:26 +0000] [1921] [INFO] Booting worker with pid: 1921

根据 Klaus D. 的要求,我的 wsgi """ pyhub2 项目的 WSGI 配置。

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/1.8/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "pyhub2.settings")

application = get_wsgi_application()

【问题讨论】:

  • 请尝试在您的日志中找到完整的错误回溯并将其添加到您的帖子中。
  • ...pyhub2.wsgi 中有什么内容?
  • @KlausD。我的 Django 项目。
  • 把代码贴出来怎么样?
  • 什么代码?我的 Django 设置?我可以,但是里面有很多私人信息。

标签: python django nginx gunicorn


【解决方案1】:

如果 nginx 正在处理 SSL 协商并且 gunicorn 正在上游运行,则在启动 gunicorn 时不需要传递 --certfile=/etc/ssl/cert_chain.crt --keyfile=/etc/ssl/server.key

您可以尝试使用 Nginx 配置:

upstream app_server {
    server 127.0.0.1:8000;
}

server {
    listen 80;
    listen [::]:80 default_server ipv6only=on;
    server_name www.xxxxx.co;

    # Redirect to SSL
    rewrite ^ https://$server_name$request_uri? permanent;
    include "/opt/bitnami/nginx/conf/vhosts/*.conf";
}

server {  
    # Listen for SSL requests
    listen 443;
    server_name www.xxxx.co;

    ssl on;
    ssl_certificate /etc/ssl/cert_chain.crt;
    ssl_certificate_key /etc/ssl/server.key;


    client_max_body_size 4G;

    keepalive_timeout 5;

    location = /favicon.ico { access_log off; log_not_found off; }

    access_log /opt/bitnami/nginx/logs/access.log;
    error_log /opt/bitnami/nginx/logs/error.log;

    location /xxxx.txt {
        root /home/bitnami;
    }

    location /status {
        stub_status on;
        access_log off;
        allow 127.0.0.1;
        deny all;
    }

    location /static {
        autoindex on;
        alias /opt/bitnami/apps/django/django_projects/PyHub2/static;
    }

    location /admin {
        include proxy_params;

        proxy_set_header X-Forwarded-For $scheme;
        proxy_set_header X-Forwarded-Proto $scheme;

        # Proxy to upstream app_server
        proxy_pass http://app_server;

        allow 96.241.66.109;
        deny all;
    }

    location /robots.txt {
        root /opt/bitnami/apps/django/django_projects/PyHub2;
    }

    location / {
        try_files $uri @app_proxy;
    }

    location @app_proxy {
        # Handle requests, proxy to SSL
        include proxy_params;

        proxy_set_header X-Forwarded-For $scheme;
        proxy_set_header X-Forwarded-Proto $scheme;

        # Proxy to upstream app_server
        proxy_pass http://app_server;
    }

    include "/opt/bitnami/nginx/conf/vhosts/*.conf";
}

另外,您可以尝试使用 --check-config 标志启动 gunicorn 以检查 SSL 之外的配置错误,并确保您能够在没有 SSL 的情况下在本地访问 :8000。

【讨论】:

  • 感谢您的回复!当我使用你的代码时,它没有做它应该做的事情,它只是将我重定向到127.0.0.1
  • 嘿 Dorian,我编辑了我的答案,并根据您的示例包含了完整的 nginx 配置。当您说它重定向到 127.0.0.1 时,它是否成功访问 Django 但不是通过 SSL? “/opt/bitnami/nginx/conf/vhosts/*.conf”中有什么?
猜你喜欢
  • 2013-10-31
  • 1970-01-01
  • 2021-01-19
  • 1970-01-01
  • 1970-01-01
  • 2021-05-11
  • 1970-01-01
  • 2015-06-02
  • 2013-04-06
相关资源
最近更新 更多