Migrate Apache directives to Nginx

Nathan Zabaldo nate at usamm.com
Wed Sep 20 23:47:50 UTC 2017


Question on SF:
https://serverfault.com/questions/874730/convert-apache-mod-proxy-p-to-nginx-equivalent

My PHP MVC platform CodeIgniter performs routing based on the REQUEST_URI.

In Apache, you cannot change the REQUEST_URI environment variable. So, in
Apache, I made use of the [P]proxy flag. Sending the rewritten URL using
mod_proxy took care of that and it just works. The browser does not
redirect and the information is returned.

How do I do the same in Nginx? Or is there a way to change the REQUEST_URI
without proxy?

The *APACHE DIRECTIVES* take a URL like:

https://www.example.com/_img/h_32/w_36/test.jpg

and converts it to

https://www.example.com/img/i/_img/h_32/w_36/test.jpg.

Then once rewritten, it sends the request using mod_proxy and RewriteCond
makes sure to not call RewriteRule again if the URL has /img/i in it. Thus
avoiding the loop of death.

*APACHE DIRECTIVES*

    RewriteCond %{REQUEST_URI} (h|w|fm|trim|fit|pad|border|or|bg)_
    RewriteCond %{REQUEST_URI} !^(.*)/img/i/
    RewriteRule "(.*)\.(jpg|png)$" /index.php/img/i/$1.$2 [P]

*NGINX DIRECTIVES SO FAR*: You can see in *BLOCK 1* the regex to match and
a possible rewrite. Where do I go next?

    root /var/www/vhosts/example.com/htdocs;
    index index.php index.html;

    # *BLOCK 1*
    location ~* \/(h|w|fm|trim|fit|pad|border|or|bg)_.*\.(jpg|png)$ {
         #rewrite "(.*)\.(jpg|png)$" index.php/img/i$1.$2; #doesn't change
the REQUEST_URI
            #do I do proxy pass here?
            #How do I avoid the regex matching again?
    }

    location / {
        try_files $uri $uri/ /index.php$uri?$args;
    }

    location ~ ^(.+\.php)(.*)$ {
     fastcgi_pass   127.0.0.1:9000;
     fastcgi_param  CI_ENV production; #CI environment constant
     fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
     include fastcgi_params;
    }
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.nginx.org/pipermail/nginx/attachments/20170920/be8ed74a/attachment.html>


More information about the nginx mailing list