简体   繁体   English

HTTP 404 与 Nodejs 应用程序在 ubuntu 18.04 AWS 服务器运行 Nginx 反向代理

[英]HTTP 404 with Nodejs app on ubuntu 18.04 AWS server running Nginx reverse proxy

I have moved my Nodejs (10.16) app to AWS ubuntu 18.04 running Nginx (17.4) reverse proxy from development after full testing.经过全面测试后,我已将我的 Nodejs (10.16) 应用程序移动到运行 Nginx (17.4) 反向代理的 AWS ubuntu 18.04。 The problem I have is that there is http 404 to front end React Native request.我遇到的问题是前端 React Native 请求有 http 404。 Here is the log output on my front end:这是我前端的日志 output:

[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

Here is the log output after starting app on the ubuntu server with node index.js :以下是使用node index.js在 ubuntu 服务器上启动应用程序后的日志 output :

Listening on port 3000...
Executing (default): SELECT 1+1 AS result
DB connection has been established successfully.

Here the server is listening on port 3000 and database connection has been established successfully.此处服务器正在侦听端口3000 ,并且已成功建立数据库连接。

Here is the nginx proxy config file default under /etc/nginx/sites-available/ :这是/etc/nginx/sites-available/ default的 nginx 代理配置文件:

$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;
    }
}

Here is the db.js in nodejs app:这是db.js应用程序中的 db.js:

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;

Here is the end of the index.js:这是 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}...`);

});

Here is the routes.js :这是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);
}

on AWS security group, inbound TCP port 80, 22, 443 and 3000 are opened for all traffic.在 AWS 安全组上,所有流量都开放入站TCP port 80, 22, 443 and 3000 What is missing in the config causing http 404?导致 http 404 的配置中缺少什么?

try this config for nginx试试这个配置 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;
    }
}

It turns out that in /etc/nginx/nginx.conf , there is missing a include /etc/nginx/sites-available/* .事实证明,在/etc/nginx/nginx.conf中,缺少一个include /etc/nginx/sites-available/* The reverse proxy config has to be included in nginx.conf first.反向代理配置必须首先包含在 nginx.conf 中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM