如何在 nginx 上添加 Expires headers

How to Add Expires headers on nginx

根据 GTMetrix:

有 6 个静态组件没有 far-future 有效期。

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 网站上收到这条消息的。我该如何解决?

GTMetrix指出的所有6个资源均来自外域并且过期header未设置far-future 到期日期,因为它们可能会非常频繁地更改,因此缓存它们可能不会产生预期的结果。

对于这些外部资源的过期 header 警告,您不能从服务器端做任何事情。 GTMetrix显示的只是一个警告,可以忽略。