简体   繁体   中英

Deploying nodejs project on nginx server

I have searched the whole internet and can't find any answer to this. I've just started my own nginx server and are deploying a nodejs project where I'm using angular 4 and expressjs. It works fine when I use the location '/' in nginx but when i change to '/dbproject' it does'nt work.

First it did'nt found the .js files in my public folder but when I builded angular with ng build --deploy url = /dbproject it found it, but then I get the errors:

Uncaught SyntaxError: Unexpected token <
polyfills.bundle.js:1 Uncaught SyntaxError: Unexpected token <
styles.bundle.js:1 Uncaught SyntaxError: Unexpected token <
vendor.bundle.js:1 Uncaught SyntaxError: Unexpected token <
main.bundle.js:1 Uncaught SyntaxError: Unexpected token < .

The odd thing is that it works all fine when i'm using the location '/' in nginx.

Here's the nginx setup:

location /dbproject {
    proxy_pass http://localhost: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's the app.js file:

const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const cors = require('cors');
const passport = require('passport');
const db = require('./config/db');
const app = express();

const users = require('./routes/users');
db.connect(db.MODE_PRODUCTION, function(err) {
  if (err) {
    console.log('Unable to connect to MySQL.')
    process.exit(1)
  } else {
    console.log("Connectad");
  }
});

// Port Number
const port = 3000;

// CORS Middleware
app.use(cors());

// Set Static Folder
app.use(express.static(path.join(__dirname, 'public')));


// Body Parser Middleware
app.use(bodyParser.json());

// Passport Middleware
app.use(passport.initialize());
app.use(passport.session());

require('./config/passport')(passport);

app.use('/users', users);

// Index Route
app.get('/', (req, res) => {
  res.send('Invalid Endpoint');
});


app.get('*', (req, res) =>{
  res.sendFile(path.join(__dirname, 'public/index.html'));
});
// Start Server
app.listen(port, () => {
  console.log('Server started on port '+port);
});

It's solved.

Before:

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

After:

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

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