question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Proxying multiple subdomains

See original GitHub issue

Hi,

I’m working on a project which uses multiple subdomains for local development.

root domain example: .ecommerce.domain.local specific site domain: gb.websitename.ecommerce.domain.local asset domain: css.websitename.ecommerce.domain.local

The subdomains all resolve to the root domain because they point at the same vm ip (via hosts file entries)

I can’t get browsersync to load my site on a device using the external url unless that device is a vm running on my machine. If it’s my iPhone it fails to load.

Here’s my browsersync Gulp settings

browserSync.init({
        proxy: gb.websitename.ecommerce.domain.local,
        notify: false,
        startPath: '/'
    });

I can manage to proxy this setup via proxylocal and proxyreverse. Proxy reverse takes the site domain and uses the --rewrite-domain flag pointing to the root domain.

Any ideas what I can configure to get this working BrowerSync?

Issue Analytics

  • State:open
  • Created 8 years ago
  • Reactions:8
  • Comments:12

github_iconTop GitHub Comments

5reactions
sclu1034commented, Nov 18, 2016

Proxying subdomains is possible with http-proxy-middleware. Get the requested domain from req.headers.host and replace host/port/whatever with your proxy url:

let proxy = require('http-proxy-middleware');
bs.init({
    middleware: [
      function (req, res, next) {
        let target = 'http://' + req.headers.host.replace('localhost:3000', 'example.com');
        proxy({ target })(req, res, next);
      }
    ]
});
1reaction
nickpishcommented, Feb 15, 2019

To further highlight the above: for my project running a local WordPress multisite config via WAMP, I used the following slight modification of the above code by @SirAiedail and it works beautifully- in this case using any sub-domain of wp-multisite.local such as site1.multisite.local by accessing site1.localhost:3000 in the browser. I just needed to add changeOrigin: true as in @lukepolo’s post. Thank you for the wonderfully helpful thread!

middleware: [
    function(req, res, next) {
        let target = 'http://' + req.headers.host.replace('localhost:3000', 'wp-multisite.local');
        proxy({
            target,
            changeOrigin: true
        })(req, res, next);
    }
],
Read more comments on GitHub >

github_iconTop Results From Across the Web

nginx reverse ssl proxy with multiple subdomains - Server Fault
I would do something like this (tested with nginx 1.4.2, seems to work): server { listen 127.0.0.1:443 ssl; server_name site1.example.com; ...
Read more >
How to build a web app with multiple subdomains using Nginx
Providing multiple, custom subdomains to clients can be a bit tricky to configure ... For our article, we will use Nginx as a...
Read more >
Reverse Proxy with multiple subdomain - Caddy Community
Hi, new user here. I'm trying to do multiple reverse proxy with subdomains. I'm able to get reverse proxy working with single IP...
Read more >
Hosting from Home the Easy Way with subdomains ... - YouTube
Hosting your website, weather station, photo collection and more on multiple devices at home can get complicated when you want to have ...
Read more >
Rank & File: Why a Reverse Proxy is Better than a Subdomain
I've been a part of too many projects where we have seen migrations in both directions and the subdomain -> subdirectory always yields...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found