The generate .htaccess doesn't work on nginx server. Is there anyone able to convert the below code to nginx rewrite?

      # ----------------------------------------------------------------------
      # Responsive Images
      # ----------------------------------------------------------------------
      RewriteCond %{HTTP_COOKIE} rwdimgsize=large
      RewriteRule (.*)\/mobile\/(.*)\.r(\.(jpe?g|png|gif))$ $1/desktop/$2$3

      # remove .r from all requests
      RewriteRule (.*)\.r(\.(jpe?g|png|gif))$ $1$2
      # ----------------------------------------------------------------------
      # END Responsive Images
      # ----------------------------------------------------------------------

The small image is "mobile" and large image is "desktop"

Comments

svendecabooter’s picture

You might want to check out https://github.com/filamentgroup/Responsive-Images/pull/12
Not sure if that'll be of any help though...

spacereactor’s picture

Thank, but still couldn't to work. My rewrite fail to generate with .r and definitely I mess up the nginx when I add the cookies in.

omega8cc’s picture

Maybe try this:

location ~* (?:mobile/)?(?:.*)\.r\.(?:jpe?g|png|gif) {
  if ( $http_cookie ~* "rwdimgsize=large" ) {
    rewrite ^/(.*)/mobile/(.*)\.r(\.(?:jpe?g|png|gif))$ /$1/desktop/$2$3 last;
  }
  rewrite ^/(.*)\.r(\.(?:jpe?g|png|gif))$ /$1$2 last;
  access_log off;
  try_files $uri @cache;
}

I didn't test it yet, but I think it should work.

FYI: for BOA users - it should got to the nginx_vhost_include.conf file, as explained in http://drupalcode.org/project/octopus.git/blob/HEAD:/docs/HINTS.txt#l16

spacereactor’s picture

I using #3 but not lucky, and this time there no error in drupal log but it can't generate image with insert ".r"

If anyone can get nginx to work, let me know. Thank

mattfielding’s picture

This code definitely works for me

location ~* \.r\.(jpe?g|png|gif) {
  if ( $http_cookie ~* "rwdimgsize=large" ) {
    rewrite ^/(.*)/mobile/(.*)\.r(\.(?:jpe?g|png|gif))$ /$1/desktop/$2$3 last;
  }
  rewrite ^/(.*)\.r(\.(?:jpe?g|png|gif))$ /$1$2 last;
  access_log off;
  try_files $uri @cache;
}

There is a problem getting it to with BOA. The location statement seems to stop it working.

It is because nginx_vhost_include.conf is called into nginx_advanced_include.conf below the following location statement

