2

I'm using nginx to create reverse proxy for two different applications, one hosted on the server where nginx is hosted at port 8011, and the other hosted on a different server with port number 8015. Using reverse proxy, the first application can be accessed on ROOT:80/ while the second application can be accessed at ROOT:80/blog.

Now, the static files of both the applications are inside their respective static folders which are located in two different paths (static path of first application hosted on the same server as nginx: PATH_ONE/static and static path of the second application: PATH_TWO/static). Now, the application hosted under ROOT:80/ picks up its static files correctly from the configuration used below. However, I'm not able to figure out how to go about configuring the same for the second application.-

How do I ensure that both applications hosted on different URLs using nginx use their respective static files?

nginx configuration:

upstream test_hosts {
   least_conn;
   server IP_ADDR_ONE:8011;
   server IP_ADDR_TWO:8011;
}

server{
    listen 80;

    location / {
        proxy_pass "http://test_hosts";
    }

    location /blog {s
        rewrite ^/blog(.*) /$1 break;
        proxy_pass "http://IP_ADDR_TWO:8015";
    }

    #browser caching of static assets
    location ~*  \.(jpg|jpeg|png|gif|ico|css|js|html)$ {
            expires 7d;
            root /PATH_ONE;
    }

} 
4
  • Give example of complete path1 and path2 for now, to suggest a solution Commented Jun 28, 2018 at 11:09
  • @TarunLalwani path1 = 192.xxx.xxx.121; path2 = 192.xxx.xxx.149 Commented Jun 28, 2018 at 11:14
  • why would path be an IP? I meant about root /PATH_ONE; Commented Jun 28, 2018 at 11:16
  • @TarunLalwani My bad. PATH_ONE would be the absolute path to the static folder on the respective servers, for example: /home/blog/server/. The static folder would be inside the respective paths of the two servers. Commented Jun 28, 2018 at 11:49

1 Answer 1

3

There are multiple ways to solve this. Depending how you are use case works. If you can blog on the /blog url itself then it can be the easiest thing to do

location /blog {
    proxy_pass http://IP_ADDR_TWO:8015;
}    

location /blog/static {
    alias <App2 static path>;
}

If you can't do that then you can do something like below where you have common root folder of both apps

#browser caching of static assets
location ~*  \.(jpg|jpeg|png|gif|ico|css|js|html)$ {
        expires 7d;
        root /home;
        try_files /blog/server/$uri /app/server/$uri = 404;
}

This will first check file in blog and if it exists then it will return the same if not will check on the other path /ROOT_ONE as per your question.

If both apps use same name for static files and with same paths then it could create error and serve wrong files.

In case you don't have static files on the same server from both apps then you can try something like below

#browser caching of static assets
location ~*  \.(jpg|jpeg|png|gif|ico|css|js|html)$ {
        expires 7d;
        root /home;
        try_files /app/server/$uri = @try_blogs;
}

location @try_blogs {
     proxy_pass http://IP_ADDR_TWO:8015$uri;
}
5
  • The <App2 static path> path should be on the same server where nginx is hosted, or could it be located on a different server where my second application runs from? Commented Jun 28, 2018 at 12:22
  • It has to be on same server else you need to try another alternative I am adding to the answer Commented Jun 28, 2018 at 12:27
  • What would $uri represent in your example? The absolute path of the static folder of my second application that is hosted on a separate server? Commented Jun 28, 2018 at 12:58
  • I have a form hosted on the /blog path, which sends a POST request to /login url on the second application's server. However, after the POST request is sent, I get a 404 error on the IP_ADDR_ONE/login path. Commented Jun 28, 2018 at 13:15
  • try_files /app/server/$uri = @try_blogs; I think this fallback more sense and it worked for me. Thank you. Commented Oct 5, 2018 at 9:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.