Reputation: 101
I have a website at https://mydomain.co.uk
This has a form which uses nodemailer, a server.js file, an app.js file to handle the formdata.
I have been following this tutorial, to leverage nginx as a reverse proxy. I have included the following in my mydomain.co.uk
server block
location / {
proxy_pass http://localhost:80;
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;
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
# try_files $uri $uri/ =404;
}
And now when I try to visit https://mydomain.co.uk
I have a too many redirects error.
I have run certbot on mydomain.co.uk
and before I attempted to set up the reverse proxy, it was running absolutely fine.
I know the proxy config is the reason for the error in my browser, However I don't know how to set up the config.
I have already looked at this answerbut cant see how it applies to what I'm doing.
Extras
I have the server.js
file running with pm2
and I have set the env to production with
pm2 start ecosystem.config.js --env production
the ecosystem.config.js
looks like this
module.exports = {
apps : [
{
name: "myserver",
script: "server.js",
watch: true,
env: {
"PORT": 3000,
"NODE_ENV": "development"
},
env_production: {
"PORT": 80,
"NODE_ENV": "production",
}
}
]
}
What am I missing?
EDIT
Since following the answer below my config file now looks like this
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
# try_files $uri $uri/ =404;
proxy_pass http://localhost:8080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_cache_bypass $http_upgrade;
add_header X-Cache-Status $upstream_cache_status;
}
and my server.js file now looks like this
var path = require("path");
const express = require("express");
const app = express();
const nodemailer = require("nodemailer");
const PORT = process.env.PORT || 5500; // CHANGE TO WHATEVER
// middleware
var htmlPath = path.join(__dirname, "/html");
// app.use(express.static("public"));
app.use(express.static(htmlPath));
app.use(express.json());
However now, express seems to be only serving one single html file, and not the whole html directory..
The reverse-proxy is working, as it is serving from express, but what do I need to do to ensure express serves the whole html directory?
Upvotes: 0
Views: 188
Reputation: 781
Hi dude :) You need proxy pass to the node's port. You uncorrecyly configure port and directives in nginx right now. However, nginx sending request to 80 , so , it is default , and again.. and it repeat as loop infinite.
Correctly directives
server {
listen 80;
listen [::]:80;
server_name mydomain.co.uk;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
index index.html;
charset utf-8;
# There 3000 port your development nodejs running port
location / {
proxy_pass http://localhost:3000/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_cache_bypass $http_upgrade;
}
}
Upvotes: 1