Is it possible to redirect only certain requests to real websites to a webserver on localhost?

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:

  1. Run Nginx locally. I’m not sure if this allows you to work the way you need to.
  2. 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.

New contributor

27450604 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

9

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật