0

In my current scenario there are a dozen domains with a PHP redirect script set up on them that obtains the destination URL from a SQL database based on the $_SERVER['QUERY_STRING'] parameter passed in the URL initially requested by the User.

NginX is configured like this on each of these servers (which we'll call Domains_A/B/C):

        location /r/ {
            rewrite ^/r/(.*)$ /r/index.php?$1;
        }

So the request goes like User > Domains_A/B/C > Destination URL

But what I would like is to not have to duplicate the PHP redirect script and keep this script on a single domain. This may be obtained by doing something like this:

Domains_A/B/C

        location /r/ {
            rewrite ^/r/(.*)$ https://Domain_X/r/index.php?$1;
        }

Domain_X (with PHP redirect script)

        location /r/ {
            rewrite ^/r/(.*)$ /r/index.php?$1;
        }

But this multiplies the redirects as User > Domain_A/B/C > Domain_X (with PHP redirect script) > Destination URL. And also the Referer sent to Destination URL is Domain_X which is something I am trying to avoid.

I would like to not multiply the redirects and to keep Domains_A/B/C as the Referer too. As in have NginX obtain the destination URL from Domain_X when Domains_A/B/C are accessed and point the redirect directly at the Destination URL from Domains_A/B/C.

Something along these lines on Domains_A/B/C:

        location /r/ {
            set Destination_URL get from https://Domain_X/r/index.php?$1;
            rewrite ^/r/(.*)$ Destination_URL;
        }

Is this possible? I was not able to find a solution yet.

5
  • What do you mean by "duplicate the PHP script"? You only need one copy of the script. You do not have to do any of this. Feb 2, 2021 at 12:14
  • @MichaelHampton of course I didn't have to do any of this, but all the same I did not know any other way, that was the motivation behind the question. See the accepted answer. Feb 2, 2021 at 17:41
  • You don't have to do that either. Just call the PHP script directly. Feb 2, 2021 at 17:44
  • @MichaelHampton The redirect needs to happen on some domains A/B/C (as in the user's browser needs to see only the domains A/B/C), but the redirect URL has to be obtained from the PHP script which resides on a different server on domain X (the domain X has to remain hidden from the user). If I call the script directly on domain X, the redirect happens on that server and that is not the desired outcome. Maybe I do not understand something fully, if that is the case, please give me some more information. Feb 2, 2021 at 17:52
  • OK, I missed the detail where you moved the PHP script to another server. I don't see that in your question. In that case you can't run it directly anyway. Feb 2, 2021 at 18:15

1 Answer 1

0

You can proxy_pass the request to the domain that has the redirect script:

On Domains_A/B/C:

location /r/(.*)$ {
    proxy_pass https://Domain_X/r/index.php?$1;
}

Or (in my installations it worked only like this)

location ~ ^/r/(.*)$ {
    proxy_set_header Host PHP_SCRIPT_DOMAIN_NAME;
    proxy_pass https://PHP_SCRIPT_DOMAIN_IP:443/r/index.php?$1;
}

And on Domain_X (which has the PHP script).

location /r/ {
    rewrite ^/r/(.*)$ /r/index.php?$1;
}
1
  • 1
    This worked with some variations, thank you very much! I sent the edits I made in case you want to include them in your answer, as I think they add some value. Feb 2, 2021 at 17:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .