user3814030
user3814030

Reputation: 1283

nginx configuration for expiration header

I need to do 2 things, first set the expiration header to 30d and second to enable the page speed module. Non of them work so far, this is my nginx.conf file

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    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  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
        expires 30d;
        add_header Pragma public;
        add_header Cache-Control "public";
    }

    include /etc/nginx/conf.d/*.conf; 
}

Upvotes: 0

Views: 454

Answers (1)

Hassan
Hassan

Reputation: 2843

For turning on pagespeed, you first need to build your nginx from source with pagespeed's module. Its very easy! You could just follow Google's instruction here and then here

After you complied Nginx from source with pagespeed module enabled, you could just add this to your conf:

pagespeed on;
pagespeed FileCachePath /var/ngx_pagespeed_cache;

To set the expiration header, I think it's better to put your code inside a server block, and then inside the main location block. See this blog post but it uses an if clause if you don't mind.

If you are optimizing your website, maybe consider using gzip on too in your conf. It compress the content before sending them to your clients. It saves you a lot of bandwidth and I think it reduce the latency (faster load).

If you are decided to use gzip with pagespeed, make sure to add below line to your conf and read

pagespeed FetchWithGzip on;

Upvotes: 1

Related Questions