How to make BrowserSync work with an nginx proxy server?

后端 未结 4 1549
北荒
北荒 2020-12-13 16:10

(If needed, please see my last question for some more background info.)

I\'m developing an app that uses a decoupled front- and backend:

  • The backend is
4条回答
  •  盖世英雄少女心
    2020-12-13 16:43

    To get more control over how opening the page is done, use opn instead of browser sync's mechanism. Something like this (in JS - sorry, my Coffee Script is a bit rusty):

    browserSync({
        server: {
            // ...
        },
        open: false,
        port: 3001
    }, function (err, bs) {
        // bs.options.url contains the original url, so
        // replace the port with the correct one:
        var url = bs.options.urls.local.replace(':3001', ':3002');
        require('opn')(url);
        console.log('Started browserSync on ' + url);
    });
    

    I'm unfamiliar with Nginx, but according to this page, the solution to the second problem might look something like this:

    map $http_upgrade $connection_upgrade {
        default upgrade;
        '' close;
    }
    
    server {
        # ...
    
        # BrowserSync websocket
        location /browser-sync/socket.io/ {
            proxy_pass http://localhost:3001;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "Upgrade";
        }
    }
    

提交回复
热议问题