nginx不可以緩衝偽靜態網頁嗎?

來源:互聯網
上載者:User
最近想用nginx的proxy_cache緩衝網站的一些頁面,一台nginx做proxy,兩台web,web站是PHP的偽靜態頁面,測試的時候發現nginx無法緩衝後端為偽靜態html,靜態html可以!是我哪裡配置錯了,還是nginx本身就無法緩衝偽靜態頁面?

回複內容:

最近想用nginx的proxy_cache緩衝網站的一些頁面,一台nginx做proxy,兩台web,web站是PHP的偽靜態頁面,測試的時候發現nginx無法緩衝後端為偽靜態html,靜態html可以!是我哪裡配置錯了,還是nginx本身就無法緩衝偽靜態頁面?

如果後端有set-cookie,nginx也是不會緩衝的,加上此配置proxy_ignore_headers Set-Cookie就可緩衝。另外這幾個頭部(Expires、E-Tags、Last-Modified、Cache-Control)的值對緩衝也是影響的

如果返回的頁面沒有返回 Expires、E-Tags、Last-Modified、Cache-Control 等表示這個檔案可以緩衝的http頭,nginx是不會進行緩衝的
這跟瀏覽器的行為是一樣的, 如果server返回的http頭部表明了這個檔案可以緩衝,nginx或者瀏覽器才會進行緩衝,否則就認為是動態網頁面。

這個時候nginx-cache-purge這個模組就很有用了,他可以協助你管理和清除fastcgi_cache

安裝方法

mkdir /home/cache/wpcache -pcd ~./lnmp stopapt-get install git -y #centos用yum install git -ygit clone https://github.com/FRiCKLE/ngx_cache_purgewget http://soft.vpser.net/web/nginx/nginx-1.0.15.tar.gztar zxvf nginx-1.0.15.tar.gzcd nginx-1.0.15/./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-ipv6 --add-module=../ngx_cache_purgemakemake install/root/lnmp start

這裡我以wordpress為例,發一個配置方法

http

#在http層添加以下三行代碼fastcgi_cache_path /home/cache/wpcache levels=1:2 keys_zone=wpcache:10m inactive=20m;fastcgi_cache_key "$scheme$request_method$host$request_uri";fastcgi_cache_use_stale error timeout invalid_header http_500;#wpcache為名字,可以隨便修改。10m為記憶體佔用

server

    set $no_cache 0;    # 不緩衝POST操作    if ($request_method = POST) {        set $no_cache 1;    }       if ($query_string != "") {        set $no_cache 1;    }       # 不緩衝後台    if ($request_uri ~* "(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/|index.php|wp-comments-popup.php|wp-links-opml.php|wp-locations.php|sitemap(_index)?.xml|[a-z0-9_-]+-sitemap([0-9]+)?.xml)") {        set $no_cache 1;    }       # 已登入的不緩衝(防止留言串號)    if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {        set $no_cache 1;    }    location / {        try_files $uri $uri/ /index.php?$args;    }        location ~ .php$ {        try_files $uri /index.php;         include fastcgi_params;        #Lnmp.org一鍵包改成include fcgi.conf;        fastcgi_pass unix:/var/run/php5-fpm.sock;     #Lnmp.org一鍵包改成fastcgi_pass  unix:/tmp/php-cgi.sock;        fastcgi_cache_bypass $no_cache;            fastcgi_no_cache $no_cache;        fastcgi_cache wpcache;    #要跟前面設定的名稱一樣        fastcgi_cache_valid  30m;   #緩衝時間    }    location ~ /purge(/.*) {            allow 11.22.33.44;  #此處該為你vps的ip            allow 8.8.8.8;            allow 127.0.0.1;            deny all;        fastcgi_cache_purge wpcache "$scheme$request_method$host$1";    }

估計你瞭解php-fpm中的緩衝。

nginx不僅有個大家很熟悉的緩衝代理後端內容的proxy_cache,還有個被很多人忽視的fastcgi_cache。
proxy_cache的作用是緩衝後端伺服器的內容,可能是任何內容,包括靜態和動態。
fastcgi_cache的作用是緩衝fastcgi產生的內容,很多情況是php產生的動態內容。
proxy_cache緩衝減少了nginx與後端通訊的次數,節省了傳輸時間和後端寬頻。
fastcgi_cache緩衝減少了nginx與php的通訊的次數,更減輕了php和資料庫(mysql)的壓力,這比用memcached之類的緩衝要輕鬆得多。

via http://www.linuxyan.com/web-server/78.html

http://segmentfault.com/blog/qianfeng/1190000000430660

本來想談談fastcgi_cache,但是 @李惟 長篇大論了一番,搶了我的彩頭。
如果那些偽靜態頁面不常改動,在HTTP Header裡也配置上緩衝,連nginx的事都省了不少。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.