提供静态内容 NGINX
Serving Static Content NGINX
我是 NGINX 的新手。我还不太了解它,但我正在尝试。
我很好奇使用 NGINX 从我的页面提供静态内容的最佳方式是什么。我想提供静态内容的主要原因是我想减少我的应用程序服务器的负载,并提高页面加载速度。
遇到了
几篇好文章帮助我把这些放在一起 post : here, here, here, and here.
但一切还是有点清晰。
配置
文件路径: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
保存我的文件后,我 运行 service nginx reload
.
接下来,我尝试了 运行 : curl -X GET -I http://45.33.69.160/index.php
我得到了:
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
然后,我尝试了运行curl -X GET -I http://45.33.69.160/css/custom.css
我得到了:
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
为什么我看到 Cache-Control: no-cache
而我刚刚设置了缓存?
我现在一切都不清楚。
问题
谁能说清楚如何做:
- 正确配置
- 测试该配置是否有效
- 查看缓存和不缓存的区别
- 对其进行基准测试并在页面或 CLI 上打印出该报告
?
Cache-Control: no-cache
如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所说,喜欢
proxy_ignore_headers Cache-Control;
配置
除此之外:在 NGINX reference about content caching 中,它表示要放置行
proxy_cache_path /data/nginx/cache keys_zone=one:10m;
在http
部分,后面是
proxy_cache one;
在server
部分。
测试
In this SF question,它表示通过配置文件
添加 X-Cache-Status
header 来测试缓存行为
add_header X-Cache-Status $upstream_cache_status;
Its answer 表示
You can view headers with
- the Firefox addon firebug
- the Chrome debugging console
- cURL (curl -I )
我是 NGINX 的新手。我还不太了解它,但我正在尝试。 我很好奇使用 NGINX 从我的页面提供静态内容的最佳方式是什么。我想提供静态内容的主要原因是我想减少我的应用程序服务器的负载,并提高页面加载速度。
遇到了
几篇好文章帮助我把这些放在一起 post : here, here, here, and here.
但一切还是有点清晰。
配置
文件路径: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
保存我的文件后,我 运行 service nginx reload
.
接下来,我尝试了 运行 : curl -X GET -I http://45.33.69.160/index.php
我得到了:
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
然后,我尝试了运行curl -X GET -I http://45.33.69.160/css/custom.css
我得到了:
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
为什么我看到 Cache-Control: no-cache
而我刚刚设置了缓存?
我现在一切都不清楚。
问题
谁能说清楚如何做:
- 正确配置
- 测试该配置是否有效
- 查看缓存和不缓存的区别
- 对其进行基准测试并在页面或 CLI 上打印出该报告
?
Cache-Control: no-cache
如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所说,喜欢
proxy_ignore_headers Cache-Control;
配置
除此之外:在 NGINX reference about content caching 中,它表示要放置行
proxy_cache_path /data/nginx/cache keys_zone=one:10m;
在http
部分,后面是
proxy_cache one;
在server
部分。
测试
In this SF question,它表示通过配置文件
添加X-Cache-Status
header 来测试缓存行为
add_header X-Cache-Status $upstream_cache_status;
Its answer 表示
You can view headers with
- the Firefox addon firebug
- the Chrome debugging console
- cURL (curl -I )