Question

Multiple domains on different ports of the same droplet.

  • Posted June 24, 2014

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?

Subscribe
Share

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


Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

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>