Serving static NGINX content
I am new to NGINX . I don't know about it yet, but I'm trying. I am curious about the best way to use static content in my page using NGINX. The main reason I want to serve static content is because I want to put less load on my application servers and increase page load speed.
I came across
some good articles to help me consolidate this post: here , here , here and here .
But it's still a little clear.
Configuration
The path to the file: 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 ran service nginx reload
.
Next, I tried to 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 running 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 am I seeing Cache-Control: no-cache
and I just configured the cache?
Everything is just unclear to me now.
Questions
Can someone please clarify how:
- set it up correctly
- check the configuration if it works.
- see the section between caching and not caching
- check this and print this report to page or CLI
?
source to share
Cache-Control: no-cache
As said in this answer about no-cache , which refers to the spec , it Cache-Control: no-cache
should tell the user agent and intermediate caches that use the caching style (namely to repeat the check with the server each time). This is applicable if you are exclusively using nginx. If you are using it as pass -through then you need to set proxy_ignore_headers like
proxy_ignore_headers Cache-Control;
Config
Also: in the NGINX link about content caching , it says to put the line
proxy_cache_path /data/nginx/cache keys_zone=one:10m;
in the part http
followed by
proxy_cache one;
in part server
.
Testing
In this SF question , he says to test the caching behavior by adding a header X-Cache-Status
via the config file
add_header X-Cache-Status $upstream_cache_status;
His answer says that
You can view the headers with
- Firefox addon firebug
- chrome debug console
- cURL (curl -I)
source to share