1

This question may be asked many times, I have been reading but still can't get what I want.

I have an application running in http://172.17.1.10:8080/myapp I would like to setup nginx reverse proxy accessing the application using http://sub.domain.com only , hidding "myapp" in the address bar.

My current setting is:

server{
        listen 80;
        server_name sub.domain.com;
        location /myapp {
                rewrite ^/myapp(.*) /$1 break;
                proxy_pass http://172.17.1.10:8080;
                proxy_redirect off;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP      $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                include /etc/nginx/proxy_params;
        }

}

But I keep viewing the Nginx index page with http://sub.domain.com, and http://sub.domain.com/myapp the tomcat index page. What am I missing?

masegaloeh
  • 18,236
  • 10
  • 57
  • 106
Yichaoz
  • 381
  • 4
  • 8
  • 20

2 Answers2

1

Change these three lines should fix you:

location /myapp {
            rewrite ^/myapp(.*) /$1 break;
            proxy_pass http://172.17.1.10:8080;

To:

location / {
            proxy_pass http://172.17.1.10:8080/myapp;
xeon
  • 3,806
  • 18
  • 18
  • I first tried that, but it just redirect to sub.domain.com/myapp, and "myapp" will still be part of url – Yichaoz Dec 07 '14 at 04:36
  • You need to follow the redirects and find out why, my suggestion does not included redirects. Did you remove the rewrite? – xeon Dec 07 '14 at 04:38
  • I did exactly what you mentiones, removed the locationand the rewrite line. now when I type sub.domain.com it automatically redirect to sub.domain.com/myapp (and also with error "myappmyapp not found" ) – Yichaoz Dec 07 '14 at 04:46
  • You could try adding a forward slash after /myapp in proxy_pass. But it seems to me like your myapp or something else is redirecting requests. – xeon Dec 07 '14 at 08:00
1

When you change the content of the URI being processed within a location block containing a proxy_pass directive then you need to handle Location header rewrites with proxy_redirect :

In some cases, the part of a request URI to be replaced cannot be determined:

  • When location is specified using a regular expression. In this case, the directive should be specified without a URI.

  • When the URI is changed inside a proxied location using the rewrite directive, and this same configuration will be used to process a request (break).

But in your case there's no reason to use a rewrite because nginx already handles this in the proxy_pass directive while adding an URI prefix:

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.

Also you need to remove the proxy_redirect directive so nginx can rewrite Location headers using the proxy_pass URI prefix as a pattern and the location prefix as a replacement.

So simply use this :

server {

    server_name sub.domain.com;

    location / {
            proxy_pass http://172.17.1.10:8080/myapp/;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            include /etc/nginx/proxy_params;
    }

}
Xavier Lucas
  • 13,095
  • 2
  • 44
  • 50
  • I get infinite redirect loop... I think something else is redirecting it... – Yichaoz Dec 07 '14 at 19:17
  • @Kossel Check you backend replies, it doens't come from this nginx configuration (except if your include directive imports something inappropriate). – Xavier Lucas Dec 07 '14 at 19:21
  • by default the app is checked by spring Security and will redirect to /login or /home I think the further problem is not nginx related... thanks – Yichaoz Dec 07 '14 at 19:47