Question

How to send requests to node js from react server by nginx with ssl?

Posted May 18, 2021 182 views
NginxNode.jsReact

I have an app with an nginx server with ssl setup serving files generated from a react project. The front end is supposed to interrogate a node back end by sending requests to port 8000 on same machine.

  • How should I modify CORS defintion using node to enable the front end to send requests to node js?
  • Should I modify the requests from front end to use https instead of http?
  • Should I modify nginx config file to enable the front end to communicate with react?

Here node-js code:

const express = require("express");
const bodyparser = require("body-parser");

var app = express();
var sleep = require("system-sleep");

const usersRouter = require("./api-routes");


if(process.env.IP_ADDRESS)
{
    IP_ADDRESS = "http://"+process.env.IP_ADDRESS
}
else
{
    IP_ADDRESS= "http://localhost:3000"
} 
console.log("IP ADDRESS FROM GLOBAL VAR")
console.log(process.env.IP_ADDRESS)

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

var cors = require("cors");
app.use(
  cors({
    origin: [IP_ADDRESS],
    credentials: true,
  })
);

//"http://localhost:3000"
app.use(function (req, res, next) {
  res.header("Access-Control-Allow-Origin", IP_ADDRESS);
  res.header("Access-Control-Allow-Headers", true);
  res.header("Access-Control-Allow-Credentials", true);
  res.header(
    "Access-Control-Allow-Methods",
    "GET, POST, OPTIONS, PUT, PATCH, DELETE"
  );
  next();
});

app.use("/", usersRouter);


const port = process.env.PORT || 8000;
app.listen(port, () => console.log(`Listening on port ${port}..`));


Here is nginx config file in sites-enabled:

server {
        #listen [::]:80;
        server_name domain_name www.domain_name;
        root /var/www/react_app/html;
        index index.html;

        #server_name affiliates-pal.com;
        access_log /var/log/nginx/react-app.access.log;
        error_log /var/log/nginx/react-app.error.log;
        location / {

                try_files $uri /index.html;

        }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/domain_name/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/domain_name/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot


}

server {
    if ($host = www.domain_name) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = domain_name) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


        listen 80;
        server_name domain_name www.domain_name;
    return 404; # managed by Certbot




}

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
1 answer

Hi there,

What you could do is to use an Nginx reverse proxy so that the requests to /api would be proxied to your NodeJS application. That way you will be able to keep your SSL certificate as the requests would be going through the same domain and the reverse proxy would handle the rest.

This could be achieved with he following reverse proxy definition:

location /app {
    proxy_pass http://localhost:800;
    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;
}

For more information on how to setup NodeJS with Nginx you could take a look at this step by step tutorial here:

https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-16-04

Hope that this helps!
Regards,
Bobby

by Brennen Bearnes
Node.js is an open source JavaScript runtime environment for easily building server-side and networking applications. Node.js applications can be run at the command line but this guide focuses on running them as a service using PM2, so that they will automatically restart on reboot or failure, and can safely be used in a production environment.