How do I host multiple Node.js sites on the same IP/server with different domains?

前端 未结 12 684
轻奢々
轻奢々 2020-11-28 01:03

I have a linux server with a single IP bound to it. I want to host multiple Node.js sites on this server on this IP, each (obviously) with a unique domain or subdomain. I wa

相关标签:
12条回答
  • 2020-11-28 01:22

    Hm ... why you think that nodejs should act as a proxy. I'll suggest to run several node apps listening on different ports. Then use nginx to forward the request to the right port. If use a single nodejs you will have also single point of failure. If that app crashes then all the sites go down.

    0 讨论(0)
  • 2020-11-28 01:26

    Based on @Michaaatje and @papiro, a very easy way:

    Say you have some typical pages such as...

    var app = express()
    app.use(sess)
    app.use(passport.initialize())
    app.use(passport.session())
    app.use('/static', express.static('static'))
    
    app.get('/', ensureLoggedIn("/loginpage"), function(req, res, next) {
        ...
    })
    
    app.get('/sales', ensureLoggedIn("/loginpage"), function(req, res, next) {
        ...
    })
    
    app.get('/about', ensureLoggedIn("/loginpage"), function(req, res, next) {
        ...
    })
    
    app.post('/order', ensureLoggedIn("/loginpage"), urlencodedParser, (req, res) => {
        ...
    })
    

    .. and so on.

    Say the main domain is "abc.test.com"

    But you have an "alternate" domain (perhaps for customers) which is "customers.test.com".

    Simply add this ...

    var app = express()
    app.use(sess)
    app.use(passport.initialize())
    app.use(passport.session())
    app.use('/static', express.static('static'))
    
    app.use((req, res, next) => {
        req.isCustomer = false
        if (req.headers.host == "customers.test.com") {
            req.isCustomer = true
        }
        next();
    })
    

    and then it is this easy ...

    app.get('/', ensureLoggedIn("/loginpage"), function(req, res, next) {
        if (req.isCustomer) {
            .. special page or whatever ..
            return
        }
        ...
    })
    
    app.get('/sales', ensureLoggedIn("/loginpage"), function(req, res, next) {
        if (req.isCustomer) {
            res.redirect('/') .. for example
            return
        }
        ...
    })
    
    app.get('/about', ensureLoggedIn("/loginpage"), function(req, res, next) {
        if (req.isCustomer) { ... }
        ...
    })
    
    app.post('/order', ensureLoggedIn("/loginpage"), urlencodedParser, (req, res) => {
        if (req.isCustomer) { ... }
        ...
    })
    

    Thanks to @Michaaatje and @papiro .

    0 讨论(0)
  • 2020-11-28 01:29

    First install forever and bouncy.

    Then write a startup script. In this script, add a rule to the iptables firewall utility to tell it to forward the traffic on port 80 to port 8000 (or anything else that you choose). In my example, 8000 is where I run bouncy

    sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8000
    

    Using forever, let's tell the script to run bouncy on port 8000

    forever start --spinSleepTime 10000 /path/to/bouncy /path/to/bouncy/routes.json 8000
    

    The routes.json would something like

    {
        “subdomain1.domain.com" : 5000,
        “subdomain2.domain.com" : 5001,
        “subdomain3.domain.com" : 5002
    }
    

    NodeJS application1, application2 and application3 run on port 5000, 5001 and 5002 respectively.

    The script I use in my case can be found here and you might have to change a little to fit in your environment.

    I also wrote about this in more details and you can find it here.

    0 讨论(0)
  • 2020-11-28 01:29

    This is my simplest demo project without any middleware or proxy.
    This requires only a few codes, and it's enough.

    https://github.com/hitokun-s/node-express-multiapp-demo

    With this structure, you can easily set up and maintain each app independently.
    I hope this would be a help for you.

    0 讨论(0)
  • 2020-11-28 01:29

    This guide from digital ocean is an excellent way. It uses the pm2 module which daemonizes your app(runs them as a service). No need for additional modules like Forever, because it will restart your app automatically if it crashes. It has many features that help you monitor the various applications running on your server. It's pretty awesome!

    0 讨论(0)
  • 2020-11-28 01:31

    Literally, when you get the request and response object, you can get the domain through "request.headers.host"... (not the IP address, actually the domain).

    0 讨论(0)
提交回复
热议问题