Posted by Terko on February 10, 2010 at 7:15pm
I am trying to get better rating from YSlow. The tool is reporting, that the files in my misc folder Misc don't have expire header.
# (no expires) http://uhaaa.net/misc/collapse-fix.js
# (no expires) http://uhaaa.net/misc/compat-1.0.jsI was trying different combinations to make this folder with right headers, but without success.
Please, give me idea how to do this.
Adding this in nginx.conf didn't help
location ^~ /misc/ {
access_log off;
expires 30d;
}
Comments
I found solution Adding
I found solution
Adding this:
location ~ .js$ {if ( $request_method !~ ^(GET|HEAD)$ ) {
return 405;
}
if ($http_cookie ~ "DRUPAL_UID") {
return 405;
}
error_page 405 = @uncached;
access_log off;
expires max; # if using aggregator
add_header X-Header "Boost Citrus 2.2";
}
I get this sample code from http://groups.drupal.org/node/26363
Just removed the line that says "try files"
Thanks omega88c again. Your sample codes are helping me a lot :)
Edit:
It's strange, but now it's not working again :(
Post your whole config, if
Post your whole config, if you like.
Thanks! This is the
Thanks!
This is the config
#----------------------------------------------------------------------
# Events Module
#
# http://wiki.codemongers.com/NginxEventsModule
#
#----------------------------------------------------------------------
user nginx;
worker_processes 4;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 128;
#log_format main '$remote_addr - $remote_user [$time_local] $request '
# '"$status" $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
error_log /var/log/nginx_error.log debug;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 20;
tcp_nodelay on;
#gzip on;
# output compression saves bandwidth
gzip on;
gzip_http_version 1.1;
gzip_vary on;
gzip_comp_level 6;
gzip_proxied any;
gzip_types text/plain text/html text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
# make sure gzip does not lose large gzipped js or css files
# see http://blog.leetsoft.com/2007/7/25/nginx-gzip-ssl
gzip_buffers 16 8k;
# Disable gzip for certain browsers.
gzip_disable “MSIE [1-6].(?!.SV1)”;
client_max_body_size 10m;
server {
listen 80; # Replace this IP and port with the right ones for your requirements
server_name uhaaa.com uhaaa.net joke.bg parfumcheta.com supertune.eu supermama.bg; # Multiple hostnames seperated by spaces. Replace these as well.
#charset koi8-r;
#access_log logs/host.access.log main;
access_log off;
location = / {
root /home/uhaaa22/public_html; # Again, replace this.
index index.php;
access_log off;
}
location / {
root /home/uhaaa22/public_html;
index index.php index.html;
if (!-e $request_filename) {
rewrite ^/(.)$ /index.php?q=$1 last;
expires max;
break;
}
}
error_page 404 /index.php;
# serve static files directly
location ^~ ^.+.(jpg|jpeg|gif|css|png|js|ico)$ {
access_log off;
expires 30d;
}
location ~* .(ico|css|js|gif|jp?g|png)\?[0-9]+$ {
expires max;
break;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ .php$ {
fastcgi_pass 127.0.0.1:9000; # By all means use a different server for the fcgi processes if you need to
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /home/uhaaa22/public_html$fastcgi_script_name; # !! <--- Another path reference for you.
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
include /etc/nginx/fastcgi_params;
}
# imagecache needs to have php read any files that it's planning to manipulate
location ^~ /files/imagecache/ {
access_log off;
index index.php index.html;
# assume a clean URL is requested, and rewrite to index.php
if (!-e $request_filename) {
rewrite ^/(.)$ /index.php?q=$1 last;
break;
}
}
location ^~ /jokefiles/imagecache/ {
access_log off;
index index.php index.html;
# assume a clean URL is requested, and rewrite to index.php
if (!-e $request_filename) {
rewrite ^/(.)$ /index.php?q=$1 last;
break;
}
}
location ^~ /car/imagecache/ {
access_log off;
index index.php index.html;
# assume a clean URL is requested, and rewrite to index.php
if (!-e $request_filename) {
rewrite ^/(.)$ /index.php?q=$1 last;
break;
}
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /.ht {
# deny all;
#}
}
server {
listen 80;
server_name cdn1.uhaaa.net cdn2.uhaaa.net cdn3.uhaaa.net;
access_log off;
root /home/uhaaa22/public_html;
server_name_in_redirect off;
location / {
return 404;
# or
# rewrite ^ http://www.example.com permanent;
access_log off;
}
location ~ ^.+.(jpg|jpeg|gif|png|ico|js|css)$ {
# access_log off;
# error_log /dev/null crit;
expires 45d;
access_log off;
try_files $uri @drupal;
}
location @drupal {
###
### now simplified to reduce rewrites
###
rewrite ^/(.)$ http://uhaaa.net/$1 permanent; }
}
server {
listen 80;
server_name cdn1.uhaaa.com cdn2.uhaaa.com cdn3.uhaaa.com;
access_log off;
root /home/uhaaa22/public_html;
server_name_in_redirect off;
access_log off;
location / {
return 404;
# or
# rewrite ^ http://www.example.com permanent;
}
location ~ ^.+.(jpg|jpeg|gif|png|ico|js|css)$ {
# access_log off;
# error_log /dev/null crit;
expires 45d;
access_log off;
try_files $uri @drupal;
}
location @drupal {
###
### now simplified to reduce rewrites
###
access_log off;
rewrite ^/(.*)$ http://uhaaa.com/$1 permanent; }
}
server {
listen 80;
server_name pma777.uhaaa.com;
access_log off;
root /home/uhaaa22/pma777;
server_name_in_redirect off;
}
}
I don't know what this
I don't know what this location is supposed to do:
location ~* .(ico|css|js|gif|jp?g|png)\?[0-9]+$ {expires max;
break;
}
I suggest you comment it out unless you know what it's for.
Try editing:
# serve static files directlylocation ^~ ^.+\.(jpg|jpeg|gif|css|png|js|ico)$ {
access_log off;
expires 30d;
}
to remove the tilde(~) and make it:
location ~ ^.+\.(jpg|jpeg|gif|css|png|js|ico)$ {I've removed the first
I've removed the first statement, maybe it's left from my other tries to make everything work well :)
I've made and your other suggestion, but there is no change after doing that. YSlow gives me again that these files located in misc folder are without expire header.
On the net tab it shows me
On the net tab it shows me that it's not finding or serving those files at all.
They're not Drupal core files so it's already a bit of a hack to have them residing in a Drupal core directory like /misc/. That looks like some kind of old jquery upgrade hack.
Are you sure they're in /misc?
They are from Drigg project.
They are from Drigg project. In page.tpl.php there is this code:
<!-- REMOVE these if you are using jquery-update!! --><script type="text/javascript" src="<?php print $base_path ?>misc/collapse-fix.js"></script>
<script type="text/javascript" src="<?php print $base_path ?>misc/compat-1.0.js"></script>
I wonder if it's better to use jquery, but I believe, that it's slower than that files.
At this moment they're just
At this moment they're just 404. Have you confirmed that they're actually in the /misc folder and that their permissions are set correctly for the web server user to read?
Edit: Hmm...they're reading now.
Brian, this was caused by
Brian, this was caused by removing the ^ sign. My site Uhaaa.com stopped loading css after removing ^, maybe the CSS in the folder too.
So I've returned the ^ sign again.
Maybe I can't move these files in /files folder and rewrite the template to read them from
<script type="text/javascript" src="<?php print http://uhaaa.net/collapse-fix.js"></script>Interesting. The regex isn't
Interesting. The regex isn't catching those for some reason. Probably the hyphen is throwing it off.
Try:
location ~ ^.*\.(jpg|jpeg|gif|css|png|js|ico)$ {For some reason something
For some reason something goes wrong and every link in the site was non working. I've restored my old nginx.conf. Maybe I was set expire for something that it's not supposed to be cached :)
I've restored my config and
I've restored my config and tried your last proposal, but without luck. Uhaaa.net works, but CSS styles of Uhaaa.com are destroyed. Maybe this is issue with this regex of my server. It just don't work without ^ sign. So all my locations are with this sign. I remember, that it was hard to find why many examples don't work for me when I've made the first setup of nginx.
I'm sorry I can't duplicate
I'm sorry I can't duplicate this. I've tried files with crazy names on my test server like file-file.01.file-2.jpg and they still get picked up by my default location:
location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|js)$ {Thank you Brian for your
Thank you Brian for your support! I will try to upgrade Nginx to the latest version and I will try again.
I struggle to get nginx to
I struggle to get nginx to set an expire header to the files generated by imagecache. I've tried everything I've found on drupal.org without having any success...
All files that are used in the theme has an expire-header, just not the imagecache-generated files...
location ~* .(css|js|jpg|jpeg|png|gif|ico)$ {expires 45d;
}
It seems, when testing with YSlow, that everything except the images generated by imagecache are effected by that code... :/
Any ideas why?
This code affects images
This code affects images after they were created by imagecache. First image creation and delivery (first time for this image with this preset) will be handled by imagecache and the expires header are not set in this case.
So.. if I understand you
So.. if I understand you correctly... there is no way to make the expire header permanent (after imagecache has created them)?
No, I was only relating to
No, I was only relating to the first time the image is created. After that it should be handled by nginx, and with the expires header you define to nginx.
I have not checked the nginx config file used in this thread, but testing my own (http://github.com/yhager/nginx_drupal) for already created imagecache files, the expires header is shown as required (30 days in my setup):
Strange...can't see anything
Strange...can't see anything in your config that may be missing in mine... Here's my page: http://idpass.no/
Current config: http://pastebin.com/30ZaaSbY
If you have any suggestions, I would highly appreciate all feedback :)
Try the following
Try the following change:
--- 30ZaaSbY.txt 2010-02-25 18:30:50.000000000 +0200 +++ 30ZaaSbY.yh.txt 2010-02-25 18:31:25.000000000 +0200 @@ -33,7 +33,7 @@ server { } # imagecache needs to have php read any files that it's planning to manipulate - location ^~ /sites/idpass.no/files/imagecache/ { + location /sites/idpass.no/files/imagecache/ { index index.php index.html; # assume a clean URL is requested, and rewrite to index.php if (!-e $request_filename) {Your beyond awesome yhager!
Your beyond awesome yhager! :D
I really don't now how to thank you!
Glad to have helped :)
Glad to have helped :)
Seems like it worked, but
Seems like it worked, but once I flush the images they are never generated again... :P
I also tried to change it back like I had and add another rule, but that didn't help either..
# performance for imageslocation ~* .(css|js|jpg|jpeg|png|gif|ico)$ {
expires 45d;
}
location /sites/idpass.no/files/imagecache/ {
expires 45d;
}
Right. You need to find a way
Right. You need to find a way to tell nginx to *try* the file and pass it on to drupal if it does not exist. I am doing this using nginx try_files directive. This is where the magic happens:
# serve imagecache files directly or redirect to drupal if they do not exist location ~* imagecache { access_log off; expires 30d; try_files $uri @drupal; } # serve static files directly location ~* ^.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|swf|flv)$ { access_log off; expires 30d; } # This rewrites pages to be sent to PHP processing location @drupal { rewrite ^/(.*)$ /index.php?q=$1 last; }also read carefully the location directive documentation on nginx site.
I see.. I guess my real
I see.. I guess my real problem is that I use the nginx-version supplied by Debian which seems to be pretty old (0.6.32-3) and "try_files" is not a valid directive.
I'll search for an updated package and see if I manage to solve it, thanks again for a very quick answer... :)
To everyone who might have
To everyone who might have the same problem in the future...
Make sure nginx is up to date and use the config provided by yhager. It works great! :)
# serve imagecache files
# serve imagecache files directly or redirect to drupal if they do not existlocation ~* imagecache {
access_log off;
expires 30d;
error_page 404 = @drupal;
}
To confirm...
Brian, are you saying to replace yhager's snippet of his nginx config with the code you've provided? So the entire snippet would be:
# serve imagecache files directly or redirect to drupal if they do not exist
location ~* imagecache {
access_log off;
expires 30d;
error_page 404 = @drupal;
}
# serve static files directly
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|html|xml|swf|flv)$ {
access_log off;
expires 30d;
}
# This rewrites pages to be sent to PHP processing
location @drupal {
rewrite ^/(.*)$ /index.php?q=$1 last;
}
Thanks!
--Kelly Bell
Gotham City Drupal
twitter: @kbell | @gothamdrupal
http://drupal.org/user/293443
Try yaeger's config. They
Try yaeger's config. They probably both work. Honestly I don't remember from 3 years ago why I posted that change and my current config is slightly different.
I now use:
location ~* imagecache {access_log off;
error_log /dev/null crit;
expires 45d;
error_page 404 /index.php?q=$uri_args;
}
but that also requires this part somewhere at the http{} level:
map $uri&$args $uri_args {default '';
~^/(?<key>.*)$ $key;
}
These changes may not mean much without the full config.
(No subject)