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

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

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.