Nginx to set static page compression and cache expiration Time _nginx

Source: Internet
Author: User
Tags nginx server

Friends using the Nginx server may be aware of the need to set the HTML static page cache with page compression and expiration time settings, I would like to introduce to you the configuration method, including the Ico,gif,bmp,jpg,jpeg,swf,js,css, MP3 files are cached locally.

The server's bandwidth overhead can be reduced by nginx the static resources on the server and compressing the resources.

The following is how Nginx sets the static resource expiration time:

    Location ~*. (Ico|gif|bmp|jpg|jpeg|png|swf|js|css|mp3) {
      root  /var/www/opkeep;
      Expires 30d;
    }


The above configuration can cache the Ico,gif,bmp,jpg,jpeg,swf,js,css,mp3 files locally, without having to retrieve them again from the server every time you access them.

The compressed configuration is as follows:

    gzip on;
    Gzip_min_length 1000;
    Gzip_buffers   4 8k;
    Gzip_types    text/plain application/x-javascript text/css;


For text, JS and CSS file compression, under normal circumstances, compressed size is the original size of 25%, or even smaller.


Cache dynamic pages, cache expiration time

This is configured with a virtual machine:

Nginx Configuration file content:

This sentence is mainly:

Proxy_cache_path/www/levels=1:2 keys_zone=z:10m inactive=1m max_size=30g;

This sentence defines an area, the name is Z, in the memory space is 10MB, the maximum space in the hard disk is 30G;

Inactive=1m is, after 1 minutes cache failure, request from the source server from the new
Right here, inactive=1m. If the cache is not accessed for 1 minutes, Nginx deletes the cache.

/usr/local/nginx/conf/nginx.conf

#user nobody;

Worker_processes 1;
#error_log Logs/error.log;
#error_log Logs/error.log Notice;

#error_log Logs/error.log Info;


#pid Logs/nginx.pid;


Events {worker_connections 1024;}
  HTTP {include mime.types;

  Default_type Application/octet-stream; #log_format Main ' $remote _addr-$remote _user [$time _local] "$request" ' # ' $status $body _bytes_sent ' $http _refe

  RER "' # '" $http _user_agent "" $http _x_forwarded_for ";

  #access_log Logs/access.log Main;
  Sendfile on;

  #tcp_nopush on;
  #keepalive_timeout 0;

  Keepalive_timeout 65;
  #gzip on;
  Proxy_cache_path/www/levels=1:2 keys_zone=z:10m inactive=1m max_size=30g;
    server {Listen 80;

    server_name localhost;

    #charset Koi8-r;

    #access_log Logs/host.access.log Main;
   Location/{root/www/;

      #expires Max;
      #proxy_store on;
      #proxy_store_access USER:RW GROUP:RW ALL:RW;
   #proxy_temp_path/www/;
   Proxy_cache Z; Proxy_cache_valid 200 1M
      #expires Max;
 
      Include proxy.conf;
      if (!-e $request _filename) {Proxy_pass http://192.168.1.199:45815;
      #这里设置当 access to the contents of the/ajax/directory, read directly from the source server, mainly for AJAX access requests, real-time location/ajax/{include proxy.conf;
      if (!-e $request _filename) {Proxy_pass http://192.168.1.199:45815; }} #location ~.*.
    (jpg|png|jpeg|gif) #{# expires Max;

    #} #error_page 404/404.html;
    # REDIRECT Server error pages to the static page/50x.html # Error_page 502 503 504/50x.html;
    Location =/50x.html {root html; # Proxy The PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ php$ {# Proxy_pass http:/
    /127.0.0.1;      # Pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ php$ {# root
    html
    # Fastcgi_pass 127.0.0.1:9000;
    # Fastcgi_index index.php; # Fastcgi_param SCRIPT_FILENAME/SCRIPTS$FAStcgi_script_name;
    # include Fastcgi_params; #} # Deny access to. htaccess files, if the Apache ' s document Root # concurs with Nginx ' s one # #location ~/
    . HT {# deny All; #}} # Another virtual host using mix of ip-, name-, and port-based configuration # #server {# Listen 800
  0;
  # Listen somename:8080;

  # server_name somename alias Another.alias;
  # location/{# root HTML;
  # index index.html index.htm;
  #} # # HTTPS server # #server {# listen 443;

  # server_name localhost;
  # SSL on;
  # ssl_certificate Cert.pem;

  # Ssl_certificate_key Cert.key;

  # ssl_session_timeout 5m;
  # ssl_protocols SSLv2 SSLv3 TLSv1; # ssl_ciphers high:!anull:!
  MD5;

  # ssl_prefer_server_ciphers on;
  # location/{# root HTML;
  # index index.html index.htm;

 #  }
  #}

}

/usr/local/nginx/conf/proxy.conf

Proxy_redirect off     ;
Proxy_set_header    Host $host;
Proxy_set_header    x-real-ip $remote _addr;
Proxy_set_header    x-forwarded-for $proxy _add_x_forwarded_for;
Proxy_set_header    accept-encoding ' gzip ';
Client_max_body_size  100m;
Client_body_buffer_size 256k;

Proxy_connect_timeout  ;
Proxy_send_timeout   ;
Proxy_read_timeout   ;

Proxy_buffer_size    512k;
Proxy_buffers      8 512k;
Proxy_busy_buffers_size 512k;
Proxy_temp_file_write_size 512k;

HTML files can be cached and then go to a lot of places to ask

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.