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

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");

	IP_ADDRESS = "http://"+process.env.IP_ADDRESS
	IP_ADDRESS= "http://localhost:3000"

    extended: true,

var cors = require("cors");
    origin: [IP_ADDRESS],
    credentials: true,

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);

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;

        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


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

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:

Hope that this helps! Regards, Bobby