Author Topic: Ubuntu-Nginx-PHP5-fpm-Varnish  (Read 3966 times)

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2141
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
Ubuntu-Nginx-PHP5-fpm-Varnish
« on: มิถุนายน 17, 2016, 02:01:26 PM »
# BEGIN W3TC Browser Cache
gzip on;
gzip_types text/css text/x-component application/x-javascript application/javascript text/javascript text/x-js text/richtext image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon;
location ~ \.(css|htc|js|js2|js3|js4)$ {
    expires 31536000s;
    add_header Pragma "public";
    add_header Cache-Control "max-age=31536000, public";
    add_header X-Powered-By "W3 Total Cache/0.9.3";
}
location ~ \.(html|htm|rtf|rtx|svg|svgz|txt|xsd|xsl|xml)$ {
    expires 3600s;
    add_header Pragma "public";
    add_header Cache-Control "max-age=3600, public";
    add_header X-Powered-By "W3 Total Cache/0.9.3";
}
location ~ \.(asf|asx|wax|wmv|wmx|avi|bmp|class|divx|doc|docx|eot|exe|gif|gz|gzip|ico|jpg|jpeg|jpe|json|mdb|mid|midi|
mov|qt|mp3|m4a|mp4|m4v|mpeg|mpg|mpe|mpp|otf|odb|odc|odf|odg|odp|ods|odt|ogg|pdf|png|pot|pps|ppt|
pptx|ra|ram|svg|svgz|swf|tar|tif|tiff|ttf|ttc|wav|wma|wri|xla|xls|xlsx|xlt|xlw|zip)$ {
    expires 31536000s;
    add_header Pragma "public";
    add_header Cache-Control "max-age=31536000, public";
    add_header X-Powered-By "W3 Total Cache/0.9.3";
}
# END W3TC Browser Cache
# BEGIN W3TC Skip 404 error handling by WordPress for static files
if (-f $request_filename) {
    break;
}
if (-d $request_filename) {
    break;
}
if ($request_uri ~ "(robots\.txt|sitemap(_index)?\.xml(\.gz)?|[a-z0-9_\-]+-sitemap([0-9]+)?\.xml(\.gz)?)") {
    break;
}
if ($request_uri ~* \.(css|htc|js|js2|js3|js4|html|htm|rtf|rtx|svg|svgz|txt|xsd|xsl|xml|asf|asx|wax|wmv|wmx|avi|bmp|class|divx|
doc|docx|eot|exe|gif|gz|gzip|ico|jpg|jpeg|jpe|json|mdb|mid|midi|mov|qt|mp3|m4a|mp4|m4v|mpeg|mpg|mpe|
mpp|otf|odb|odc|odf|odg|odp|ods|odt|ogg|pdf|png|pot|pps|ppt|pptx|ra|ram|svg|svgz|swf|tar|tif|tiff|ttf|ttc|wav|
wma|wri|xla|xls|xlsx|xlt|xlw|zip)$) {
    return 404;
}
# END W3TC Skip 404 error handling by WordPress for static files

##Special thank to rkruk https://github.com/rkruk/Ubuntu-Nginx-PHP5-fpm-Varnish/blob/master/var/www/nginx.conf
« Last Edit: มิถุนายน 17, 2016, 07:30:25 PM by golfreeze »

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2141
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
Re: Ubuntu-Nginx-PHP5-fpm-Varnish
« Reply #1 on: มิถุนายน 17, 2016, 07:43:24 PM »
ปกติถ้าเราลงตัว nginx ให้ทำงานเป็น web server แทน apache ใน ubuntu แล้ว
ตัว nginx สามารถใช้งานกับ module fast_cgi ที่จะทำหน้าที่เป็น caching ในตัวได้ครับ
แต่ต้องเปิดการใช้งานเพิ่ม โดยเริ่มแรกเข้าไป เพิ่มที่

#cd /etc/nginx/
#mkdir cache
#cd site-enable
#vi packetlove.com

ทำการเพิ่ม 2 บันทัดนี้ลงไปเพื่อเปิดใช้งาน fastcgi_cache ในบันทัดบนสุดของ configure และไม่ให้อยู่ใน server {}

fastcgi_cache_path /etc/nginx/cache levels=1:2 keys_zone=MYAPP:100m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_cache_use_stale error timeout invalid_header http_500;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;

หลังจากนั้นเพิ่ม configure นี้เพื่อให้ PHP ส่งค่าไปยัง  php5-fpm ลงในส่วนของ "location ~ .php$ { }"

fastcgi_cache MYAPP;
fastcgi_cache_valid 200 60m;

check configure ว่าถูกต้องหรือไม่
#service nginx configtest

ถ้า OK แล้วก็ restart nginx 1 รอบครับ
service nginx reload

หน้าตาจะเป็นประมาณนี้ครับ
fastcgi_cache_path /etc/nginx/cache levels=1:2 keys_zone=MYAPP:100m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_cache_use_stale error timeout invalid_header http_500;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;

server {
    listen   80;

    root /usr/share/nginx/html;
    index index.php index.html index.htm;

    server_name example.com;

    location / {
        try_files $uri $uri/ /index.html;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_cache MYAPP;
        fastcgi_cache_valid 200 60m;
    }
}

หลังจากนั้นตรวจสอบว่า cache ถูกสร้างไว้ที่ /etc/nginx/cache หรือเปล่า ก็จะเห็นว่ามี folder ที่มี character 1 ตัว ถูกสร้างขึ้น
#cd /etc/nginx/cache
#ls -lahR | more
e
 1e
เนื่องจากกำหนด levels=1:2

#reference
https://www.digitalocean.com/community/tutorials/how-to-setup-fastcgi-caching-with-nginx-on-your-vps
https://easyengine.io/wordpress-nginx/tutorials/single-site/fastcgi-cache-with-purging/
http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html#fastcgi_cache_path