I'm trying to rewrite an url to another one, but i can't use the $1 correctly:

location ~ ^/announce {
    rewrite ^(.*)$1 http://exemple.com/$1/announce.php?ip=$remote_addr&$args;

In fact http://jack.exemple.com:2052/anything/announce should become https://exemple.com/anything/announce.php?ip=$remote_addr&

That's work without the $1, but not with args beetwin http://jack.exemple.com:2052/ and /announce

I think it's something pretty simple, but not so common, and I can't find the good syntax.

  • What does "but not with args between http://jack.exemple.com:2052/ and /announce mean exactly? Please show a clear example what is the source URL, and what exactly you want to do with it. – Tero Kilkanen Jul 25 '17 at 16:26

I resolved my problem with this syntax:

rewrite ^(.*)$ $1?ip=$remote_addr&$args break;

I just add ?ip=$remote_addr&$args at the end of my URL. For information, this is for transfer real ip from host to client, passing by cloudflare.

| improve this answer | |
  • You should use nginx set_real_ip directive to make nginx use the IP in the header Cloudflare adds to the request. This way you can get the correct client IP everywhere in your setup. – Tero Kilkanen Aug 9 '17 at 19:47
  • Yes I did it! But the thing is, this directive works to get the correct client IP on the server, but not to send this same IP from server to client! So we need to rewrite the IP to send it to client – poka Aug 11 '17 at 9:00

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