tasmcan
By:
tasmcan

How to register lxc address to my domain addresses in Ubuntu

January 18, 2015 770 views

Hi there,
I'm newbie in web and DNS things. I have a Ubuntu droplet. I created 2 LXC which they gives me web services. I entered nat-pat configuration for them.
If you enter http://ipaddress:2082 goes lxc1
http://ipaddress:2083 goes lxc2

abc.com and def.com are my domains . I want to register these domains to my lxcs.
Is it possible ? If yes, how ?

abc.com:2082 and :2083 works well now

Regards

1 Answer

In order to access a site via the bare domain without a port number in the URL, the service must be listening on port 80 (for HTTP) or 443 (for HTTPS). Only one service can be listen to a port at a time. So in order to do what you want, you'll need to use a reverse proxy of some sort. Nginx is a good fit for this. Let it listen on port 80 and then direct the request to the appropriate service based on the domain name. Here's a really basic example:

upstream app_server_one {
    server 127.0.0.1:2082 fail_timeout=0;
}

upstream app_server_two {
    server 127.0.0.1:2083 fail_timeout=0;
}

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;
    server_name abc.com;

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://app_server;
    }
}

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;
    server_name def.com;

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://app_server_two;
    }
}

Note that setting the server_name directive is important here.

For more info on Nginx, check out:

by Justin Ellingwood
Nginx is an incredibly powerful web server and reverse proxy. In this guide, we'll discuss how to set up server blocks on Ubuntu 14.04 in order to server multiple domains from a single server.
Have another answer? Share your knowledge.