Using nginx + php-fpm so it doesn't use htaccess. Is there any doc on how to get it work in nginx environment. And i also can't get the FAST 404 to work,
already copy the follow code at the bottom of my settings.php but no lucky, doesn't work. Adv CSS/JS Agg - Asynchronous Mode Set to FALSE.

// List of extensions for static files
$exts = 'txt|png|gif|jpe?g|shtml?|css|js|ico|swf|flv|cgi|bat|pl|dll|exe|asp|xml';

// It is not an imagecache path, which we allow to go through Drupal
if (!strpos($_SERVER['QUERY_STRING'], 'imagecache') && !strpos($_SERVER['QUERY_STRING'], '/advagg_')) {
// It is not our main feed page
if ($_SERVER['QUERY_STRING'] != 'rss.xml') {
// Is it a static file?
if (preg_match('/\.(' . $exts . ')$/', $_SERVER['QUERY_STRING']))
// Just send a 404 right now ...
{
header('HTTP/1.0 404 Not Found');
print '';
print '404 Not Found';
print '
Not Found

';
print '

The requested URL was not found on this server.

';
print '';
exit();
}
}
}

Comments

mikeytown2’s picture

http://wiki.nginx.org/HttpGzipStaticModule should do the gzip stuff. The other rules has to do with the expiration time and changing it from the default 2 weeks to 1 year.

I use this URL as a the test for fast 404s example.com/sites/default/files/advagg_css/css_missing_0.css

I then check for the X-AdvAgg header. If that is set in the request then I know missing files are correctly being routed to the advagg handler. My guess is you have a rule for the files directory in your nginx config. What you need to do is look for the imagecache rules and adapt them for the advagg_css and advagg_js directories.

spacereactor’s picture

StatusFileSize
new3.06 KB

i got the async mode working. I need to manually add "sites/default/files" since i using private setting at admin/settings/advagg/config

I attach my nginx.conf and domain.com in a zip file if anyone need it.
Using most of my nginx conf setting from perusio. THANK you perusio!!
https://github.com/perusio/drupal-with-nginx

spacereactor’s picture

Status: Active » Closed (works as designed)
spacereactor’s picture

Status: Closed (works as designed) » Active

how you purge the cache css and js? i try to flash all cache but all the files in advagg_css and advagg_js remain, i need to manually delete the files in advagg_css and advagg_js. Is that how it work or my nginx setting wrong

mikeytown2’s picture

I use the similar logic in this module for what is used in core in D7. Aggregates do not get flushed till they haven't been used in 30 days drupal_delete_file_if_stale(). Let me know if you have any more questions :)

spacereactor’s picture

Status: Active » Fixed

thank for your info.

omega8cc’s picture

Status: Fixed » Needs review

My working configuration supporting advagg in Nginx:

https://github.com/omega8cc/nginx-for-drupal/blob/master/aegir/conf/ngin...

mikeytown2’s picture

After testing on a lot of browsers I brought back etags. I don't think nginx has full etag support out of the box, so I would turn back on Last-Modified; they accomplish the same goal.
http://drupalcode.org/project/advagg.git/commitdiff/c132a7e45375def56d41...

    ###
    ### advagg_css and advagg_js support
    ###
    location ~* /files/advagg_(?:css|js)/ {
        access_log off;
        expires 365d;
        rewrite ^/files/advagg_(.*)/(.*)$ /sites/$host/files/advagg_$1/$2 last;
        add_header Pragma "";
        add_header Cache-Control "public";
        add_header X-Header "AdvAgg Generator 1.0-boa-T-5";
        try_files $uri @drupal;
    }
spacereactor’s picture

I have set private file to be "sites/default/private" and advagg to "sites/default/public" for publlic access css and js

location ~* advagg_(?:css|js)/ {
access_log off;
expires 365d;
rewrite ^advagg_(.*)/(.*)$ /sites/$host/public/advagg_$1/$2 last;
add_header Pragma "";
add_header Cache-Control "public";
add_header X-Header "AdvAgg Generator 1.0-boa-T-5";
try_files $uri @drupal;
}

But it keep create file at /sites/default/public/default/advagg_css and js

