Nginx selective forwarding

I encountered a situation today where Nginx had to forward all requests to tomcat except couple of URL’s which will be served by static content located inside Nginx root folder. This is what I did:


server {
   server_name localhost;

   #root specifies the folder which contains all static content for Nginx. Make sure you have a folder named as calendar located inside root folder.
   root /usr/share/nginx/html; 
   index index.html index.htm;
   #This (localhost/calendar) is the URL which I wanted to be served by Nginx and not tomcat. 
   location /calendar {
      proxy_redirect off;

   location / {
      proxy_set_header X-Forwarded-Host $host;
      proxy_set_header X-Forwarded-Server $host;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_pass http://localhost:8080;

You just need to restart Nginx after changing this configuration and localhost/calendar will be served by Nginx and it will not be forwarded to tomcat.

I am no expert in Nginx but I get the job done. If there is a better way to do it please let me know in comments. Hope it helps.

Check out this link for more posts related to Nginx