fbpx

NGINX / PHP

Add $_SERVER[‘REDIRECT_URL’] to nginx.

by ,


We have by far the largest RPM repository with NGINX module packages and VMODs for Varnish. If you want to install NGINX, Varnish, and lots of useful performance/security software with smooth yum upgrades for production use, this is the repository for you.
Active subscription is required.

What is $_SERVER[‘REDIRECT_URL’]

As per Apache 2.4 documentation:

REDIRECT_ environment variables are created from the environment variables which existed prior to the redirect. They are renamed with a REDIRECT_ prefix, i.e., HTTP_USER_AGENT becomes REDIRECT_HTTP_USER_AGENT.

You may find some information for the same in php.net documentation, but this gives you wrong idea about it (note that it comes from a comment by a user):

Data: $_SERVER[‘REDIRECT_URL’]
Data type: String
Purpose: The URL path name of the current PHP file, path-info is N/A and excluding URL query string. Includes leading slash.
Caveat: This is before URL rewrites (i.e. it’s as per the original call URL).
Caveat: Not set on all PHP environments, and definitely only ones with URL rewrites.

Which is all the way wrong.

First, REDIRECT_URL is not path to a PHP file. In general it is a path to a URL (URI, that is path starting with forward slash) of originally requested resource.
More importantly, it is a path that you get after all the internal rewrites/redirects (but the last one) take place.

In nginx + PHP-FPM setup, that would be equal to rewritten URL (value of $uri after all the try_files and rewrite directives).

Why is this important?

Some PHP scripts rely on this Apache-only server variable:
One notable software that makes use of it is PrestaShop, or its performance fork – ThirtyBees.

If you’re running those under nginx with PHP-FPM setup, chances are, you will hit this bug or similar.

Without further ado, here’s how to fix Prestashop (ThirtyBees) and emulate $_SERVER['REDIRECT_URL'] for them.

Add $_SERVER['REDIRECT_URL'] to nginx + PHP-FPM (works for any CMS)

Within your main location of nginx server definition, “save” the rewritten URL. Then in the PHP location set it as environmental variable REDIRECT_URL:

location / {
        # ....
        # you may have a bunch of rewrites above which rewrite URL, e.g.:
        #  rewrite ^/([0-9])(-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$1$2.jpg last;


        # At the very bottom of this location, put:
        # Now this is for emulating $_SERVER['REDIRECT_URL'] (Apache)!
        set $redirect_url $uri;
}

location ~ \.php$ {
    # ...
    # some existing stuff you have for defining PHP-FPM socket or port, etc.

    # We pass rewritten URI to PHP. It will be available as $_SERVER['REDIRECT_URL'] 
    fastcgi_param REDIRECT_URL $redirect_url;
}

In a nutshell, this allows you to emulate some of the Apache behaviours in nginx. As a result, Apache-centric software can run that easy with nginx as well.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This site uses Akismet to reduce spam. Learn how your comment data is processed.