【问题标题】:Cannot GET / Node JS Amazon EC2 server issues无法 GET / Node JS Amazon EC2 服务器问题
【发布时间】:2020-06-02 13:57:10
【问题描述】:

在我的 app.js 文件中

app.listen(process.env.PORT || 8080, () => console.log(`Listening on port ${process.env.PORT || 8080}!`));

在我的 webpack.config.js 文件中

devServer: {
   port: 3000,
   open: true,
   proxy: {
     '/api': 'http://localhost:8080'
   }
}

在我的 EC2 实例上的 /etc/nginx/sites-enabled/default 中

server {
  listen       80;
  server_name  _;

  location / {
    proxy_pass http://[private ip]:8080;
    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;
  }
}

编辑:完整的 app.js 文件

const app = express();
app.use(cors());
app.use(express.static('dist'));
app.listen(process.env.PORT || 8080, () => console.log(`Listening on port ${process.env.PORT || 8080}!`));

app.post('/api/[route]', async (req, res, next) => {
  try {
    const seq = await sequelize();
    const file = await [...];
  } catch (err) {
    return next(err);
  }
});

【问题讨论】:

  • 在 nginx 中试试这个proxy_pass http://localhost:8080/;
  • @Ahmad 还是同样的错误
  • 对不起,您的 webpack 代理正在为 3000 上的应用程序提供服务,我认为您的代理通行证必须是 http://localhost:3000
  • 我改成这个,收到502 Bad Gateway
  • 请分享您的 app.js 代码,您的代码是否有 / 的端点?

标签: node.js nginx amazon-ec2 server


【解决方案1】:

正确的配置是

server {
  listen       80;
  server_name  _;

  location / {
    proxy_pass http://localhost:8080;
    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;
  }
}

我相信我一开始就错误地安装了 pm2,因为 nginx 服务器和 pm2 都试图侦听端口 80。所以我重新安装了所有东西,瞧,它现在可以工作了。

【讨论】:

    猜你喜欢
    • 2012-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-14
    • 2020-10-23
    • 1970-01-01
    • 2020-07-12
    • 2012-12-05
    相关资源
    最近更新 更多