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,421
  • 17
  • 14

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
  • 9,247
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
  • 306
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
  • 9,247
Tero Kilkanen
  • 38,887