2

I have a server hosting multiple .NET Core applications on different ports, with nginx reverse proxy setup to redirect users to the correct application via URL.

The current setup is as followed:

Upstream:

upstream app1 {
    server 127.0.0.1:44305;
}

upstream app2 {
    server 127.0.0.1:44306;
}

Location:

location /app1/ {
    rewrite ^/app1/?(.*)$ /$1 break;
    proxy_pass http://app1;
}

location /app2/ {
    rewrite ^/app2/?(.*)$ /$1 break;
    proxy_pass http://app2;
}

I can access the application via https://mywebsite.com/app1 and https://mywebsite.com/app2, however the 2 sites cannot load their respective static files (under the wwwroot folder). When I check the developer console, the error was that the static file being load is https://mywebsite.com/static/image.png instead of https://mywebsite.com/app1/static/image.png, which was the cause of the 404 error when loading the static files.

I have tried adding a location block to specify the static folder:

location ~* /app1/.(css|js|lib|ico)$ {
    alias /var/www/app1/wwwroot/;
}

But it still doesn't work.


Update from this answer, I added this block:

location /static/ {
    proxy_pass http://app1/static/;
}

Now that the static file is redirected from https://mywebsite.com/static to https://mywebsite.com/app1/static/. However, both apps are redirecting the app1 static folder. What can I do to distinguish between the static requests from each application?

Jeff Do
  • 21
  • 2
  • 2
    Does this answer your question? [How to handle relative urls correctly with a nginx reverse proxy](https://serverfault.com/questions/932628/how-to-handle-relative-urls-correctly-with-a-nginx-reverse-proxy) - There are several different approaches possible and I think I covered most of them in that answer. – HBruijn Jul 18 '23 at 07:38
  • @HBruijn it actually helped to give me some idea, but I ran into another issue instead. Updated in the original post. – Jeff Do Jul 18 '23 at 08:10
  • When the `/static` overlaps , rewrite the HTML/CSS code your back-end apps generate. – HBruijn Jul 18 '23 at 08:15
  • Is there no way to differentiate using nginx? I don't understand what you meant by rewrite the HTML/CSS code. – Jeff Do Jul 18 '23 at 08:22
  • Please don't rewrite the original question but create another. – AlexD Jul 18 '23 at 08:55
  • With rewrite html/css was referring to the last option in https://serverfault.com/questions/932628/ : use the nginx http sub module to rewrite any links to resources with the correct path i.e. rewrite in nginx all references from `/static` to `/app1/static` in the /app1 location block respectively to `/app2/static` in the app2 location block. – HBruijn Jul 18 '23 at 09:21
  • @HBruijn thank you, I understand what you meant now. It looks like it would work. – Jeff Do Jul 18 '23 at 10:06

0 Answers0