skip to Main Content

I am trying to setup a separate API backend for a website that serves on /mypath but NGINX doesn’t seem to actually proxy requests.

The nginx configuration:

upstream backend_website {
        server 127.0.0.1:5173;
}

upstream backend_api {
        server 127.0.0.1:5001;
}


# HTTP
#
server {
       listen 80;
       listen [::]:80;

       server_name example.com;

       # Redirect to HTTPS (:443)
       return 301 https://$host$request_uri;
}

# HTTPS
#
server {
        listen 443 ssl;
        listen [::]:443 ssl;

        ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

        server_name example.com;

        location / {
                include proxy_params;
                proxy_pass https://backend_website;
        }

        location /mypath {
                include proxy_params;
                proxy_pass https://backend_api;
        }
}

The index.ts file for the service

import cors from "cors";
import express from "express";
import { config } from "config";
import { MyController } from "controllers/my.controller";
import * as https from "https";
import * as fs from "fs";

const certificate = fs.readFileSync("/etc/letsencrypt/live/example.com/fullchain.pem", "utf8");
const privateKey = fs.readFileSync("/etc/letsencrypt/live/example.com/privkey.pem", "utf8");

const app = express();
app.use(express.json());
app.use(cors());
app.use("/", MyController );

const httpsServer = https.createServer({
    cert: certificate,
    key: privateKey
}, app);

httpsServer.listen(config.PORT, "127.0.0.1", () => console.log(`Running api on https://127.0.0.1:${ config.PORT }/`));

However, the server does not proxy requests to the API. It works flawlessly on the root path (that one uses Vite), but /mypath doesn’t work when opening the URL I just get Cannot GET /mypath. I opened 127.0.0.1:5001 using lynx and it does work. So the problem is somewhere around NGINX.

2

Answers


  1. Chosen as BEST ANSWER

    I found the answer. For some unintuitive reason, you have to specify Express should serve on /mypath/ even though you already tell that in the NGINX config file.

    So this code

    const app = express();
    app.use(express.json());
    app.use(cors());
    app.use("/", MyController );
    
    const httpsServer = https.createServer({
        cert: certificate,
        key: privateKey
    }, app);
    
    httpsServer.listen(config.PORT, "127.0.0.1", () => console.log(`Running api on https://127.0.0.1:${ config.PORT }/`));
    

    Becomes that

    const app = express();
    app.use(express.json());
    app.use(cors());
    app.use("/mypath/", MyController );
    
    const httpsServer = https.createServer({
        cert: certificate,
        key: privateKey
    }, app);
    
    httpsServer.listen(config.PORT, "127.0.0.1", () => console.log(`Running api on https://127.0.0.1:${ config.PORT }/`));
    

  2. You need to edit the /etc/nginx/site-enabled/ file

    
     cd /etc/nginx/site-enabled/
     sudo nano default
    
    
        location /wishfoundationindia/ {
                    proxy_pass http://localhost:8000/;
                    proxy_buffering off;
                    proxy_set_header X-Real-IP $remote_addr;
                    proxy_set_header X-Forwarded-Host $host;
                    proxy_set_header X-Forwarded-Port $server_port;
            }
    
            location /lms/ {
                    proxy_pass http://localhost:7000/;
                    proxy_buffering off;
                    proxy_set_header X-Real-IP $remote_addr;
                    proxy_set_header X-Forwarded-Host $host;
                    proxy_set_header X-Forwarded-Port $server_port;
            }
    
    

    then edit in location function with the help of this pic.
    rfr image

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search