serve static files directly
-
Howdy
Just been speed testing some sites (for a change) and one thing that was flagged was:
"Leverage browser caching"
Did a bit of digging and found this code to use in nginx.conf:
# serve static files directly
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico)$ {
expires 30d;
}Which I guess caches those file extentions for 30 days, reducing bandwidth & page load speed.
So I added that (had to create a sever block), restarted nginx - and got this:
Starting nginx: [emerg]: unknown directive "server" in /usr/local/nginx/conf/nginx.conf:
Hmmff
Looking at other peoples nginx.conf files they have server blocks in fine.
Am I missing something!
Cheers
Ben.Posted 1 year ago # -
Ok - so worked out that I can put this in the actual domain conf file
usr/local/nginx/sites-available/site.com
And that seems to be working fine :)
Is there a way to do this server wide?
Posted 1 year ago # -
Is there a way to do this server wide?
In /usr/local/nginx/conf/nginx.conf I added an expires setting:
http { expires 1M; }followed by
n2r sudo /etc/init.d/php-fpm restartAfter doing a hard refresh on the page, this seemed to satisfy the Google Webmaster Tools Page Speed analyzer.
Posted 1 year ago # -
Is there a way to do this server wide?
In /usr/local/nginx/conf/nginx.conf I added an expires setting:
http { expires 1M; }followed by
n2r sudo /etc/init.d/php-fpm restartAfter doing a hard refresh on the page, this seemed to satisfy the Google Webmaster Tools Page Speed analyzer.
Posted 1 year ago # -
there is Ben .. pop it in the default vhost:-
usr/local/nginx/sites-available/default
.. you could add to the nginx.conf too but it's pretty untidy.
Posted 1 year ago # -
i did the same but doesn't work...
usr/local/nginx/sites-available/defaultserver {
listen 80;
server_name localhost;
location / {
root html;
index index.php index.html index.htm;
}
# redirect server error pages to the static page /50x.htmlerror_page 500 502 503 504 /50x.html;
location = /50x.html
{
root html;
}
# serve static files directly
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|swf)$ {
expires 30d;
}
am I doing something wrong???Posted 1 year ago # -
This thread is a bit jumbled...(to me anyhow)
What should we include in which file in order to leverage browser caching? Please
Posted 2 months ago # -
I put this code
# serve static files directly
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico)$ {
expires 30d;
}In
usr/local/nginx/sites-available/mydefaultsite.comDoesn't work!
Please, help me! :|
Posted 2 months ago # -
Marcus, this thread aside, the guide examples xcache and includes the configuration. That doesn't mean that this is the only caching option available. What kind of CMS are you using?
famfam, was this not working for you previously? What has changed from your original config or is this a new box? Are you installing Nginx from repositories or compiling?
Posted 2 months ago # -
Am using WP at the moment, Guv
I have set up opcode caching with APC anyhow....
There is a module in W3Total Cache that allows you to leverage browser caching, but I want to do it the proper way..
Posted 2 months ago # -
Hi Guv,
Nothing has changed in my original config.
I follow your tutorial to install nginx sometime ago.
Testing I found that this option works.
server { listen 80; server_name mydomain.com; rewrite ^/(.*) http://www.mydomain.com/$1 permanent; } server { listen 80; server_name http://www.mydomain.com; access_log /home/user/public_html/mydomain.com/log/access.log; error_log /home/user/public_html/mydomain.com/log/error.log; location / { root /home/user/public_html/mydomain.com/public/; index index.php index.html; include /usr/local/nginx/conf/wordpress_params.super_cache; } # Add expires header for static content #location ~* \.(?:ico|css|js|gif|jpe?g|png)$ { expires 2592000s; add_header Pragma "public"; add_header Cache-Control "max-age=2592000, public, must-revalidate, proxy-revalidate"; # } # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include /usr/local/nginx/conf/fastcgi_params; fastcgi_param SCRIPT_FILENAME /home/user/public_html/mydomain.com/public/$fastcgi_script_name; } }Is it correct?
Posted 2 months ago # -
nice Famfam
it works...
Posted 2 months ago # -
Marcus I know it's work but is not specific for images... I think this need to be fixed.
Posted 2 months ago #
Reply
You must log in to post.
Want HTML?
a blockquote code em strong ul ol liPlace code between backticks `codeHere`
You've got it.
