webutek
By:
webutek

How do I set my domain to point to the VPS' IP address with also a port and path specified?

February 24, 2017 700 views
DNS Ubuntu 16.04

I've tried adding a SRV record, but the form prevents setting the destination to @ (the base host).

2 Answers
jtittle1 February 24, 2017
Accepted Answer

@webutek

If you're running an application that runs on a port other than port 80 or 443 (HTTP and HTTPS), and the application isn't designed to run on those ports (i.e. most NodeJS apps, for example), then what you'd want to do is setup a proxy. This would essentially accept a request on port 80/443 and then proxy it to the port that your application is running on.

Most DNS providers, other than Amazon, CloudFlare, and potentially Google (not 100% on as I've not used their DNS in a while) won't allow you to point anything other than you domain to root (i.e. @).

DigitalOcean falls in to that category as of this post (i.e. it's not allowed).

Using a proxy (i.e. setting up NGINX as a proxy, for example), will allow you to setup a server block such as:

upstream myapp {
    server http://localhost:2623;
}

server
{
    listen                          80 default_server;
    listen                          [::]:80;
    server_name                     _;

    root                            /home/nginx/htdocs/public;

    location /
    {
        proxy_pass myapp;
        proxy_connect_timeout 59s;
        proxy_send_timeout 600;
        proxy_read_timeout 600;
        proxy_buffer_size 64k;
        proxy_buffers 16 32k;
        proxy_busy_buffers_size 128k;
        proxy_temp_file_write_size 64k;
        proxy_pass_header Set-Cookie;
        proxy_redirect off;
        proxy_set_header Accept-Encoding '';
        proxy_ignore_headers Cache-Control Expires;
        proxy_set_header Referer $http_referer;
        proxy_set_header Host $host;
        proxy_http_version 1.1;
        proxy_hide_header X-Powered-By;
        proxy_set_header Cookie $http_cookie;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_no_cache $http_pragma $http_authorization;
        proxy_cache_bypass $http_pragma $http_authorization;
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504 http_404;
    }
}

What the above would do is take a request on Port 80, proxy it to the myapp backend block, which sets the 'server' to use -- in this case, it'd be http://localhost:2623, which would be an application that is running on port 2623.

You can setup multiple upstream blocks to proxy to different applications and you can setup multiple location blocks to handle different paths.

For example, we could add:

upstream myapp2 {
    server http://localhost:2624;
}

Create a new location block that uses it at a path other than /:

location /newpath {
    proxy_pass myapp2;
}

Now when a request for /newpath is made, it'll respond with the application running on 2624 as per the new upstream block we just set up.

That's one way of doing it and keeping ports off the URL.

You can't, your webserver determines that.

Have another answer? Share your knowledge.