如何在反向nginx代理中的更多服务器块中摆脱404错误代码

时间:2019-09-13 13:34:16

标签: node.js angular nginx pm2

对于我的应用程序,我安装了nginx 1.15.9(作为反向代理服务器)(在Ubuntu 18.04上)。我想在此服务器上运行Angular应用程序和Node应用程序(pm2),并且在我的(子)域上获得了angular应用程序,但是在从angular应用程序到节点应用程序的调用中,我收到404错误。 / p>

//路径:/ etc / nginx / sites-available / zbs

server {
    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    //more Certbot stuff

  root /var/www/zbs/html/schoolsysteem-project/;

  location / {                //angular
          try_files $uri $uri/ index.html;
  }

  location /api {             //pm2
          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;
  }
}

//路径:/ var / www / zbs / html / schoolsysteem-project

我放在此目录中的所有节点文件和角度文件。

//路径:/var/www/zbs/html/schoolsysteem-project/routes/loginUser.js

module.exports = app => {
  app.post('/api/loginUser', (req, res, next) => {
    //working code
  });
}

//路径:/var/www/zbs/html/schoolsysteem-project/index.js

require('babel-register')({
   presets: [ 'es2015' ]
});

require('./server');

//路径:/var/www/zbs/html/schoolsysteem-project/server.js

import express from 'express';
import Cors from 'cors';
import bodyParser from 'body-parser';
import logger from 'morgan';
import passport from 'passport';
import path from 'path';

const app = express();
const API_PORT = process.env.API_PORT || 3000;

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

require('./config/passport');

app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec));

app.use(logger('dev'));
app.use(passport.initialize());

//Cross Origin Resource Sharing
var corsWhitelist = [
    'http://localhost:4200',
  'http://localhost:4200/*'
];

var corsOptions = {
    origin: function(origin, callback){
        var isWhitelisted = corsWhitelist.indexOf(origin) !== -1;
        callback(null, isWhitelisted);
    },
    credentials:true
}

app.use(Cors(corsOptions));

//routes
require('./routes/getDownloadPdfWithPost')(app);
require('./routes/loginUser')(app);
require('./routes/registerUser')(app);
require('./routes/forgotPassword')(app);
require('./routes/resetPassword')(app);
require('./routes/updatePassword')(app);
require('./routes/updatePasswordViaEmail')(app);
require('./routes/findUser')(app);
require('./routes/getSubjectWithPost')(app);
require('./routes/deleteUser')(app);
require('./routes/updateUser')(app);
require('./routes/addSubject')(app);
require('./routes/updateUserSubjectEnrollment')(app);
require('./routes/updateSubjectTab')(app);
require('./routes/api/test')(app);

// eslint-disable-next-line no-console
app.listen(API_PORT, () => console.log(`Listening on port ${API_PORT}`));

module.exports = app;

还有来自角度应用程序的呼叫:

this._http.post<any>(`https://mijn.<some domain>.nl/api/loginUser`, JSON.parse(userJsonArr)).subscribe((val) => { 
//more code
)}

如何调用pm2(节点)路由并使其正常工作?

我也想将节点代码和角度代码分开。有两个根源,该怎么办?或者有更好的解决方案?

谢谢

0 个答案:

没有答案