【问题标题】:failed (22: Invalid argument) while connecting to upstream连接到上游时失败(22:无效参数)
【发布时间】:2015-12-20 14:08:58
【问题描述】:

我正在按照教程在线获取一个新的 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 时,我会看到“欢迎使用 NGinx”页面。

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

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

提前致谢。

【问题讨论】:

    标签: django nginx uwsgi


    【解决方案1】:

    您的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

    【讨论】:

    • 你说的我已经试过了,但我仍然收到502错误。我已经更新了问题,使其符合当前情况。我已经能够访问 gezelligehotelletjes.com:8000/media/media.png,但是当我运行 uwsgi --socket gezelligehotelletjes.sock --uwsgi-file test.py 时出现 502 错误。
    • 所以你已经切换到一个unix套接字。您确定上游块中的路径名和服务命令行匹配吗?错误日志现在说什么?
    • 很抱歉响应缓慢。教程说我们必须更改为套接字,因为开销较小。我现在得到的错误是2015/12/22 18:24:21 [crit] 23345#0: *1 connect() to unix:/home/johan/gezelligehotelletjes/gezelligehotelletjes/gezelligehotelletjes.sock failed (13: Permission denied) while connecting to upstream, client: 95.96.206.181, server: gezelligehotelletjes.com, request: "GET /favicon.ico HTTP/1.1", upstream: "uwsgi://unix:/home/johan/gezelligehotelletjes/gezelligehotelletjes/gezelligehotelletjes.sock:", host: "gezelligehotelletjes.com:8000
    • @JohanVergeer 检查包含套接字的目录的权限。
    • 项目在/home/johan/gezelligehotelletjes 这有所有正确的权限设置。在 home/johan/gezelligehotelletjes/gezelligehotelletjes 文件夹内,我放置了 gezelligehotelletjes.sock,目前它是空的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-03
    • 1970-01-01
    • 2014-02-26
    • 2012-11-21
    • 1970-01-01
    • 2020-03-13
    • 1970-01-01
    相关资源
    最近更新 更多