12

I am trying to do something that seems to me very simple but I must be stupid or miss something very obvious because I cannot figure it out.

I want to access the multiple web applications running on my server with a unique prefix for each one of them:

http://mywebserver/app1 -> localhost:9001
http://mywebserver/app2 -> localhost:9002
...

From what I understood, I need to configure Nginx as a reverse proxy, so I did that:

server {
listen 80;
server_name mywebserver;

  location /app1{
    proxy_set_header    Host $host;
    proxy_set_header    X-Real-IP $remote_addr;
    proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header    X-Forwarded-Proto $scheme;
    proxy_pass          http://127.0.0.1:9001/;
  }
  ...
}

This configuration allows me to access the homepage of the application at http://mywebserver/app1, but the static content is not loaded.

When I open the console of my browser, I can see that it is trying to get the resources from http://mywebserver/static/custom/index.js and not something like http://mywebserver/app1/static/custom/index.js.

I tried to add a root directive in the location block to serve the static but it is not taken into account since the static content request is made on / and not on /app1.

How to fix this? Is there something obvious I am missing or I did I completely misunderstood the concept of reverse proxy?

Thank you.

aallrd
  • 191
  • 1
  • 1
  • 5
  • 3
    Try changing location /app1 to /app1/ (trailing slash). The current location will not match subdirectories ;) – Sebastian Neira Sep 28 '16 at 12:00
  • Omg, the /app1/ did the trick... Thanks a lot ! @RichardSmith I find it very weird to have to change the code of the application, it seems to completely defeat the purpose of the reverse proxy... – aallrd Sep 28 '16 at 13:41
  • Clearly we saw different questions ;-) – Richard Smith Sep 28 '16 at 14:36

1 Answers1

7

The suggestion from @sikian did the trick, I changed the location declaration from location /app1 { ... } to location /app1/ { ... }.

The static files were served without error in the console, and the application seems to be fully working.

aallrd
  • 191
  • 1
  • 1
  • 5
  • OMG. Shaking my head so hard. This resolved my issue of reverse proxy working but the static assets not being served up correctly. So much time I wasted! – SMT Nov 01 '17 at 18:06
  • I have the same issue and this solution not work for me. Can anyone share the complete solution? Thanks! :D – samsalfer May 19 '21 at 12:29