How to handle domain and sub-domain based applications with reverse proxy ?

Tarikul Islam
Oceanize Lab Geeks
Published in
2 min readMar 27, 2018

Use Case1: I have a Sass application. When create a new account I want to add this as sub-domain. Like my account link is www.example.com/tarikul But I want to browse this as a sub-domain like www.tarikul.example.com

Use Case 2: I have a application which have different pars. But I want to browse those pars from separate domain like I have www.example.com/blogs and www.example.com/recruit But I want to browse those link as follows

www.example.com/recruit   =>    www.example-recruit.com 
www.example.com/blogs => www.my-blogs.com

By using Reverse Proxy we can solve those 2 use case. Here I will show how to achieve those 2 configuration with Reverse Proxy in Nginx server

Use case 1 configurations

if your main domain is www.example.com and your account url is www.example.com/tarikul and you want’s to browse it like www.tarikul.example.com

server {
listen 80;
server_name tarikul.example.com;
location ~* \.(js|html|ico|gif|jpg|png|css|pdf|map)$ {
proxy_pass http://example.com$request_uri;
}
location / {
proxy_pass http://example.com/tarikul$request_uri;
}
}

Use case 2 configurations

If your main domain/application is www.example.com and your blog link is www.example.com/blogs and you want’s to browse it like www.my-blogs.com then the configuration will be

Note: In www.my-blogs.com domain panel. it should be add/pointed DNS record at nginx server

server {
listen 80;
server_name my-blogs.com;
location ~* \.(js|html|ico|gif|jpg|png|css|pdf|map)$ {
proxy_pass http://example.com$request_uri;
}
location / {
proxy_pass http://example.com/blogs$request_uri;
}
}

Explanation

1. Listen : refers the incoming request ports in may be HTTP, HTTPS, SSH

2. server_name: Important part. the requesting domain

3. Location : this is the path route. location /info/{ this means if we browse www.my-blogs.com/info then those conditions will applied

location ~* \.(js|html|ico|gif|jpg|png|css|pdf|map)$ {
proxy_pass http://example.com$request_uri;
}

this part means all asset request conditions will go through. if request is www.my-blogs.com/style.css then the request will go internally www.example.com/style.css

4. proxy_pass : This is very important part of this proxy setting. proxy pass associate URL so that it can request internally the proxyed server.

--

--