'Website (nginx) and nodejs REST API on the same server

I want to serve a gatsby.js static website using nginx and a nodejs REST API on the same server.

Currently I have the REST API running with a SSL certificate using Let's Encrypt. It is listening to port 80 and 443.

Now I want to serve the website but I found that the port 80 is already used by the nodejs server.

How can I handle this? Any idea on how to do it?



Solution 1:[1]

I see 2 solutions to your problem,

  1. You make your REST API listen on a different port than 80/443, so that your Gatsby application can use it. You should be able to do this in the configuration of your BACKEND application.

  2. You create a sub-domain for your REST API using your DNS provider such as cms.mysite.com which will also use ports 80/443.

The domain name of the frontend application will be mysite.com.

You configure your Nginx server to serve both applications (BACKEND + FRONTEND) depending on which subdomain is making the request. Here is an example of the Nginx default.conf configuration file:

upstream backend {
    server web:8000;
}

server {
    listen 80;
    server_name cms.mysite.com;

    location / {
        return 301 https://$host$request_uri;
    }

    ...
}

server {
    listen 443 ssl;
    server_name cms.mysite.com;

    ...
}

server {
    listen 80;
    server_name mysite.com;

    location / {
        return 301 https://$host$request_uri;
    }

    ...
}

server {
    listen 443 ssl;
    server_name mysite.com;

    ...

}

This is my first answer to the community, I hope it will help you !

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dieudonné Twahirwa