user1940163
user1940163

Reputation: 191

How to access localhost:port by domain names

I have localhost:8080 forwarded to mysite1.myhost1.com and localhost:9000 forwarded to mysite2.myhost2.com

I am able to access the websites by localhost:8080 and localhost:9000 resp.

What I want is I want to type mysite1.myhost1.com in browser and it should redirect to localhost:8080 ... that will in turn forward to real mysite1.myhost1.com... Similarly I want to type mysite2.myhost2.com in browser and it should redirect to localhost:9000 ... that will in turn forward to real mysite2.myhost1.com.

Is this possible? I am using apache webserver Please let me know how to achieve this?

Upvotes: 1

Views: 224

Answers (1)

AP2411
AP2411

Reputation: 31

It sounds like your solution would be using Apache's Reverse Proxy.

First you have to enable the Apache Proxy Module:

sudo a2enmod proxy

By adding this to your Virtual Hosts file you can pass a request to a different backend server (for example localhost:8080):

ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/

If you also want to pass Websocket requests you can add the following below:

RewriteEngine on
RewriteCond %{HTTP:Upgrade} websocket [NC]
RewriteCond %{HTTP:Connection} upgrade [NC]
RewriteRule .* ws://localhost:8080%{REQUEST_URI} [P]

Your final config will look similar to this (for mysite1.myhost1.com):

<VirtualHost *:80>
        ServerName mysite1.myhost1.com

        ProxyPreserveHost On
        ProxyPass / http://localhost:8080/
        ProxyPassReverse / http://localhost:8080/

        RewriteEngine on
        RewriteCond %{HTTP:Upgrade} websocket [NC]
        RewriteCond %{HTTP:Connection} upgrade [NC]
        RewriteRule .* ws://localhost:8080%{REQUEST_URI} [P]

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Remember that I used http over https in my example for simplicity, but https is the recommended way.

Upvotes: 2

Related Questions