16

ok, this task should be simple but I just can't get it to work. I would like to have a subfolder after my domain name (actually after the IP of that domain name), which redirects to a specific port on the same server. Essentially, I want to get rid of having to use many ports.

Here is my nginx config for that

server {
    listen 80;

    index index.html index.htm index.nginx-debian.html index.php;

    server_name aaa.bbb.ccc.ddd;

    location ^~ /app2 {
        proxy_set_header X-Real-IP  $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host $host;
        proxy_pass http://aaa.bbb.ccc.ddd:8001;
    }
}

So upon accessing aaa.bbb.ccc.ddd/app2 I would like this to resolve to http://aaa.bbb.ccc.ddd:8001.

This can note possibly be so complicated. What am I missing here?

Thank you Pat

Tero Kilkanen
  • 34,499
  • 3
  • 38
  • 58
pAt84
  • 301
  • 1
  • 2
  • 6
  • Do you mean "redirects to a port" or "proxies to a port"? They're quite different. Your question is unclear, perhaps you could expand on your use case including client. – Tim Jul 27 '16 at 19:08

1 Answers1

20

Since you tagged this as a reverse proxy question, I assume you mean that you want to proxy the request so that user only sees http://aaa.bbb.ccc.ddd/app2 URL in her browser.

You can change your location block to this:

location ~/app2(.*)$ {
    proxy_set_header X-Real-IP  $remote_addr;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header Host $host;
    proxy_pass http://aaa.bbb.ccc.ddd:8001$1;
}

Here we capture the URI part after /app2 to $1 variable, and use it in the proxy_pass directive.

Tero Kilkanen
  • 34,499
  • 3
  • 38
  • 58
  • Thank you. That kinda did the trick. http://aaa.bbb.ccc.ddd:8001 is only displayed partially. There are websockets on that page and plenty of java script. I guess that is because I proxy the port and do not forward it as Tim mentioned above. Is this actually possible to achieve just using nginx? – pAt84 Jul 27 '16 at 21:05
  • What do the URLs of resources look like at the `aaa.bbb.ccc.ddd:8001` page? Most likely you need to fix the URLs to contain the `/app2` part. – Tero Kilkanen Jul 27 '16 at 21:09
  • It is really just a websocket there (showing some radar data, automotive...). There is no sign of "app2" in the source code. – pAt84 Jul 27 '16 at 21:23
  • Hm ok, yes. To call the websocket JS uses " var ws = new WebSocket('ws://' + location.host + '/ws');". location.host now misses the port number, which makes the whole thing not work anymore. – pAt84 Jul 27 '16 at 21:57
  • Ok, so it seems that once I call aaa.bbb.ccc.ddd/app2 I require it to internally use aaa.bbb.ccc.ddd:8001. Another option is to change all the java script. The reasoning is that I want to use nginx to give me a "one-port-solution" but after I have accessed (e.g. over http//aaa.bbb.ccc.ddd/app2) I would, internally, need it ti go back to aaa.bbb.ccc.ddd:8001. Is that possible? – pAt84 Jul 27 '16 at 22:52
  • 1
    There are two options. Either a `proxy_pass` solution, where user sees `http://aaa.bbb.ccc.ddd/app2` in the browser. In this case, the `location.host` contains `aaa.bbb.ccc.ddd`. You need to add `app2` there to make it work. Another alternative is `rewrite`, where user sees `http://aaa.bbb.ccc.ddd:8001` in the browser, and is what the browser will use. There is no middle-way. – Tero Kilkanen Jul 28 '16 at 08:39