【问题标题】:502 Bad Gateway, Django, Nginx, Gunicorn, Postgresql, Virtualenv502 网关错误,Django,Nginx,Gunicorn,Postgresql,Virtualenv
【发布时间】:2015-04-22 03:21:19
【问题描述】:

我已经研究了一周了。我一直在尝试在数字海洋上部署一个小测试应用程序,并且我遵循了许多类似这样的教程:https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-django-with-postgres-nginx-and-gunicorn

我什至尝试过一键安装教程: https://www.digitalocean.com/community/tutorials/how-to-use-the-django-one-click-install-image

昨晚我尝试了这个,我发现它是最近的一个: http://franklevering.me/blog/how-deploy-your-django-app-postgresql-nginx-and-gunicorn

当我访问我的 IP 时:http://188.166.62.146 我收到了 Welcome to nginx!页。 当我转到 http://188.166.62.146:8001 时,我收到 400 Bad Request。

我运行以下命令:

gunicorn --log-file=- blog.wsgi:application

[2015-02-20 07:02:55 +0000] [6089] [INFO] Starting gunicorn 19.2.1
[2015-02-20 07:02:55 +0000] [6089] [INFO] Listening at: http://127.0.0.1:8000 (6089)
[2015-02-20 07:02:55 +0000] [6089] [INFO] Using worker: sync
[2015-02-20 07:02:55 +0000] [6094] [INFO] Booting worker with pid: 6094

在 /var/log/nginx 中我重复出现此错误:

2015/02/20 03:07:27 [error] 4755#0: *9 connect() failed (111: Connection refused) while connecting to upstream, client: 95.128.91.3, server: 188.166.62.146, request: "GET /favicon.ico HTTP/1.1", u$$.1", upstream: "http://127.0.0.1:8001/favicon.ico", host: "188.166.62.146"

这是我的 settings.py 文件:

DEBUG = TEMPLATE_DEBUG = False


import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))

ALLOWED_HOSTS = []


INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'posts',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'blog.urls'

WSGI_APPLICATION = 'blog.wsgi.application'

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'blog',
        'USER': 'myuser',
        'PASSWORD': 'mypassword',
    }
}
LANGUAGE_CODE = 'en-us'

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'Asia/Nicosia'

USE_I18N = True

USE_L10N = True

USE_TZ = True

STATIC_ROOT = '/opt/blogger/blog/static/'
STATIC_URL = '/static/'

TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates')]

这是我在 etc/nginx/sites-available/blogger 中的配置文件:

server {
    server_name project.com;

    access_log off;

    location /static/ {
        alias /opt/blogger/blog/static/;
    }

    location / {
        proxy_pass http://127.0.0.1:8001;
        proxy_set_header X-Forwarded-Host $server_name;
        proxy_set_header X-Real-IP $remote_addr;
        add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
    }
}

我使用当前命令绑定gunicorn:

blog.wsgi:application --bind 188.166.62.146:8001

/opt/blogger/blog# gunicorn blog.wsgi:application --bind 127.0.0.1:8001

[2015-02-20 13:02:37 +0000] [7922] [INFO] Starting gunicorn 19.2.1
[2015-02-20 13:02:37 +0000] [7922] [INFO] Listening at: http://127.0.0.1:8001 (7922)
[2015-02-20 13:02:37 +0000] [7922] [INFO] Using worker: sync
[2015-02-20 13:02:37 +0000] [7927] [INFO] Booting worker with pid: 7927
^Z
[1]+  Stopped                 gunicorn blog.wsgi:application --bind 127.0.0.1:8001

/opt/blogger/blog#bg

[1]+ gunicorn blog.wsgi:application --bind 127.0.0.1:8001

【问题讨论】:

  • IP 地址不是服务器名称。
  • 感谢 Daniel 修复了网关 502 错误。但是现在我进入了一个显示的主页:Welcome to nginx!当我去 188.166.62.146:8001 我收到一个错误的请求 400
  • 您是否将/etc/nginx/sites-available/blogger 链接到/etc/nginx/sites-enabled/blogger 并删除默认的`/etc/nginx/sites-enabled/default`?这样做后你还需要重启 Nginx。
  • @Tony Kyriakidis 尝试设置 debug = True,如果可行,则在将 debug 设置为 False 时尝试使用 ALLOWED_HOSTS。请注意您提供的 tangowithDjango 链接“16.5.4.关闭调试模式”部分
  • 或尝试 debug = False 并更改 ALLOWED_HOSTS = ['*',] 如果可行,那么您知道与这些设置有关。尽管根据文档docs.djangoproject.com/en/1.7/ref/settings,使用 * 表示允许的主机并不好

标签: django postgresql nginx gunicorn digital-ocean


【解决方案1】:

首先,将您的 nginx 服务器名称更改为您的 IP 地址。

然后将 gunicorn 绑定到 127.0.0.1:8001。

交易是,据我所知,nginx 会将来自服务器名称的所有传入请求转发到 proxy_pass 变量。然后向您指向 proxy_pass 的位置发出 gunicorn 应答请求。

【讨论】:

  • 当您在同一个 IP 地址上使用多个域时,这仍然有效吗? (怎么做?)
  • 当然。 1)确保每个 nginx 条目在不同的端口(反向代理)上被接受 2)使每个 nginx 条目响应特定的 DNS 名称。站点 A 回答 example.com,站点 B 回答 coolexamples.com - 这应该在 server_Name 指令中配置。
猜你喜欢
  • 2012-05-28
  • 2018-12-05
  • 2017-07-07
  • 2014-12-07
  • 2021-11-19
  • 2016-06-23
  • 2017-10-01
  • 2017-05-17
  • 2020-09-29
相关资源
最近更新 更多