rewrite ^advagg_(.*)/(.*)$ /sites/$host/public/advagg_$1/$2 last;

I can't get aboves to work, what do i mess up again....

omega8cc’s picture

Right, there is an experimental module for Nginx: http://mikewest.org/2008/11/generating-etags-for-static-content-using-nginx but we should stay with Last-Modified probably.

Thanks for the update!

omega8cc’s picture

@spacereactor you could try this, as you don't need any rewrite, IMO:

    ###
    ### advagg_css and advagg_js support
    ###
    location ~* advagg_(?:css|js)/ {
        access_log off;
        expires 365d;
        add_header Pragma "";
        add_header Cache-Control "public";
        try_files $uri @drupal;
    }
spacereactor’s picture

#9 solution for /sites/default/public/default/advagg_css and js is by setting the path to "sites" for your advadd folder location.

Goto admin/settings/advagg/config -> Use a different directory for storing advagg files: -> sites
you may need to create advagg_css & advagg_js folder, but inputing sites as your path, it will now link to your /sites/default/advagg_css and js folder or if you using a subdomain name or other domainname, it will automatic link to your /sites/subdomain/advagg_css and js folder or /sites/other-domainname/advagg_css and js folder.

Hope this help someone.

crea’s picture

I remember reading about Etags in Nginx and the program author said that it's not supported in Nginx because it essentially duplicates last-modified.

Peter Bowey’s picture

Etag can be useful for static and dynamic content, typically - when the modify time is not known. Etag is more granular than 'Last-Modified'.

'Last-Modified' is typically accurate to the second, and only measures change along the axis of time. Touching [date/time] a file does not change it’s content, but would force a cache miss when the cache is based on nothing but it's time-stamp ['Last-Modified'].

In contrast, Etags are 'content-based' identifiers that provide a mechanism for confirmation that the content of the file you’re reading is accurate, regardless of various delivery deployment methods on the server.

Some thoughts: It is not a bad practice to rely entirely on 'Last-Modified' when serving truly static content. In this case, the correct use of 'Cache-Control' and 'Expires' headers, will allow client browsers and upstream proxies to cache the static content correctly.

see http://mikewest.org/2008/11/generating-etags-for-static-content-using-nginx

Yahoo!’s Exceptional Performance team explicitly recommends against using the Etag header unless you know what you’re doing (which really boils down to ensuring that Apache doesn’t use the file’s inode to generate the key). I think they've painted with a really broad brush, though, as YSlow’s admonitions against Etags really only apply to the default configurations of Apache and IIS. Etag can be a very valuable addition to the caching arsenal if you think about them; Yahoo!’s throwing the baby out with the bathwater.

HTTP 1.1 introduces a new kind of validator called the ETag. ETags are unique identifiers that are generated by the server and changed every time the representation does. Because the server controls how the ETag is generated, caches can be posotive that if the ETag matches when they make a If-None-Match request, the representation really is the same.

Almost all caches use Last-Modified times in determining if an representation is fresh; However, ETag validation is also becoming more prevalent.

Most modern Web servers will generate both ETag and Last-Modified headers to use as validators for static content (files) automatically; you won’t have to do anything. However, they don’t know enough about dynamic content (like CGI, ASP or database sites) to generate them; see Writing Cache-Aware Scripts -> http://www.mnot.net/cache_docs/#SCRIPT.

[nginx.conf:] config settings for advagg module see -> http://drupal.org/project/advagg

        # advagg_css support (for Drupal Advanced CSS/JS Aggregation module)
        location ~* /files/advagg_css/ {
            access_log         off;
            expires           365d;
            rewrite  ^/files/advagg_css/(.*)$ /sites/$host/files/advagg_css/$1 last;
            add_header Pragma "";
            add_header Cache-Control "public";
            add_header X-Header "AdvAgg CSS Generator 1.0";
            try_files $uri @drupal;
        }

        # advagg_js support (for Drupal Advanced CSS/JS Aggregation module)
        location ~* /files/advagg_js/ {
            access_log         off;
            expires           365d;
            rewrite  ^/files/advagg_js/(.*)$ /sites/$host/files/advagg_js/$1 last;
            add_header Pragma "";
            add_header Cache-Control "public";
            add_header X-Header "AdvAgg JS Generator 1.0";
            try_files $uri @drupal;
        }
