By d4ng3r09
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.
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
}
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!
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
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.