Two Websites on One IP
So let's say someone let me use their server for my website, but is already hosting another website on port 80 on that IP. How do I point my website running on a different port to my domain? I tried using a reverse proxy on nginx but it still just shows website on port 80 as opposed to mine on the other port.
12 Answers
If you take a quick look at Server Block Examples, you may notice the first example shows how NGINX can server two different domain names.
http { index index.html; server { server_name access_log logs/domain1.access.log main; root /var/www/ } server { server_name access_log logs/domain2.access.log main; root /var/www/ }
}Typically, these server blocks are separated out into their own files in a directory NGINX is configured to scan for (ie /etc/nginx/conf.d/ or /etc/nginx/sites-enabled/).
By loading up two valid server blocks which each have a different server_name will result in NGINX serving both (or more) domains.
I always check Pitfalls and Common Mistakes when creating configurations as well.
Without more information on what kind of websites you are talking about (static HTML websites, NodeJS / PHP / Python / Ruby / YouNameIt backed website / web app), is hard to guide you properly.
But assuming you are talking about static HTML websites, there is a concept called Virtual Hosting, which means that the same WebServer (like Apache or Nginx) can host multiple domains on the same port.
So if you are using Apache, you can create two sites / virtual hosts, one for each of the websites. They will have their own config as part of Apache config (usually inside /etc/apache2), and they can be stored in different directories in the server. When a request arrives, Apache will know which website to server based on the Host header that the browser sends automatically (which contains the domain).
On the other hand, if you need to host two different web applications, say a NodeJS running on port 80 (would be strange, since you usually want to run those in high ports like 3000, 8000, 8080, 9000 so it doesn't have to run as root) and a Rails application, and you want then both to be accessible on port 80 (so the end user doesn't have to use a specific port when browsing your websites), then, you would have to change the NodeJS application to listen on some other port.
Then, add a reverse proxy like you mentioned (I usually prefer Nginx or Traefik, you could also try Caddy) to listen on port 80. If you use Traefik or Caddy and expose port 443 as well, you get the extra benefit of automatic SSL certs with Let's Encrypt, so your websites will go through a secure channel.
This reverse proxy, will then forward requests to whatever destination you select (static files, another port like where NodeJS is listening, etc) based on rules you configure, like the Host header.
Hope this helps
4