如何在nginx上添加Expires头文件

时间:2015-02-03 00:45:35

标签: nginx

根据GTMetrix的说法:

有6个静态组件没有远期到期日。

http://linuxedgr.disqus.com/recent_comments_widget.js?num_items=5&hide_mods=0&hide_avatars=0&avatar_size=32&excerpt_length=100
http://s7.addthis.com/js/300/addthis_widget.js#pubid=ra-548e3c553a19ddf3
http://fonts.googleapis.com/css?family=Open+Sans:400italic,700italic,400,700
http://www.google-analytics.com/ga.js
http://a.disquscdn.com/count.js
http://linuxedgr.disqus.com/count-data.js?1=egkatastasi-tou-nginx&1=eisagogi-ston-nginx&1=new-dedicated-server-in-da-house-me-5-euro&1=o-epimenon-nika&1=pos-mporo-na-kano-encrypt-kai-decrypt-arxeia-xrismopoiontas-to-openssl-toolkit&1=pos-sindeo-ena-domain-apo-tin-IPHost-me-tin-ip-tou-server-mou

在我的/etc/nginx/vhosts.d/www.linuxed.gr.conf

server {
    listen 80;

        # Δώσε το absolute path που εχεις τα αρχεία του website
    root /var/www/html/linuxed;

    # Όριστε το index file
    index index.html index.html index.php;

    # Δώσε το όνομα του domain και το alias
    # τα οποια πρεπει να τα εχει δηλώσει και στο /etc/hosts
    server_name www.linuxed.gr linuxed.gr;

        # Συμπίεση 
        gzip on;
        gzip_disable "msie6";
        gzip_comp_level 6;
        gzip_min_length  1100;
        gzip_buffers  16 8k;
    gzip_http_version 1.1;
        gzip_proxied     any;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;        

    # specify a vary: accept-encoding header
        gzip_vary on;   

    # Όριστε μία 404 page
        error_page  404              /error-404.html;
        location = /error-404.html {
        root   /var/www/html/linuxed/;
        }  


        # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
        location = /50x.html {
        root   /usr/share/nginx/html/;
        }

        # Disable favicon.ico logging
        location = /favicon.ico {
            log_not_found off;
            access_log off;
        }

        # Allow robots and disable logging
        location = /robots.txt {
            allow all;
            log_not_found off;
            access_log off;
        }

    ############################
    # Leverage browser caching #
    ############################
    location ~*  \.(jpg|jpeg|png|gif|ico|css|js)$ {
            expires max;
            log_not_found off;
        add_header Pragma public;
            add_header Cache-Control "public";
        } 

    # Deny access to htaccess and htpasswd files
        location ~ /\.ht {
            deny  all;
        }
}

此处还有我的/etc/nginx/nginx.conf

    user nginx;
worker_processes  4;

error_log  /var/log/nginx/error.log;

pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
    use epoll;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    types_hash_max_size 2048;   

    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 off;

    # Tcp_nopush causes nginx to attempt to send its HTTP response head in one packet, 
    # instead of using partial frames. This is useful for prepending headers before
    #  calling sendfile or for throughput optimization.
    tcp_nopush on;

    # don't buffer data-sends (disable Nagle algorithm). Good for sending frequent
    # small bursts of data in real time.
    tcp_nodelay on; 

    sendfile        on;

    # allow the server to close the connection after a client stops responding. 
    #Frees up socket-associated memory.
    reset_timedout_connection on;

    gzip on;
    gzip_disable "msie6";

    client_body_buffer_size 10k;
    client_header_buffer_size 1k;
    client_max_body_size 8m;
    large_client_header_buffers 2 1k;

    client_body_timeout 12;
    client_header_timeout 12;
    keepalive_timeout 15;
    send_timeout 10;

    include conf.d/*.conf;
    include vhosts.d/*.conf;


}

我认为我的配置非常正确。我怎么在GTMetrix网站上收到此消息。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

GTMetrix指出的所有6个资源都来自 外部域 ,并且过期标头未设置为远期到期日期,因为它们可能会经常更改,因此缓存它们可能无法产生预期的效果。

您无法从服务器端对这些外部资源的过期标头警告做任何事情。 GTMetrix显示的只是一个可以忽略的警告。