1

I recently installed NGINX as a reverse proxy for my apache server, to handle static files. So far so good, everything working fine, except for the fact that I cant watermark my images anymore.

My .htaccess on Apache for this was:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteRule ^(.*)wp-content/upload/(.*\.(jpg|JPG|jpeg|png))$ $1watermark.php?src=wp-content/upload/$2
</IfModule>

This is my current vhosts nginx.conf (wich isnt working for the watermark):

server {
  listen      80;
  server_name mydomain.com www.mydomain.com ;
  error_log /var/www/vhosts/mydomain.com/statistics/logs/error_log.nginx warn;

  location / {
    proxy_pass  http://www.mydomain.com:8080$request_uri;
    include  /etc/nginx/proxy.conf;
  }

  location ~* ^.+\.(jpg|jpeg|gif|png|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|wav|bmp|rtf|js|ico|swf)$ {
    root  /var/www/vhosts/mydomain.com/httpdocs;
    expires 7d;
  }
  #This here, isnt working#
  location /wp-content/upload/ {
  rewrite ^/(.*)wp-content/upload/(.*\.(jpg|JPG|jpeg|png))$ /$1watermark.php?src=wp-content/upload/$2;
  }
 }

But doesnt work at all. I read rewrite nginx documentation, I have been trying for several days and still, I cant find the way to make it work.

2
  • The two rewrites matches different things but the main difference that strikes me is upload vs uploads in the nginx rewrite. Typo?
    – spock
    Jun 5, 2011 at 3:05
  • Yes, my bad, im a bit sleepy, I edited the post.
    – PingP0n6
    Jun 5, 2011 at 3:22

2 Answers 2

1

location ~ \.(jpg|jpeg|png|gif|swf|flv|pdf)$ Does something like this look familiar? It's a pretty standard location for catching requests for static files. Most likely you have it as well, but I obviously can't tell since you've decided to only show of 1 line of your Nginx configuration.

Now, since we have read the documentation about one of the most basic nginx blocks: the location, we know that some locations takes precedence over others, in fact, we know that nginx logic says that since you can make regex location really specific it'll assume that matching regex location should be used first.

We also know that location / is the least specific we can get as a left-prefix match for / matches every possible URI.

Therefore, if another matching location exists then location / won't be used. Considering the static request from the first part of my reply and your information that you put the rewrite into location / we probably now know why!

If it still doesn't work then please provide your configuration so people have some actual data to work with.

2
  • Thanks for your answer, I updated the post with current configuration. I actually tried several combinations, but none of them worked for me. If you can give me any other tip, ill appreciate it. Thanks.
    – PingP0n6
    Jun 5, 2011 at 18:05
  • My answer is the same. Read the documentation I linked and you'll understand why your rewrite is never run. Jun 5, 2011 at 20:28
0

Now the expressions look the same except for the initial slashes in the nginx rewrite. Since you've been staring at this for days I'll assume you have already considered that part.

Make sure the location block with that rewrite actually matches the image requests (and isn't overridden by some other location block). When you're confident this is the case, add the appropriate flag to your nginx rewrite line. I'd guess break is appropriate in this case.

You must log in to answer this question.

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