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 ?

2
  • Are you good ? does the config below work for you ? Nov 22, 2016 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, 2018 at 19:15

4 Answers 4

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/.

2
  • 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 Sep 22, 2016 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, 2016 at 0:12
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 /

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 at 10:56
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;
}
2
  • you may want to use the default server and a location block then you wont need a url as it will catch any names to thw default server
    – djdomi
    Apr 19, 2020 at 21:14
  • @djdomi What would that look like?
    – uranibaba
    Apr 22, 2020 at 9:07
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.