4

I'm new to NGINX. I don't know a lot about it yet, but I'm trying to. I'm curious what is the best way to serve the static contents from my page using NGINX. The main reason why I want to serve the static contents is that I want put less load on my application servers, and increase the page load speed.


I came across

a couple good articles that help me put these together this post : here, here, here, and here.

But everything is still a little clear.


Configuration

File Path : etc/nginx/default

server {

    listen 80 default_server;
    server_name default;
    root /home/forge/site/public;

    location / {
        proxy_pass http://43.35.49.160/;
        try_files $uri $uri/ /index.php?$query_string;
    }

    # Media: images, icons, video, audio, HTC
    location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
        expires 1M;
        access_log off;
        add_header Cache-Control "public";
    }

    # CSS and Javascript
    location ~* \.(?:css|js)$ {
        expires 1y;
        access_log off;
        add_header Cache-Control "public";
    }
}

Test/Result

After saving my file, I run service nginx reload.

Next, I tried run : curl -X GET -I http://45.33.69.160/index.php

I got:

HTTP/1.1 200 OK
Server: nginx/1.6.3
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Cache-Control: no-cache
Date: Fri, 08 May 2015 15:14:55 GMT
Set-Cookie: XSRF-TOKEN=eyJpdiI6IkhPa2kwK1wvd2kxMFV0TURzYnMwSXFnPT0iLCJ2YWx1ZSI6IkFpSFpvakNjcGp0b0RWcVViYXJcLzRHbmo3XC9qbStYc2VzYVh4ZHVwNW45UGNQMmltZEhvSys1NjhZVzZmckhzOGRBUk5IU1pGK084VDF1ZmhvVkZ4MlE9PSIsIm1hYyI6IjliMzc5NWQ4MWRiMjM1NzUxNjcyNGNmYWUzMGQyMDk3MjlkYTdhYzgxYTI0OGViODhlMTRjZTI4MWE5MDU2MGYifQ%3D%3D; expires=Fri, 08-May-2015 17:14:55 GMT; Max-Age=7200; path=/
Set-Cookie: laravel_session=eyJpdiI6Iklhb041MkVBak0rVm5JeUZ0VVwvZ3pnPT0iLCJ2YWx1ZSI6IitRUFlzQzNmSm1FZ0NQVVFtaTJ4cG1hODlDa2NjVDgzdXBcLzRcL0ZSM1ZPOTRvRGo5QjQ1REluTUM3Vjd3cFptV3dWdHJweTY3QW5QR2lwTkZMUlNqbnc9PSIsIm1hYyI6IjIxOTZkYzM5ODE0N2E4YmQzODMxZGYzMDY3NjI4ODM1YWQxNGMxNDRlZDZmMGE1M2IwZWY2OTU4ZmVjOTIyMjkifQ%3D%3D; expires=Fri, 08-May-2015 17:14:55 GMT; Max-Age=7200; path=/; httponly

Then, I tried run curl -X GET -I http://45.33.69.160/css/custom.css

I got :

HTTP/1.1 200 OK
Server: nginx/1.6.3
Date: Fri, 08 May 2015 15:16:03 GMT
Content-Type: text/css
Content-Length: 2890
Last-Modified: Thu, 07 May 2015 03:02:38 GMT
Connection: keep-alive
ETag: "554ad5ce-b4a"
Accept-Ranges: bytes

Why do I see Cache-Control: no-cache and I just set up the cache ?

Everything is just unclear to me right now.


Questions

Can someone please make it clear on how to :

  • configure this properly
  • test that configuration if it is work
  • see the different between caching and not caching
  • benchmark it and print out that report on a page or CLI

?

Community
  • 1
  • 1
cyb3rZ
  • 43,853
  • 82
  • 251
  • 430

1 Answers1

4

Cache-Control: no-cache

As said in this answer about no-cache, which links to the spec, the Cache-Control: no-cache should tell the user agent and in-between caches which caching style to use (namely to revalidate each time with the server). This applies if you use nginx exclusively. If you use it as a pass-through, you need to set proxy_ignore_headers, like

proxy_ignore_headers Cache-Control;

Config

Apart from that: in the NGINX reference about content caching, it says to put the line

    proxy_cache_path /data/nginx/cache keys_zone=one:10m;

in the http part, followed by

        proxy_cache one;

in the server part.

Testing

In this SF question, it says to test caching behavior by adding the X-Cache-Status header via the config file

add_header X-Cache-Status $upstream_cache_status;

Its answer states that

You can view headers with

  • the Firefox addon firebug
  • the Chrome debugging console
  • cURL (curl -I )
Community
  • 1
  • 1
serv-inc
  • 29,557
  • 9
  • 128
  • 146