【问题标题】:nginx can't listen on port 80nginx 无法监听 80 端口
【发布时间】:2024-12-21 10:35:02
【问题描述】:

我正在尝试使用 gunicorn 设置 nginx,但我不断收到“欢迎使用 nginx!”页。我能够成功监听其他端口(如 8080),但端口 80 根本不起作用。

server { 
listen          80;
server_name     host.ca www.host.ca;
access_log      /var/log/nginx/example2.log;

location / {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass   http://127.0.0.1:8000;

}

}

我以 root 身份运行服务器。 我似乎看不到任何在 80 端口运行的东西。

【问题讨论】:

  • netstat -nlp | grep 80 返回什么?
  • 我使用的是 centos-6。它没有启用站点等。我必须删除服务器条目 /etc/nginx/conf.d/default.conf 并在同一目录中添加 nginx.conf。

标签: django nginx gunicorn


【解决方案1】:

诊断问题

请务必检查您的日志(可能是 /var/log/nginx 或某些变体)。

查看可能占用 80 端口的原因

netstat -nlp | grep 80

启用站点,占用端口

然后,确保您在 sites-enabled 中启用了 Django 站点。如果您先创建了旧符号链接,请删除所有旧符号链接。

rm /etc/nginx/sites-enabled/django
ln -s /etc/nginx/sites-available/django /etc/nginx/sites-enabled/django

仔细检查您的 /etc/nginx/nginx.conf 以确保它正在加载启用站点而不是加载其他默认值。

http {
  ...
  include /etc/nginx/sites-enabled/*;
}

完成所有这些后,关闭并重新启动 nginx 服务。

service nginx restartservice nginx stop && service nginx start

【讨论】:

  • netstat -nlp |grep 80 告诉我 nginx 正在侦听端口 80(当我没有将其设置为端口 80 时)。 0.0.0.0:80。这正常吗?
  • 听起来另一种配置优先于您设置的配置。在 /etc/nginx/nginx.conf 中查找罪魁祸首,以及启用站点的无关站点。
  • 只有“默认”存在。 nginx.conf 上没有监听其他端口,只有端口 80..
  • 我修复了无关的罪魁祸首,所以现在正式没有其他 nginx.conf 监听端口 80。我仍然无法通过欢迎屏幕..
  • 你也重启了吗? service nginx restart