尝试使用HTTPS部署gunicorn时的AttributeError

时间:2015-12-30 03:16:37

标签: python django nginx gunicorn

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

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()

1 个答案:

答案 0 :(得分:0)

如果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。