Multiple domains on different ports of the same droplet.

June 24, 2014 5.4k views

Hello,

I'm running different apps on different ports of my droplet. Everything works fine, but I have to specify the ports explicitly in order to visit the website. How can I make a specific domain point to a specific port?

Configuration:
Apps developed in Python 2.7, using Flask.
Deployed on CherryPy server. Right now, there are 2 instances of CherryPy, each running different app on different ports (one, the default 80, and another on 8080).

Any help?

1 comment
  • I had two applications running, one on port 8081 and one on port 8082
    With the following nginx.conf, I was able to effectively assign each app to a separate domain:

    $ cat /etc/nginx/nginx.conf
    
    worker_processes  1;
    
    events {
        worker_connections  1024;
    }
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
    
        sendfile        on;
    
        keepalive_timeout  65;
    
        server {
            listen      80;
            server_name domain1.com;
            location / {
                proxy_set_header   X-Real-IP $remote_addr;
                proxy_set_header   Host      $http_host;
                proxy_pass         http://127.0.0.1:8081;
            }
            root /var/www/domain1;
        }
    
        server {
            listen       80;
            server_name domain2.com;
            location / {
                proxy_set_header   X-Real-IP $remote_addr;
                proxy_set_header   Host      $http_host;
                proxy_pass         http://127.0.0.1:8082;
            }
            root /var/www/domain2;
        }
    }
    

    FYI: Each server was run in a docker container, and nothing was located at /var/www

1 Answer

You could deploy Nginx in front of the CherryPy servers to act as a reverse proxy and direct traffic. Basically, you'd need to have the CherryPy servers listen on some random port on localhoast, then configure Nginx to pass requests to those ports based on domain names.

The Nginx configuration would look something like:

<pre>
upstream first_server {
server 127.0.0.1:3000;
}

upstream second_server {
server 127.0.0.1:4000;
}

server {
listen 80;
server_name domain-one.com;

# Settings to serve static files 
location ^~ /static/  {
    # root /full/path/to/application/static/file/dir;
    root /app/static/;
}

location / {
    proxy_pass         http://firstp_server;
    proxy_redirect     off;
    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-Host $server_name;
}

}

server {
listen 80;
server_name domain-two.com;

# Settings to serve static files 
location ^~ /static/  {
    # root /full/path/to/application/static/file/dir;
    root /app/static/;
}

location / {
    proxy_pass         http://second_server;
    proxy_redirect     off;
    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-Host $server_name;
}

}
</pre>

  • I have same problem but am using unicorn, so how can i get it fixed.

    ISSUE DETAIL: I have three rails app running on diff ports 104.131.74.69:80 - hailisys.com, 104.131.74.69:443 - idocere.com and 104.131.74.69:444 on a single droplet, but the problem is that clicking on idocere.com opens the app deployed on hailisys.com ie 104.131.74.69:80.

    Here is my nginx.conf
    Any help will be appreciated.

Have another answer? Share your knowledge.