Hello, how can i point several subdomains to ip:port, for example
api.domain.com -> 22.333.33.222:3000
app.domain.com -> 22.333.33.222:3001

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.

×
2 answers

Hi @klapovsciuk,

You can redirect your domain to a certain port. This depends on the WebService you are using -Nginx/Apache. If you are using Nginx, you’ll need to do add a server block to your Nginx’s website config. This can be achieved by using the bellow

location /{
    proxy_pass  http://127.0.0.1:9000/;
}

Having said that, you’ll need to add it to your subdomain’s Nginx server block/config. If you are unsure how you can do that, I’ll recommend checking out he following article:

https://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-server-blocks-virtual-hosts-on-ubuntu-16-04

If you are using Apache, you have two options, the first one is to add a redirection rule in your website’s .htaccess and the second one would be to do it directly in the Apache’s Vhost file. I like using the first option. In your .htaccess file, you can add the following rule

RewriteEngine on

# redirect to 3000 if current port is not 3000 and "some-prefix/" is matched
RewriteRule ^/(.*[^/])/?$ http://www.mysite.com:3000/$1/ [R=301,L]

If you want to use Apache’s Vhost file, I’ll recommend going through the following tutorial :

https://www.digitalocean.com/community/tutorials/how-to-set-up-apache-virtual-hosts-on-ubuntu-16-04

Regards,
KDSys

by Brennen Bearnes
The Apache web server is the most popular way to serve web content on the internet. Apache has the ability to serve multiple domains from a single server by using a mechanism called "virtual hosts". If a virtual host is configured correctly for each domain, the web server can correctly route traffic to the appropriate files based on the domain name requested. In this guide, we'll demonstrate how to configure Apache virtual hosts on an Ubuntu 16.04 server.

Hi there @klapovsciuk,

What you can do is:

  • Set up your DNS so that your subdomain names point to the server IP address:
api.domain.com -> 22.333.33.222
app.domain.com -> 22.333.33.222
  • Then on the server itself set up a reverse proxy with Nginx for example to proxy the traffic for your domains to the respective ports accordingly:

https://www.digitalocean.com/community/tutorials/how-to-configure-nginx-as-a-web-server-and-reverse-proxy-for-apache-on-one-ubuntu-18-04-server

Hope that this helps!
Regards,
Bobby

by Jesin A
In this tutorial you'll configure Nginx as both a web server and as a reverse proxy for Apache to host four domains on a single server.
Submit an Answer