简体   繁体   中英

Redirect ALL get parameters to another domain in Nginx

Only answers I can find for this have specific parameters and don't work if the parameters change.

I have a URL coming in such as:

/logs.php?user_id=10032&account_id=10099&message=0&interval=1&online=1&rsid=65374826&action=update&ids=827,9210,82930&session_id=1211546313-1602275138

I need to redirect this url to a completely different domain and file but keep the get params.

So it ends up redirecting to:

https://example.com/the_logger.php?REPEAT_ALL_GET_PARAMETERS_HERE


Here is what I have unsuccessfully tried so far:

location logs.php
{
    rewrite https://example.com/the_logger.php?$args last;
}

But it doesn't seem to match the url or redirect. I think I'm misunderstanding the logic of nginx confs. If it were .htaccess I think I'd be okay. I can put a few more examples here if need be of what I'm trying to achieve.

As you state this is a redirect and not reverse proxying a request, I would use the return directive to tell the client to do a 301 or 302 . Using the return directive is the simpler and more recommend approach to redirecting a client.

Something like should do what you want:

location /logs {
    return 302 https://example.com/the_logger.php$is_args$args;
}

Where $is_args would output a ? if and only if the query string is not empty, and $args is the query string itself

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM