4

I have an application that I can't configure a base url. Let's say that its url is 192.168.1.100:8011

I want to configure the nginx so I can entrer an url like 192.168.1.100/myappand it goes to the other app.

The configurations that I'm used to do only work when I have a base url. For example if I have an app on 192.168.1.100:8011/myapp and I wnat to use nginx for using 192.168.1.100/myapp, I have no problem but the other way I cant do it.

Is that possible ?

Drifter104
  • 3,773
  • 2
  • 25
  • 39
Jp Felgueiras
  • 41
  • 1
  • 1
  • 2
  • Are you good ? does the config below work for you ? – Scott Stensland Nov 22 '16 at 20:09
  • are both of these hosted by the same nginx server? Can you post the relevant sections of the nginx config and obscure any personal info so we can help see what the issue/solution may be? – SteamerJ Dec 18 '18 at 19:15

4 Answers4

1

What you probably need is:

location /myapp/ {
  proxy_pass http://192.168.1.100:8011/;
}

Notice the trailing slashes on both location and proxy_pass directives.

If the proxy_pass directive is specified with a URI, then when a request is passed to the server, the part of a normalized request URI matching the location is replaced by a URI specified in the directive

https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass

In this case /myapp/ is being replaced with /

Timson
  • 11
  • 1
  • This was definitely the best one in my case; the tiny detail there with the trailing / allowed me for the removal of all kinds of annoying regular expression magic I had before. Reading the manual actually helps?!? – CharlesS Feb 25 '22 at 10:56
1

You can use a rewrite rule:

location /myapp/ {
    rewrite /myapp(.*) /$1 break;
    proxy_pass http://192.168.1.100:8011
}

This will send URLS like /myapp/main/ to the backend as /main/.

allo
  • 1,620
  • 2
  • 22
  • 39
  • I think I didn't explain myself enough. What I want is to redirect the 192.168.1.100/myapp to 192.168.1.100:8011 – Jp Felgueiras Sep 22 '16 at 21:10
  • 1
    Redirect or reverse proxy? For a reverse proxy this is the solution to map urls ``/myapp/foo`` to ``/foo`` on the backend. For a redirect, just use a http redirect like described in the nginx docs. – allo Sep 23 '16 at 00:12
0

Is it okay to be taken away from /myapp (by the browser)?

That is simple:

location /myapp  { return 303 http://192.168.1.100:8011; }
location /myapp/ { return 303 http://192.168.1.100:8011; }

Do you want to keep deeper requests like /myapp/blah.txt?

location ~ ^/myapp(?<myslash>/|$)(?<mypath>.*)$
{
    set $myargs $args;  # workaround allowing spaces
    return 303 http://192.168.1.100:8011$myslash$mypath$is_args$myargs;
}

Untested.

uav
  • 534
  • 5
  • 20
0

Here is what I did to redirect http://example.com/app to http://192.168.1.24:8080/ (example.com/app being the WAN address and 192.168.1.24:8080 being the LAN address)

location /app/ {
    proxy_pass http://192.168.1.24:8080/;
    proxy_redirect http://192.168.1.24:8080/ http://example.com/app/;


    proxy_set_header Host $host;
    proxy_set_header Referer $http_referer;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header X-Forwarded-Proto $scheme;
}
uranibaba
  • 151
  • 1
  • 1
  • 6