I’ve started to experiment with nginx on my windows 11 machine.
What I wanted is the following:
- Given is some real website.
- All requests that start with realwebsite.com/magic, should instead go to localhost:3000
- But all other requests that start with realwebsite.com should go to the real website
What I have tried:
- First, I set up a server on localhost:3000 and confirmed it is working.
- Edited my hosts file in C:WindowsSystem32driversetc and added 127.0.0.1 realwebsite.com
- Edited nginx.conf by adding:
`server {
listen 80;
server_name realwebsite.com;
# send magic requests to localhost server
location /magic{
proxy_pass http://localhost:3000;
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_set_header X-Forwarded-Proto $scheme;
}
# send all other requests to the real website
location / {
proxy_pass https://www.realwebsite.com;
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_set_header X-Forwarded-Proto $scheme;
}
}`
Expected Behevioour:
when i visit realwebsite.com/magic, browser should display some test.html file
Actual Behaviour:
Site cant be reached and the server running on localhost:3000 doesn’t log any get request when entering realwebsite.com/magic
But when I visit localhost:3000 directly, the server does log the get request
My local server is simply set up like this:
const express = require('express');
const path = require('path');
const app = express();
const PORT = 8080;
const BASE_DIR = 'C:\Users\me\Desktop\Projects\test\server';
app.use('/magic', express.static(BASE_DIR));
app.use((req, res, next) => {
console.log(`${req.method} ${req.url}`);
next();
});
app.get('/magic/:filename', (req, res) => {
const filePath = path.join(BASE_DIR, req.params.filename);
res.download(filePath, (err) => {
if (err) {
console.error(err);
res.status(404).send('File not found!');
}
});
});
app.get('/magic', (req, res) => {
const filePath = path.join(BASE_DIR, 'test.html');
res.sendFile(filePath, (err) => {
if (err) {
res.status(err.status).send('File not found!');
}
});
});
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});
It looks like you’re missing the slash at the end of the location
and proxy_pass
settings. The following configuration works for me.
Nginx is configured to run on port 8000
while the localhost
server is configured to run on port 8080
.
events {
worker_connections 1024;
}
http {
server {
listen 8000;
server_name localhost;
location /magic/ {
proxy_pass http://localhost:8080/;
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_set_header X-Forwarded-Proto $scheme;
}
location / {
proxy_pass https://www.google.com/;
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_set_header X-Forwarded-Proto $scheme;
}
}
}
=== ORIGINAL ANSWER ===
It sounds like Nginx may be running somewhere other than on your local computer.
Since you’re using Nginx as a proxy (not redirecting), Nginx itself needs access to the proxy_pass
host. If Nginx isn’t running locally, then it doesn’t have access to localhost
.
Think about it this way:
Request: Browser => Nginx => http://localhost:3000
Response: Browser <= Nginx <= http://localhost:3000
localhost
is a loopback to 127.0.0.1
, which is not a publicly accessible IP address.
You have two options:
- Run Nginx locally. I’m not sure if this allows you to work the way you need to.
- If you have a publicly accessible static IP address for your computer, you could use that in Nginx instead of
localhost
.
I hope that helps.
27450604 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
9