location ~* /(?:external|system|files/imagecache|files/styles)/ {

Which is used for imagecache/images styles

If the include for nginx_vhost_include.conf happens a little earlier then it works fine

omega8cc’s picture

@MattFielding

Thanks for testing!

Added in commit: http://drupalcode.org/project/octopus.git/commit/400bf09

spacereactor’s picture

i using https://github.com/perusio/drupal-with-nginx setting. where do i add in the code? There no display of image and error like "Unable to generate the derived image located at public://styles/mobile/public/photofile.r.JPG." in my report error log

Below is my nginx domainname conf

server {
server_name example.com;

access_log  /var/log/nginx/example.com_access.log;
error_log   /var/log/nginx/example.com_error.log;

root /var/www/sites/example.com;
index index.php;


location / {

    ## Drupal 404 from can impact performance. If using a module like
    ## search404 then 404's *have *to be handled by Drupal. Uncomment to
    ## relay the handling of 404's to Drupal.
    ## error_page 404 /index.php;

    ## Use index.html whenever there's no index.php.
    location = / {
        error_page 404 =200 /index.html;
    }

    ## Using a nested location is the 'correct' way to use regexes.

    ## Regular private file serving (i.e. handled by Drupal).
    location ^~ /system/files/ {
        ## Include the specific FastCGI configuration. This is for a
        ## FCGI backend like php-cgi or php-fpm.
        include fastcgi_private_files.conf;
        fastcgi_pass phpcgi;

        ## If proxying to apache comment the two lines above and
        ## uncomment the line below.
        #proxy_pass http://phpapache;

        ## For not signaling a 404 in the error log whenever the
        ## system/files directory is accessed add the line below.
        ## Note that the 404 is the intended behavior.
        log_not_found off;
    }

    ## If accessing an image generated by imagecache, serve it directly if
    ## available, if not relay the request to Drupal to (re)generate the
    ## image.
    location ~* /imagecache/ {
        access_log off;
        expires 30d;
        try_files $uri /index.php?q=$uri&$args;
    }

    ## Drupal 7 generated image handling, i.e., imagecache in core. See:
    ## https://drupal.org/node/371374.
    location ~* /files/styles/ {
        access_log off;
        expires 30d;
        try_files $uri /index.php?q=$uri&$args;
    }

    ## Advanced Aggregation module CSS
    ## support. http://drupal.org/project/advagg.
    location ^~ /sites/default/files/advagg_css/ {
        location ~* /sites/default/files/advagg_css/.*\.css$ {
            access_log off;
            add_header Pragma '';
            add_header Cache-Control 'public, max-age=946080000';
            add_header Accept-Ranges '';
            try_files $uri /index.php?q=$uri&$args;
        }
    }

    ## Advanced Aggregation module JS
    ## support. http://drupal.org/project/advagg.
    location ^~ /sites/default/files/advagg_js/ {
        location ~* /sites/default/files/advagg_css/.*\.js$ {
            access_log off;
            add_header Pragma '';
            add_header Cache-Control 'public, max-age=946080000';
            add_header Accept-Ranges '';
            try_files $uri /index.php?q=$uri&$args;
        }
    }

    ## All static files will be served directly.
    location ~* ^.+\.(?:css|js|jpg|jpeg|gif|ico|png|html|xml)$ {
        access_log        off;
        expires           30d;
        ## No need to bleed constant updates. Send the all shebang in one
        ## fell swoop.
        tcp_nodelay off;
    }

    ## Trying to access private files directly returns a 404.
    location ^~ /sites/default/files/private/ {
        internal;
    }

    ## Keep a tab on the 'big' static files.
    location ~* ^.+\.(?:m4a|mp3|mp4|mov|ogg|flv|pdf|ppt[x]*)$ {
        expires 30d;
        ## No need to bleed constant updates. Send the all shebang in one
        ## fell swoop.
        tcp_nodelay off;
    }

    ## Advanced Help module makes each module provided README available.
    location ^~ /help/ {
        location ~* ^/help/[^/]*/README\.txt$ {
            ## Include the specific FastCGI configuration. This is for a
            ## FCGI backend like php-cgi or php-fpm.
            include fastcgi_private_files.conf;
            fastcgi_pass phpcgi;

            ## If proxying to apache comment the two lines above and
            ## uncomment the line below.
            #proxy_pass http://phpapache;
        }
    }

    ## Replicate the Apache <FilesMatch> directive of Drupal standard
    ## .htaccess. Disable access to any code files. Return a 404 to curtail
    ## information disclosure. Hide also the text files.
    location ~* ^(?:.+\.(?:htaccess|make|txt|engine|inc|info|install|module|profile|po|sh|.*sql|theme|tpl(?:\.php)?|xtmpl)|code-style\.pl|/Entries.*|/Repository|/Root|/Tag|/Template)$ {
        return 404;
    }

    ## First we try the URI and relay to the @cache if not found.
    try_files $uri @cache;
}

## We define a named location for the cache.
location @cache {

    ## Error page handler for the case where $no_cache is 1. POST
    ## request or authenticated.
    error_page 418 = @no_cache;

    ## If $no_cache is 1 then it means that either we have a session
    ## cookie or that the request method is POST. So serve the dynamic
    ## page.
    if ($no_cache) {
        return 418; # I'm a teapot/I can't get no cachifaction
    }

    # Now for some header tweaking. We use a date that differs
    # from stock Drupal. Everyone seems to be using their
    # birthdate. Why go against the grain?
    add_header Expires "Tue, 13 Jun 1977 03:45:00 GMT";
    # We bypass all delays in the post-check and pre-check
    # parameters of Cache-Control. Both set to 0.
    add_header Cache-Control "must-revalidate, post-check=0, pre-check=0";
    # Funny...perhaps. Egocentric? Damn right!;
    add_header X-Header "Boost Helás Avril 1.0";

    # We try each boost URI in succession, if every one of them
    # fails then relay to Drupal.
    try_files /cache/normal/$host${uri}_${args}.html /cache/perm/$host${uri}_.css /cache/perm/$host${uri}_.js /cache/$host/0$uri.html /cache/$host/0${uri}/index.html /index.php?q=$uri&$args;
}

## We need another named location for the rewrite to work otherwise we
## get the unclean URLs in all their glory.
location @no_cache {
    try_files $uri /index.php?q=$uri&$args;
}

########### Security measures ##########

## Restrict access to the strictly necessary PHP files. Reducing the
## scope for exploits. Handling of PHP code and the Drupal event loop.
location = /index.php {
    fastcgi_pass phpcgi;
    ## To use Apache for serving PHP uncomment the line bellow and
    ## comment out the above.
    #proxy_pass http://phpapache;
    ## Filefield Upload progress
    ## http://drupal.org/project/filefield_nginx_progress support
    ## through the NgninxUploadProgress modules.
    track_uploads uploads 60s;
}

## Boost stats.
location = /boost_stats.php {
    fastcgi_pass phpcgi;
    ## To use Apache for serving PHP uncomment the line bellow and
    ## comment out the above.
    #proxy_pass http://phpapache;
}


## Disallow access to .git directory: return 404 as not to disclose
## information.
location = /.git {
    return 404;
}

## Disallow access to patches directory.
location = /patches {
    return 404;
}

## Disallow access to drush backup directory.
location = /backup {
    return 404;
}

## Disable access logs for robots.txt.
location = /robots.txt {
    access_log off;
}

## RSS feed support.
location = /rss.xml {
    try_files $uri /index.php?q=$uri;
}

## XML Sitemap support.
location = /sitemap.xml {
    try_files $uri /index.php?q=$uri;
}

## Support for favicon. Return a 204 (No Content) if the favicon
## doesn't exist.
location = /favicon.ico {
    try_files /favicon.ico =204;
}

## Any other attempt to access PHP files returns a 404.
location ~* ^.+\.php$ {
    return 404;
}

location = /xmlrpc.php {
    fastcgi_pass phpcgi;
}

location = /cron.php {
    allow 127.0.0.1;
    allow 192.168.1.0/24;
    fastcgi_pass phpcgi;
    error_page 403 =404;
    deny all;
}

location = /authorize.php {
    fastcgi_pass phpcgi;
}

location = /update.php {
    fastcgi_pass phpcgi;
}

location ~ (.*)/x-progress-id:(\w*) {
rewrite ^(.*)/x-progress-id:(\w*)  $1?X-Progress-ID=$2;
}

location ^~ /progress {
report_uploads uploads;
}

}
omega8cc’s picture

@spacereactor You should put it just before/above location ~* /imagecache/ {.