Welcome! Log In Create A New Profile


Reverse proxy & rewrite rule issue

Posted by vukpm 
Reverse proxy & rewrite rule issue
June 07, 2010 10:12AM
Hi all!

I've set up nginx to be the front end web server, mostly forwarding all requests to back end apache, and serving some static content on its own.
There's however one more complex site that uses php scripting to serve all images, and also caching them in the process.

Idea is to (after some php modifying) have nginx check whether the image exist (has already passed php script, and was put in the cache folder) and if it does - serve it immediately, if not - pass it through the php script (reverse proxy to apache).

When the image exists, this works fine, however if the file doesn't exist nginx forwards the request to Apache, but it ignores .htaccess rules that forward the request through php - apache simply returns 404 page...

Apache rewrite rule is:
[code]RewriteRule ^cache/thumbnail/([^<]+)/([^<]+)/?$ thumbnail2.php?file=$2&size=$1&reverse=1 [L] [/code]

I've tried several similar configs, but the idea can be seen through this one.
server {
listen xx.xx.xx.xx:80;
server_name www.xyz.com;

#location ~ /\. {
# deny all;

location ~ ^/(cache/thumbnail)/ {

if (-f $request_filename) {

if (!-f $request_filename) {
#rewrite ^/cache/thumbnail/([^<]+)/([^<]+)/?$ thumbnail2.php?file=$2&size=$1&reverse=1 last;
proxy_pass http://xx.xx.xx.xy:80;


root /var/www/vhosts/site1/httpdocs/;

location / {
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;


Basically, I'm not sure that I've correctly rewrote the rewrite rule (commented line), or that the rest of this config is appropriate for what I'm trying to achieve. Needless to say, it doesn't work when the rewrite rule has been commented out.

Could someone point me in the right direction? Thanks!
Re: Reverse proxy & rewrite rule issue
June 08, 2010 09:33AM
Problem was in the Rewrite Base... It was / under apache, but nginx included /cache/thumbnail/ path, until I modified the rewrite code ([b] / [/b] before the thumbnail2.php). Also, permanent at the end helped.

if (!-f $request_filename) {
rewrite ^/cache/thumbnail/([^<]+)/([^<]+)$ /thumbnail2.php?file=$2&size=$1&reverse=1 permanent;
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 73
Record Number of Users: 6 on February 13, 2018
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready