【问题标题】:nginx 502 bad gateway in ubuntuubuntu中的nginx 502坏网关
【发布时间】:2021-03-29 08:32:42
【问题描述】:

我的服务器中的 Ubuntu 和 nginx。我有这个服务器的多个子域,它的 3000 端口已经被其他网站使用。整个节点应用程序将从 pm2 提供服务。

我猜是 502 网关错误,是我的服务器没有提供端口。

// /etc/nginx/sites-available/projectB

server {
    root /var/www/projectB;
    server_name projectB.domain.com www.projectB.domain.com;

    location / {
      proxy_pass http://localhost:3001; // this will be fine if it is localhost:3000;
      proxy_http_version: 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection 'upgrade';
      proxy_set_header Host $host;
      proxy_cache_bypass $http_upgrade;
    }
}

这是我的后端服务端口。里面不存在3001端口。我也会在下面提供 nginx error.log。

// sudo netstat -plant

Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      494/systemd-resolve 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      787/sshd: /usr/sbin 
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      80730/nginx: master 
tcp        0      0 127.0.0.1:27017         0.0.0.0:*               LISTEN      32831/mongod        
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      80730/nginx: master 
tcp        0      0 127.0.0.1:27017         127.0.0.1:39994         ESTABLISHED 32831/mongod        
tcp        0      0 127.0.0.1:39994         127.0.0.1:27017         ESTABLISHED 81007/node          
tcp        0    364 139.59.107.31:22        124.13.195.75:50855     ESTABLISHED 82341/sshd: root@pt 
tcp        0      0 127.0.0.1:27017         127.0.0.1:39984         ESTABLISHED 32831/mongod        
tcp        0      0 127.0.0.1:27017         127.0.0.1:39986         ESTABLISHED 32831/mongod        
tcp        0      0 127.0.0.1:39986         127.0.0.1:27017         ESTABLISHED 81007/node          
tcp        0      0 127.0.0.1:39984         127.0.0.1:27017         ESTABLISHED 81007/node          
tcp6       0      0 :::22                   :::*                    LISTEN      787/sshd: /usr/sbin 
tcp6       0      0 :::3000                 :::*                    LISTEN      81007/node          
tcp6       0      0 :::443                  :::*                    LISTEN      80730/nginx: master 
tcp6       0      0 :::80                   :::*                    LISTEN      80730/nginx: master 

nginx/error.log

[error] 80731#80731: *434 connect() failed (111: Connection refused) while connecting to upstream, client: clientPort, server: projectB.domain.com, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:3001/", host: "projectB.domain.com"

【问题讨论】:

    标签: nginx http-status-code-502


    【解决方案1】:

    您的应用程序本身似乎没有侦听端口 3001,这是 nginx 尝试将您的请求转发到的端口。

    检查您的应用程序是否已配置为侦听该端口并且它正在运行。您可以通过在服务器上运行此命令尝试在不使用 nginx 的情况下进行连接来检查它是否正常工作。

    curl http://localhost:3001/

    如果您收到响应,那么您的应用程序正在运行。查看 Nginx 是否能够提供该页面。它看起来应该基于您的配置。

    【讨论】:

    • 我如何检查端口是否正在侦听并且它正在运行?
    • curl localhost:3001 return curl: (7) 无法连接到 localhost 端口 3001:连接被拒绝。
    • 这意味着您的应用程序未运行或未在该端口上侦听。检查您的应用程序是否配置为侦听该端口并且它正在运行。不幸的是,如果不了解您的应用程序,我将无法提供更多帮助。
    • 酷。我想有调试它的想法,也许我的 nextjs 端口在 expressJs 的默认 3000 中提供服务。谢谢你。
    猜你喜欢
    • 1970-01-01
    • 2014-01-25
    • 2017-06-23
    • 2019-10-03
    • 2020-11-12
    • 2019-06-07
    • 2019-05-19
    • 1970-01-01
    • 2017-03-27
    相关资源
    最近更新 更多