0

I am trying to combine these 2 Nginx location definitions into 1

 location /v1/login {
     proxy_pass      http://upstream/v1/login;
     proxy_redirect     off;

     proxy_set_header   Host             $host;
     proxy_set_header   X-Real-IP        $remote_addr;
     proxy_pass_header Authorization;
     }

 location /v1/logout {
     proxy_pass      http://upstream/v1/logout;
     proxy_redirect     off;

     proxy_set_header   Host             $host;
     proxy_set_header   X-Real-IP        $remote_addr;
     proxy_pass_header Authorization;
     }

So I figured something like this should do the job

    location ^~ /v1/(login|logout) {
            rewrite ^/v1/(.*)$ /v1/$1 break;
            proxy_pass      http://upstream;
            proxy_redirect     off;

            proxy_set_header   Host             $host;
            proxy_set_header   X-Real-IP        $remote_addr;

    }

But for the life of me I can't get it to work. What am I doing wrong here? I have tried every possible combination of rewrite regexes.

solefald
  • 2,303
  • 15
  • 14
  • Why you want to combine them? Shiw full configuration – Alexey Ten Sep 12 '15 at 18:42
  • @AlexeyTen: Because I am trying to clean up and consolidate the config and consolidating these two definitions into one seems like a thing that should be easy. Whoever put the current config together thought that adding 150+ separate `location` definitions in no particular order was the way to go, so the config has become unmanageable. I've consolidated everything else and it is working, except this particular part. – solefald Sep 12 '15 at 21:37
  • What king of requests do you need to handle? Is `/v1/login` (or logout) enough, or do you need all paths starting with it, like `/v1/login/something.html`? Also, `^~` symbols do not work for regex matching, use just `~` for case-sensitive match. – Mikko Sep 13 '15 at 13:18

3 Answers3

1

What's wrong with this simple one?

location /v1/ {
    proxy_pass      http://upstream/v1/;

    proxy_set_header   Host             $host;
    proxy_set_header   X-Real-IP        $remote_addr;
}
Alexey Ten
  • 7,922
  • 31
  • 35
  • There are other `/v1/blah` locations that go to different places, so I can't just send them all there, unfortunately. – solefald Sep 12 '15 at 16:35
1

Have you tried this one?

location ~ ^/v1/(login|logout) {
    proxy_pass http://upstream/v1/$1;
    proxy_redirect off;

    proxy_set_header Host        $host;
    proxy_set_header X-Real-IP   $remote_addr;
}

Edit:

You may also add the following directive along with your proxy_pass_header Authorization; directive:

proxy_set_header Authorization $http_authorization;
Oleg
  • 266
  • 3
  • 17
  • Yep. Does not work either. I can't understand why. Spent way too much time on this already. – solefald Sep 12 '15 at 17:44
  • what is the result of this? does Nginx show any errors when you reload it? what do you see when you navigate to `/v1/login` or `/v1/logout` ? – Oleg Sep 12 '15 at 19:09
  • It hits the js script which throws logs a console error `if( config.url.indexOf("/v1/") == 0 && responseError.status == 401){`. I get a 401 in nginx too, because this is the error the `upstream` java app throws. Unfortunately there are no useful logs from the java app. – solefald Sep 12 '15 at 21:32
  • @solefald You can try to access Java app directly, by navigating to `http://yourdomain.com:3000/v1/login` where `3000` is a port number which is listened by Java app, to make sure that the problem is on Nginx side. Alse you may try [this](http://serverfault.com/questions/511206/nginx-forward-http-auth-user) solution – Oleg Sep 13 '15 at 11:50
  • Aaaand your edit solved the problem! Thank you so much! I was losing my mind here over something that simple. – solefald Sep 13 '15 at 18:14
  • @solefald happy to help. good luck! :) – Oleg Sep 13 '15 at 18:54
0

You are not passing the complete URL to proxy_pass. Try something like this:

location ~ ^/v1/(login|logout) {
    proxy_pass http://upstream;
    proxy_redirect off;

    proxy_set_header Host        $host;
    proxy_set_header X-Real-IP   $remote_addr;
}
Alexey Ten
  • 7,922
  • 31
  • 35
Tero Kilkanen
  • 34,499
  • 3
  • 38
  • 58