Form url for existing Nginx rewrite rule

Viewed 79

i have question to Nginx specialist. There are such rules on Nginx.

If i request with header 'main', it adds /ismain to url:

if ($http_main) {
      rewrite ^(.*)$ /ismain/$1;
}

Next it cut /ismain/ from url and goes to Host

location /ismain/ {
      rewrite ^/ismain/(.*)$  $1 break;
      proxy_pass http://Host:9999;
}

It works good and i can't change it because of company sequrity policy.

But now i need to do callback and headers are not allowed.

So i request in such a way(without header): http://11.11.117.111:8077/ismain/someaddress

But Nginx cut off all slashes after port... and responses 400 Bad URI. In logs i can see such url after cut:

http://11.11.117.111:8077someaddress

I tried request with double slashes:
http://11.11.117.111:8077//ismain/someaddress
http://11.11.117.111:8077/ismain//someaddress

But it's not work. I have the same response. I'm in frustration why i works with header, but doesn't want with my formed path. I suppose it's the same.

Maybe i need to screen slash with some symbol ? Can you advice me ?

1 Answers

All Nginx URIs contain a leading /.

Your first rewrite statement is adding a double //. You should use:

rewrite ^(.*)$ /ismain$1;

or:

rewrite ^/(.*)$ /ismain/$1;

Your second rewrite statement relies on the first bug. You should use:

rewrite ^/ismain(/.*)$ $1 break;

or:

rewrite ^/ismain/(.*)$ /$1 break;
Related