node js app issues after AWS Instance snapshot

helpimlost asked:

I had a node.js app with nginx running totally fine on a domain i purchased. It was run on an ubuntu server and all was well. I accidentally clicked “create image” on the aws site and ever since then the site doesn’t work. The local ip address through port 3000 is working as expected, but the domain is not. It is loading the first html file but cannot GET the stylesheets or javascripts that go with it and none of the css or javascript files are being loaded, rather they are all returning 404. I uninstalled/reinstalled nginx and such but have not had any luck. any ideas? I don’t want to change any code or such if possible, as it was working TOTALLY as expected/desired before this. I would like to express again that literally nothing other than clicking one button on aws has caused this issue

here is a config file for nginx

#
server {
listen 80;
listen [::]:80;
server_name example.org www.example.org;
return 301 https://$server_name$request_uri;
}

server {
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
server_name example.org www.example.org;

location / {
proxy_pass http://localhost:3000/login.html;
}
    ssl_certificate /etc/letsencrypt/live/example.org/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.org/privkey.pem; # managed by Certbot
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;

ssl_session_cache shared:SSL:5m;
ssl_session_timeout 1h;
add_header Strict-Transport-Security “max-age=15768000” always;


}  
#  

also, here is what was working for my app.js
this may be a good time to ask if i’m doing everything ok, im sure it can be improved as I am still just learning

var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser')
var logger = require('morgan');
var User = require("./models/user");
var db = require('./db.js');

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var devicesRouter = require('./routes/devices')

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');

app.use(logger('dev'));
app.use(express.json());
app.use(cookieParser());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use('/devices', devicesRouter);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;

Here is what the server responds when I use it (the first one is me on local host port 3000 and the second one is where it gets all funky, it looks like its trying to access /login.htmlstylesheets/login.css instead of /stylesheets/login/css

My answer:


Well, the problem is pretty obvious here:

location / {
proxy_pass http://localhost:3000/login.html;
}

So every time nginx passes a request up to your Node app, /login.html is prepended to the URI path.

My guess is you or someone else made this change days, weeks, or even months ago but never reloaded nginx with it, and it was finally loaded when your server was restarted.

You should instead be passing to http://127.0.0.1:3000, with no trailing path.

location / {
proxy_pass http://localhost:3000;
}

You also should set the document root to the directory containing the root of your static files, and use a try_files to ensure that nginx serves the static files instead of Node. A better configuration would look like:

server {
   ....
   root /srv/www/myapp/public;
   location / {
       try_files $uri @node;
   }

   location @node {
       proxy_pass http://127.0.0.1:3000;
   }
}

View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.