简体   繁体   中英

Nginx setup for a node server using angular cli

I am having issues with Nginx, Node Server and Express. I have deleted the default server block under /etc/nginx/sites-available and have added my own let's call it mikes-domain.com. I have copied the contents of the dist folder to /var/www/mikes-domain.com/html.

server {
listen 80 default_server;
root /var/www/mikes-domain.com/html;
index index.html index.htm;
server_name: mikes-domain.com;
location /admin {
proxy_pass http://localhost:8082;
prody_http_version: 1.1;
proxy_set_header Upgrade $http_upgrade;
prody_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}

Node and Express Server File

const express = require('express');
const path = require('path');
const http = require('http');
const bodyParser = require ('body-parser');
const api = require('./server/routes/api');
const app = express;
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(express.static(path.join(__dirname, 'node_modules')));
app.use(*/api', api);
app.set('trust proxy', true);
app.set('trust proxy', 'loopback');
const port = process.env.PORT || '8082';
http.createServer(app).listen(port);
console.log('Api listening on port ' + port);

When I visit mikes-domain.com/admin/ I am receiving this Cannot GET /admin/ but I am getting api works! which is what express should be displaying when I visit mikes-domain.com/admin/api/

In nginx config you pass all requests from mikes-domain.com/admin to localhost:8082

In Node Server: app.use('*/api', api)

You do not config for default route in Node ( localhost:8082 ) , so in this case Node will show nothing with ( mikes-domain.com/admin )

You can config your router to handle request to /admin/api/ with template below, unless Node will show nothing.

./server/routes/api.js

var router = express.Router()

// define the home page route
router.get('/', function (req, res) {
  res.send('Testing API')
})

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