Im currently in the last stages of setting up my website and I cant get nginx to work and its killing me
I have a very simple crud app, react front end, express backend, and obviously im using nginx as a reverse proxy
All routes in the server looks like
app.get('/GetProfile', (req, res) => {...})
standard stuff
and it works on local host
and whats even weirder is the app can make a get request to a single route called /GetAllProfiles but none of the others despite being setup the exact same way
http {
include mime.types;
default_type application/json;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80 default_server;
listen 443 ssl default_server;
listen [::]:80 default_server;
server_name musiclovers.studio;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
ssl_certificate C:\Users\Administrator\musiclovers\sslcert\cert.pem; # REPLACE HERE
ssl_certificate_key C:\Users\Administrator\musiclovers\sslcert\privkey.pem; # REPLACE HERE
try_files $uri $uri/ /index.html =404;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_pass http://localhost:888/;
proxy_set_header content-type "application/json";
proxy_ssl_session_reuse off;
proxy_pass_header Server;
proxy_cache_bypass $http_upgrade;
}
}
}
and whats even weirder is that all the requests im making log properly
72.230.78.122 - - [30/Apr/2023:01:56:04 +0000] "GET /GetAllProfiles?ID=2 HTTP/1.1"
304 0 "http://localhost:3000/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36"
72.230.78.122 - - [30/Apr/2023:01:56:08 +0000] "GET /GetProfile?id=1 HTTP/1.1" 304 0
"http://localhost:3000/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36
(KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36"
72.230.78.122 - - [30/Apr/2023:01:56:08 +0000] "GET /GetSong/?audioId= HTTP/1.1" 304 0
"http://localhost:3000/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36
(KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36"
(the mismatched "ID" casing is proper because im bad at consistancy but even then id still get a debug about a request being made because I console.log in every route, get song might be a little different but right now im just hyperfocused on getting /GetProfile setup, which on all ends - frontend and backend - are setup almost identically so I should be at the very least seeing some kind of debugging in the console)
Im super annoyed, this reverse proxy thing has been a thorn in my side for about a week and i just want to move on. I dont get it. Why does the one request work but none of the others?
Nginx is clearly recieving the requests because it logs it
but its
just.
not.
working.
EDIT: heres some of the server code
const express = require('express')
const cors = require('cors')
const mysql = require('mysql')
const multer = require('multer')
const path = require('path')
const fs = require('fs');
const { parseArgs } = require('util')
const https = require('https');
var http = require('http');
var privateKey = fs.readFileSync('./sslcert/privkey.pem', 'utf8');
var certificate = fs.readFileSync('./sslcert/cert.pem', 'utf8');
var credentials = {key: privateKey, cert: certificate};
const app = express()
var httpServer = http.createServer(app);
var httpsServer = https.createServer(credentials , app);
var corsOptions = {
origin: '*',
optionsSuccessStatus: 200 // some legacy browsers (IE11, various SmartTVs) choke on 204
}
let uploadlocation = '';
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, './Uploads')
},
filename: (req, file, cb) => {
console.log(file)
uploadlocation = Date.now() + path.extname(file.originalname)
cb(null, uploadlocation)
},
});
const upload = multer({storage: storage});
app.use(cors(corsOptions))
app.use(express.json())
app.use(express.static(path.join(__dirname, 'build')));
const db = mysql.createConnection({
user: 'root',
password: 'password',
database: 'musicsystem',
});
app.get('/GetAllProfiles', (req, res) => {
const id = req.query.ID;
console.log("User ", id, " Has requested a list of all users")
db.query("SELECT * FROM musicpost WHERE id != ?", id ,(err,result) => {
if(err)
{
console.log(err);
} else {
res.send(result);
}
})
})
app.get('/*' , (req, res) => {
res.sendFile(__dirname + '\\build\\index.html');
})
app.get('/GetProfile', (req, res) => {
const ID = req.query.id;
console.log("Getting profile ", ID);
db.query("SELECT * FROM musicpost WHERE id = ?;", [ID], (err,result) =>
{
if(err)
{
console.log(err);
} else {
res.send(result);
}
});
})
app.get('/GetSong', (req, res) => {
const fileName = req.query.audioId;
const filepath = "./Uploads/" + fileName;
const stat = fs.statSync(filepath);
console.log(filepath);
console.log(stat);
const header = 'attachment; filename=' + fileName;
res.sendFile(fileName , {root: './Uploads/'});
})
httpServer.listen(888, "127.0.0.1");
httpsServer.listen(889, "127.0.0.1", () => {
console.log("Server is running on port 3001")
} )
The problem is in your node.js code:
you have added the wildcard route in the middle of your code like this :
app.get('/*' , (req, res) => {
res.sendFile(__dirname + '\\build\\index.html');
})
And then adding other get requests. This will not work as all the GET routes written to next will never be triggered and only this function will be triggered.
To avoid this,
try adding this wild card snippet ( which I marked above ) at the end preferably just before httpServer.listen(888, "127.0.0.1");