You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Next.js+EC2(Ubuntu22.04)部署遇Nginx 502 Bad Gateway求助

问题:Next.js应用Nginx 502 Bad Gateway错误排查

我的应用基于Next.js + EC2(Ubuntu 22.04)+ S3 + CodeDeploy + PM2 + GitHub Actions部署,同时用Nginx和Let's Encrypt配置了HTTPS。之前运行正常,但昨天开始出现Nginx 502 Bad Gateway错误,尝试增大Nginx代理缓冲区大小后问题仍未解决。

错误日志

2022/11/21 05:15:48 [error] 11453#11453: *57 connect() failed (111: Unknown error) while connecting to upstream, client: 59.10.246.221, server: cocstats.awslearner-highjoon.com, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:3080/", host: "cocstats.awslearner-highjoon.com"
2022/11/21 05:15:48 [error] 11453#11453: *57 connect() failed (111: Unknown error) while connecting to upstream, client: 59.10.246.221, server: cocstats.awslearner-highjoon.com, request: "GET /favicon.ico HTTP/1.1", upstream: "http://127.0.0.1:3080/favicon.ico", host: "cocstats.awslearner-highjoon.com", referrer: "https://cocstats.awslearner-highjoon.com/"

package.json端口配置

"scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "cross-env NODE_ENV=production next start -p 3080",
    "lint": "next lint"
  },

Nginx配置

server {
        listen 80 default_server;
        listen [::]:80 default_server;
        server_name cocstats.awslearner-highjoon.com;
        return 301 https://cocstats.awslearner-highjoon.com$request_uri;

        # SSL configuration
        #
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
        #
        # Note: You should disable gzip for SSL traffic.
        # See: https://bugs.debian.org/773332
        #
        # Read up on ssl_ciphers to ensure a secure configuration.
        # See: https://bugs.debian.org/765782
        #
        # Self signed certs generated by the ssl-cert package
        # Don't use them in a production server!
        #
        # include snippets/snakeoil.conf;

        root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        server_name _;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }

        # pass PHP scripts to FastCGI server
        #
        #location ~ \.php$ {
        #       include snippets/fastcgi-php.conf;
        #
        #       # With php-fpm (or other unix sockets):
        #       fastcgi_pass unix:/run/php/php7.4-fpm.sock;
        #       # With php-cgi (or other tcp sockets):
        #       fastcgi_pass 127.0.0.1:9000;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #       deny all;
        #}
}

server {
        listen 443 ssl;
        server_name cocstats.awslearner-highjoon.com;
        ssl_certificate /etc/letsencrypt/live/cocstats.awslearner-highjoon.com/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/cocstats.awslearner-highjoon.com/privkey.pem; # managed by Certbot
        include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
        ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
        location / {
                proxy_set_header Host $host;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
                proxy_set_header X-Real-IP $remote_addr;
                proxy_pass http://127.0.0.1:3080;
                proxy_redirect off;
                proxy_buffer_size          128k;
                proxy_buffers              4 256k;
                proxy_busy_buffers_size    256k;
        }
}

排查解决步骤

1. 确认Next.js应用是否在3080端口正常运行

  • 执行netstat -tulpn | grep 3080或ss -tulpn | grep 3080,检查端口是否被监听。若无输出,说明应用未启动或端口配置异常。
  • 查看PM2日志:pm2 logs,排查应用启动时的报错(比如依赖缺失、构建产物损坏、环境变量错误)。
  • 手动启动应用测试:进入项目目录执行npm run start,观察终端输出,同时用curl http://127.0.0.1:3080测试本地访问是否正常。

2. 检查PM2进程状态

  • 执行pm2 status,确认Next.js应用进程为online状态。若为errored或stopped,执行pm2 restart <应用名称>重启进程。
  • 核对PM2应用配置,确保启动命令为npm run start,工作目录指向正确的项目路径。

3. 修复Nginx配置冲突

  • 80端口的server块中重复设置了server_name(先指定域名,后又设为_),这会导致配置冲突。删除后面的server_name _;,然后执行sudo systemctl restart nginx重启服务。
  • 验证Nginx配置语法:sudo nginx -t,确保配置无语法错误。

4. 检查系统权限与资源

  • 查看项目目录文件权限:ls -la .next,确保Node进程拥有读取构建产物的权限(所有者应为运行PM2的用户,如ubuntu或www-data)。
  • 检查EC2资源使用情况:top或htop,若内存不足可能导致应用崩溃或无法启动,可考虑临时清理内存或升级实例规格。

5. 排查部署日志

  • 查看GitHub Actions和CodeDeploy的部署记录,确认最近一次部署是否成功,是否存在构建失败、文件缺失等问题。

内容的提问来源于stack exchange,提问作者highJoon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 05:31:09