Nginx reverse proxy and subdomains

You may want to check your DNS setup.

I also use multiple subdomains which I create on the fly for various applications using web frameworks such as Python Django or Ruby on Rails.

A typical example is at mydomain.com I may want to have myapp.mydomain.com where myapp is a framework served at my_server_IP:some_port.

In order to achieve such thing (many subdomains on the fly at the same OR different ports) I delegate the decision from the DNS to the Web server with a Wildcard DNS record see Wikipedia. As the name suggests, such record produces a catch all domain which can be easily managed from the web server with the use of virtual hosts - domain proxys, etc.

An A wildcard record at GoDaddy's DNS management tool looks like this:

Host Points To TTL

* YOUR_SERVER_IP 1 Hour

And an Nginx configuration file which passes all requests for app.mydomain.com to otherserver.com:9000/index.html

The result: you type in browser http://app.mydomain.com and Nginx serves content from otherserver.com:9000/index.html which can be another server or application, etc

server {
    listen   80;

    server_name app.mydomain.com www.app.mydomain.com;

    access_log  /var/log/nginx/app_mydomain_com_access.log;
    error_log   /var/log/nginx/pp_mydomain_com_error.log;

    error_page 404 /404.html;

    error_page 500 502 503 504 /50x.html;

    location / {
        proxy_pass http://otherserver.com:9000/index.html;
    }

}

DNS were all rights, I was doing some trial in local domain before putting them on production.. I solved the issue more easier (I don't have index.html or any other files, I have only folder name).. Finally, I think this would be helpful for someone, the configuration is:

upstream tomcat_server {
    server tomcat.domain.com:8080;
}
server {
    listen 80;
    server_name app1.domain.com;
    location / {
        proxy_pass http://tomcat_server/app1/;
        sub_filter /app1/  /;
    }
}

The " /" at the end of proxy_pass is necessary ...

sub_filter is needed to avoid that the result would be something like

   app1.domain.com/app1

That's all.. I'm trying to improve this configuration for app working with tomcat 6, because the use of subdomains could generate (as in my case) some disease in the apps.