node.js - 在运行 Nginx 反向代理的 ubuntu 18.04 AWS 服务器上使用 Nodejs 应用程序的 HTTP 404

标签 node.js ubuntu nginx amazon-ec2

经过全面测试后,我已将我的 Nodejs (10.16) 应用程序移动到运行 Nginx (17.4) 反向代理的 AWS ubuntu 18.04。我遇到的问题是前端 React Native 请求有 http 404。这是我前端的日志输出:

[12:50:39] I | ReactNativeJS ▶︎ 'response in signup: ', { type: 'default',
                             │ status: 404,  //<<<=== Server Not Found
                             │ ok: false,
                             │ statusText: undefined,
                             │ headers:
                             │ { map:
                             │ { connection: 'keep-alive',
                             │ 'content-length': '153',
                             │ 'content-type': 'text/html',
                             │ date: 'Wed, 09 Oct 2019 19:50:40 GMT',
                             │ server: 'nginx/1.17.4' } },  //<<<=== Nginx 
                             │ url: 'http://my-aws-public-ip/api/users/signup', //<<<=== request URL

这是在 ubuntu 服务器上使用 node index.js 启动应用程序后的日志输出:
Listening on port 3000...
Executing (default): SELECT 1+1 AS result
DB connection has been established successfully.

此处服务器正在监听端口 3000并且数据库连接已成功建立。

这里是 nginx 代理配置文件 default/etc/nginx/sites-available/ 下:
$cat /etc/nginx/sites-available/default
server {
    listen 80;
    server_name localhost;

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

这里是 db.js在 nodejs 应用程序中:
const Sql = require("sequelize");
const db = new Sql('emps', 'postgres', `${process.env.DB_PASSWORD}`, {
    host: 'localhost',
    dialect: 'postgres',
    port:5432,
} );

db
    .authenticate()
    .then(() => {
        console.log('DB connection has been established successfully.');

    })
    .catch(err => {
        console.error('Unable to connect to the database:', err);
    });

module.exports = db;

这是 index.js 的结尾:
server.listen(port, () => {  //port=3000
  //logger('info', `Listening on port ${port}...`);
  console.log(`env var: ${process.env.jwtPrivateKey}`)
  console.log(`Listening on port ${port}...`);

});

这里是 routes.js :
module.exports = function(app, io) {
    app.use(helmet());
    app.use(compression());
    app.use(express.json());
    app.use('/api/events', events);
    app.use('/api/messages', messages);
    app.use('/api/users', users);
    app.use('/api/groups', groups);
    app.use('/api/contacts', contacts);
    app.use('/api/groupmembers', groupmembers);
    app.use('/api/sms', smsverif);
    app.use(error);
}

在 AWS 安全组上,入站 TCP port 80, 22, 443 and 3000对所有交通开放。导致 http 404 的配置中缺少什么?

最佳答案

试试这个 nginx 的配置

server {
    listen 80;
    server_name localhost your-aws-public-ip default_server;

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

关于node.js - 在运行 Nginx 反向代理的 ubuntu 18.04 AWS 服务器上使用 Nodejs 应用程序的 HTTP 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58311816/

相关文章:

javascript - Node.js 中非并行异步循环与同步循环是否存在性能差异?

linux - 通过特定客户端连接到 centos 服务器时出错

ubuntu - 在 Ubuntu 15.10 中安装 Julia 0.4

ubuntu - 没有提示的 RVM 安装

ruby-on-rails - 我的/public 目录可以是带有 rails 3 + 乘客 3 + nginx 0.8 的符号链接(symbolic link)吗?

nginx - 内部负载平衡以及docker swarm v1.12的机制是什么

node.js - Docker:服务器的空响应

javascript - Amazon S3 POST api,并使用 NodeJS 签署策略

javascript - 访问 Node js 模块中的变量

python - 如何读取从nginx传递的python/flask中的UWSGI参数