Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question
    
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 '11 at 3:05
    
Yes, my bad, im a bit sleepy, I edited the post. –  PingP0n6 Jun 5 '11 at 3:22
add comment

2 Answers

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.

share|improve this answer
    
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 '11 at 18:05
    
My answer is the same. Read the documentation I linked and you'll understand why your rewrite is never run. –  Martin Fjordvald Jun 5 '11 at 20:28
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.