简体   繁体   中英

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. The problem I have is that there is http 404 to front end React Native request. Here is the log output on my front end:

[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 :

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.

Here is the nginx proxy config file default under /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;
    }
}

Here is the db.js in nodejs app:

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:

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 :

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. What is missing in the config causing http 404?

try this config for 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/* . The reverse proxy config has to be included in nginx.conf first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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