连接到上游时失败(22:无效参数)

failed (22: Invalid argument) while connecting to upstream

我正在按照教程在线获取新的 Django 网站,但我一直收到 502 Bad Gateway 错误。

检查错误日志时,我看到此消息不断返回:

当我输入gezelligehotelletjes.com:8000

2015/12/20 13:47:02 [crit] 2194#0: *3 connect() to 0.0.31.65:80 failed (22: Invalid argument) while connecting to upstream, client: 95.96.206.181, server: 45.79.160.100, request: "GET / HTTP/1.1", upstream: "uwsg$ "uwsgi://0.0.31.65:80", host: "www.gezelligehotelletjes.com:8000"

当我输入IP地址时:45.79.160.100

2015/12/20 13:48:32 [crit] 2194#0: *6 connect() to 0.0.31.65:80 failed (22: Invalid argument) while connecting to upstream, client: 95.96.206.181, server: 45.79.160.100, request: "GET / HTTP/1.1", upstream: "uwsg$ "uwsgi://0.0.31.65:80", host: "45.79.160.100:8000"

这是我目前正在使用的项目树:

.
├── db.sqlite3
├── manage.py
├── mysite
│   ├── __init__.py
│   ├── media
│   │   └── media.png
│   ├── __pycache__
│   │   ├── __init__.cpython-34.pyc
│   │   ├── settings.cpython-34.pyc
│   │   ├── urls.cpython-34.pyc
│   │   └── wsgi.cpython-34.pyc
│   ├── settings.py
│   ├── static
│   ├── urls.py
│   ├── uwsgi_params
│   └── wsgi.py
├── mysite_nginx.conf
├── mysite.sock
├── static
│   └── admin
│       ├── css
│       │   ├── base.css
│       ├── fonts
│       │   ├── README.txt
│       ├── img
│       │   ├── calendar-icons.svg
│       └── js
│           ├── actions.js
└── test.py

目前我没有使用套接字,但现在我已经在根目录中创建了一个空文件"mysite.sock"。

这些是mysite_nginx.conf的内容:

# mysite_nginx.conf

# the upstream component nginx needs to connect to
upstream django {
    server unix:///path/to/your/mysite/mysite.sock; # for a file socket
    # server localhost:8001; # for a web port socket (we'll use this first)
}

# configuration of the server
server {
    # the port your site will be served on
    listen      8000;
    # the domain name it will serve for
    server_name gezelligehotelletjes.com; # substitute your machine's IP address or FQDN
    charset     utf-8;

    # max upload size
    client_max_body_size 75M;   # adjust to taste

    # Django media
    location /media  {
        alias /home/johan/gezelligehotelletjes/gezelligehotelletjes/media;  # your Django project's media files - amend as required
    }

    location /static {
        alias /home/johan/gezelligehotelletjes/gezelligehotelletjes/static; # your Django project's static files - amend as required
    }

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     /home/johan/gezelligehotelletjes/gezelligehotelletjes/uwsgi_params; # the uwsgi_params file you installed
    }
}

以及uwsgi_params的内容:

uwsgi_param  QUERY_STRING       $query_string;
uwsgi_param  REQUEST_METHOD     $request_method;
uwsgi_param  CONTENT_TYPE       $content_type;
uwsgi_param  CONTENT_LENGTH     $content_length;

uwsgi_param  REQUEST_URI        $request_uri;
uwsgi_param  PATH_INFO          $document_uri;
uwsgi_param  DOCUMENT_ROOT      $document_root;
uwsgi_param  SERVER_PROTOCOL    $server_protocol;
uwsgi_param  REQUEST_SCHEME     $scheme;
uwsgi_param  HTTPS              $https if_not_empty;

uwsgi_param  REMOTE_ADDR        $remote_addr;
uwsgi_param  REMOTE_PORT        $remote_port;
uwsgi_param  SERVER_PORT        $server_port;
uwsgi_param  SERVER_NAME        $server_name;

我读到我只需要将它复制并粘贴到这个文件中,但也许这也需要更改。

当我访问 gezelligehotelletjes.com 或 45.79.160.100 时,我会看到 "Welcome to NGinx" 页面。

服务器 运行 Ubuntu 14.04 LTS,我使用的是 virtualenv。

我一直在网上寻找合适的解决方案,但在过去几天的工作之后我还没有弄清楚。 我真的希望有人能在这里帮助我。

提前致谢。

您的 upstream 容器中存在语法错误。有关详细信息,请参阅 this document。尝试其中一个(或服务器的真实 IP 地址):

upstream django { server localhost:8001; }
upstream django { server 127.0.0.1:8001; }

通常不需要将IP地址放入server_name指令中。如果这样做,它应该 而不是 有前导 .。如果这是唯一一个侦听端口 8000 的服务器,它就是隐含的默认服务器,因此无论如何都会响应任何服务器名称。如果您不想指定 FQDN,则可以完全省略 server_name。有关服务器名称的更多信息,请参阅 this