node + Swagger behind nginx proxy_pass

Viewed 197

I'm trying to get node and swagger to work with nginx dynamically

server_name ~^backend(?<PORTSERVER>[^.]+)\.domain\.com$;

location /swagger
{
proxy_pass http://127.0.0.1:$PORTSERVER/swagger/;
}

location /api
{
proxy_pass http://127.0.0.1:$PORTSERVER/api;
}

this is an example of virtual host the PORTSERVER variable is taking from gitlab-ci it takes id number of merge request + 2000

when i put the port directly in place of $PORTSEVER every thing is working swagger and api

any advice is appreciated thank you

this is index.js file

    "use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
require("reflect-metadata");
const typeorm_1 = require("typeorm");
const express = require("express");
// var router = express.Router();
const fileUpload = require("express-fileupload");
const bodyParser = require("body-parser");
const routes_1 = require("./routes");
const cors = require("cors");
const typeorm_pagination_1 = require("typeorm-pagination");
const swaggerUi = require('swagger-ui-express');
const swaggerDocument = require('../../swagger.json');
var path = require('path');
typeorm_1.createConnection()
    .then((connection) => __awaiter(void 0, void 0, void 0, function* () {
    // create express app
    const app = express();
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded());
    app.use(fileUpload());
    app.use(express.static(path.join(__dirname, '..', 'public')));
    console.log(path.join(__dirname, '..', 'public'));
    // register express routes from defined application routes
    routes_1.Routes.forEach((route) => {
        app[route.method]('/api' + route.route, (req, res, next) => {
            const result = new route.controller()[route.action](req, res, next);
            res.header('Access-Control-Allow-Origin', '*');
            if (result instanceof Promise) {
                result.then((result) => (result !== null && result !== undefined ? res.send(result) : undefined));
            }
            else if (result !== null && result !== undefined) {
                res.json(result);
            }
        });
    });
   // setup express app here
    // ...
    app.use('/swagger', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
    app.use(express.json());
    app.use(cors());
    app.use(typeorm_pagination_1.pagination); // Register the pagination middleware
    // start express server
 //   app.listen(process.env.SERVER_Port);
    app.listen(process.env.PORTSERVER);
    console.log('Express server has started on port ' + process.env.PORTSERVER);
}))
    .catch((error) => console.log(error));
//# sourceMappingURL=index.js.map
1 Answers

The key is his sentence is "when i put the port directly in place of $PORTSEVER every thing is working swagger and api"

Based on the description you gave, I think that gitlabci is miss generating the port number, or miss understanding the syntax.. Both gitlabci and nginx uses $VAR syntax.. Can be a miss interpretation of the 1st line regex too..

Also, I think you need to check the content of process.env.PORTSERVERa used in the js file.. It can have different port than nginx..

For this, I would approach the issue by preventing the the job from restarting nginx to not cause down time for other vhosts.. Deploy a broken config then from the server I run nginx -t and/or diff -u a working config and a broken one..

The 1st source of truth would be nginx -t and nginx logs.. If, ever, nginx manage the starts, the HTTP code it's returning can reveal more paths to pursuit.

One thing you forgot to share is the content of your gitlabci YML.. That can help identify the issue too.

Related