I want to make an express app accessible through http://website.com/app and use nginx as a reverse proxy as well as to serve static files.
This is my nginx configuration:
upstream app {
server localhost:3333;
}
server {
listen 80;
server_name website.com www.website.com;
root /var/www/website.com;
index index.html index.htm;
location = / {
try_files $uri $uri/ =404;
}
location /app/ {
alias /var/www/app/static;
try_files $uri $uri/ @app;
}
location @app {
proxy_pass http://app;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
index.js (located in /var/www/app/static/
) requests some resource via fetch("/info")
.
app.js (located in /var/www/app/
) is supposed to deliver said resouce with
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json())
app.get("/info", (req, res) => {
res.header("Content-Type",'application/json');
res.send(JSON.stringify(info));
})
app.listen(3333, () => {
console.log("Server listening at http://localhost:3333")
})
When I visit http://mywebsite.com/app, the static files are served by nginx but /info
is yielding a 404. Nginx error log: "/var/www/website.com/info" failed (2: No such file or directory).
The express app is running ($pm2 ls
-> status "online").
I assume that the proxy configuration is not correct or there might be a misunderstanding of how nginx and servers work on my side (these are my first steps). I would be grateful for advice.