【问题标题】:Nginx error with port 80端口 80 的 Nginx 错误
【发布时间】:2014-06-03 11:48:36
【问题描述】:

我试图让我的 Django 应用程序在 VPS 上运行,我做了所有事情 according to this tutorial,但我收到了 502 错误。

我假设 nginx 正在监听 80 端口(对吗?),因为sudo netstat -nlp | grep 80 抛出:

tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      892/nginx
tcp6       0      0 :::80                   :::*                    LISTEN      892/nginx
unix  2      [ ACC ]     STREAM     LISTENING     8942     805/acpid           /var/run/acpid.socket

但是当我输入sudo nginx 时,似乎Nginx 没有监听端口80 ...:

`nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()`

我的 Nginx 配置:

server {
server_name 95.85.34.87;

access_log off;

location /static/ {
    alias /opt/myenv/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"';
}
}

谁能帮帮我?

【问题讨论】:

  • 您在 80 端口上运行其他服务吗?你是如何运行 gunicorn 的?
  • 我认为我没有在端口 80 上运行任何其他服务,因为我已经粘贴了 sudo netstat -nlp | grep 80 抛出的全部信息(我认为它过滤了哪些服务正在使用端口 80)。我对教程做了所有的事情,所以我像文章所说的那样使用 Gunicorn:gunicorn_django --bind 95.85.34.87:8001。顺便说一句,访问95.85.34.87:8001 工作正常,但我认为这不是它应该工作的方式。
  • 感谢您的回答,但不幸的是我遇到了同样的错误 - nginx 侦听端口 80,但“地址已在使用中”..

标签: linux django nginx


【解决方案1】:

我在您的netstat 输出中看到的是 nginx 已经在您的系统上运行。在 Debian 或 Ubuntu 等系统上,可能还有其他 *nix 系统,当您安装 nginx 时,它会在系统启动时启动。然后,当您尝试从命令行运行它时,您正在运行第二个实例,该实例尝试使用与启动时启动的实例相同的端口。在 Debian 或 Ubuntu 系统上,您可以通过以下方式阻止 nginx 启动:

$ sudo service nginx stop
$ sudo rm /etc/nginx/sites-enabled/default

删除默认设置会阻止它再次启动。该默认文件是/etc/nginx/sites-available/default 的符号链接,因此您可以在需要时轻松重新创建它。

或者,如果您想让启动时启动的 nginx 在其端口上运行,您可以使用从使用不同端口的命令行启动的 nginx 的配置,例如:

server {
        listen 3333 default_server;
        listen [::]:3333 default_server ipv6only=on;

附加说明:如果您将站点放在/etc/nginx/sites-enabled/ 中,那么您必须不要从命令行启动您的 nginx 实例。你应该只通过sudo service nginx [...]来控制nginx。

【讨论】:

  • 非常感谢您的详细评论。我有sudo -rm -rf'ed 'default' 配置文件,但 80 端口仍然被 nginx 监听......启用站点的目录现在只有我的站点,但我仍然得到同样的错误......你是什么提供的是我网站的默认端口是 3333(或其他)而不是 80,对吗?
  • 我已经编辑了我的答案。见最后一段。由于您决定将配置放入/etc/nginx/sites-enabled/,因此您必须使用service 命令来启动和停止您的网站,而不是使用sudo nginx 启动它,因为您将获得两个实例。
  • 再次感谢您,很抱歉造成混乱,这对我来说是一天的大量信息。所以现在 Nginx 启动了,但我收到 502 bad gateway 错误。你能告诉我应该如何检查 Nginx 和 Gunicorn 服务器日志来调试这个错误吗?
  • 当我遇到这样的错误时,我只需打开在/var/log/nginx/*/var/log/uwsgi/* 中创建的日志文件。我认为 Gunicorn(我不使用它)做了类似于 uwsgi 的事情。真的没有什么诀窍。我只是阅读这些日志,直到我看到我认为可以修复的东西。 :) 如果没有什么我可以修复的,那么我点击谷歌。
  • 谢谢你,我会想办法解决的:)我会接受你的回答是正确的,因为你告诉我“双 nginx 实例”,这似乎是真的。