...
...
        # send not cached requests to Drupal 6.x with clean URLs support
        location @drupal {
            rewrite ^/(.*)$  /index.php?q=$1 last;
        }
...
...
    # send non-static requests to php-fpm
    location ~ \.php$ {
          try_files $uri =404;            # check for existence of php file first
          fastcgi_pass   127.0.0.1:9000;  # php-fpm listening on port 9000
    }
...
...



Nginx STATIC 'etag' support

See: https://github.com/kkung/nginx-static-etags/
Compile this into nginx (source)


Nginx 'etag' [nginx.conf] Sample Usage: (for above 'static etags')

location / {
    ...
    etags               on;                     # etags required
    etag_hash           on;
    etag_hash_method    md5;
    ...
}



Nginx DYNAMIC 'etag' support
See: https://github.com/kali/nginx-dynamic-etags
Per Nginx DYNAMIC 'etag' 'readme':

Attempt at handling ETag / If-None-Match on proxied content.
I plan on using this to front a Varnish server using a lot of ESI

Etags Final Notes / Warning: The ETag format for Apache 1.3 and 2.x is 'inode-size-timestamp', please be sure to NOT include the 'inode'. Simply override the default and only use modify time [timestamp] and size [size] only. eg: "FileETag MTime Size"
This means that the same static object across cluster hosts can have the same etag! The same logic will apply for using Etags with Nginx. See code sample above:

mikeytown2’s picture

Status: Needs review » Fixed
StatusFileSize
new701 bytes

Added in the configuration from #11 into the readme.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

superfedya’s picture

>Added in the configuration from #11 into the readme.

nginx: [emerg] "location" directive is not allowed here in /usr/local/nginx//conf/nginx.conf:91
nginx: configuration file /usr/local/nginx//conf/nginx.conf test failed

Line 91: location ~* advagg_(?:css|js)/ {

Any suggestion?

Thanks

mikeytown2’s picture

Status: Closed (fixed) » Active

Based off of the recommendations in the CDN module (#974350-57: Far Future setting for Origin Pull mode), these are the headers we need to set/unset in nginx. The CDN module could use these rules translated over as well.

    Header set Cache-Control "max-age=290304000, no-transform, public"
    Header set Expires "Tue, 20 Jan 2037 04:20:42 GMT"
    Header set Last-Modified "Wed, 20 Jan 1988 04:20:42 GMT"
    Header unset ETag

These changes have been committed to AdvAgg in #1334460-12: Slow Loading CSS Aggregate

omega8cc’s picture

Here is modified Nginx config:

###
### advagg_css and advagg_js support
###
location ~* files/advagg_(?:css|js)/ {
  access_log off;
  expires    max;
  add_header ETag "";
  add_header Cache-Control "max-age=290304000, no-transform, public";
  add_header Last-Modified "Wed, 20 Jan 1988 04:20:42 GMT";
  try_files  $uri @drupal;
}

BTW: expires max; sets max-age for Cache-Control to 10 years, automatically - see http://wiki.nginx.org/NginxHttpHeadersModule#expires

omega8cc’s picture

@superfedya - see http://wiki.nginx.org/NginxHttpCoreModule#location

In short, location directive is expected inside server directive, but it is Nginx basics, not really discussed in this issue.

mikeytown2’s picture

Status: Active » Needs review
StatusFileSize
new878 bytes

I'll add the code without etags to the readme. Apache by default has etags turned on, I believe nginx has it off by default. I'll add in a note about etags in the comment block at the top. I believe add_header ETag ""; doesn't do what we want in this case.

omega8cc’s picture

I just translated it to Nginx config literally, but you are right that Nginx has no (default) support for ETag header. But you don't need any extra HttpHeadersMoreModule to unset most headers. The add_header SomeHeader ""; trick does the same - at least for headers we want to unset.

mikeytown2’s picture

Status: Needs review » Fixed
StatusFileSize
new704 bytes

Sounds good! Thanks for your help with this. The following patch has been committed.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.