Nur Hamim
Nur Hamim Anak desa yang gemar berkomunitas, suka menulis dan mencari hal baru seputar Unix/Linux dan Cloud. Saat ini sedang menempuh pendidikan S1 TI di Unindra dan kebetulan bekerja di PT Biznet GIO Nusantara

Implementasi Cache Proxy Nginx pada WordPress di CentOS 8


Implementasi Cache Proxy Nginx pada WordPress di CentOS 8

Pada tutorial kali ini kami akan memberikan cara bagaimana mempercepat website WordPress Anda dengan menggunakan proxy cache Nginx.

Untuk mengikuti tutorial ini pastikan Anda sudah instalasi WordPress disini kami juga sudah memasang SSL untuk WordPress atau Anda dapat mengikuti referensi berikut:

Pertama silakan instalasi plugin di WordPress Anda pada menu Plugins >> Add New

Install dan aktivasi plugin Nginx Helper

Klik setting pada plugin Nginx Helper

Konfigurasi plugin seperti berikut

Selanjutnya membuat file caching di nginx

1
2
[root@tutorial ~]#
[root@tutorial ~]# vim /etc/nginx/conf.d/caching.conf

isi file caching seperti berikut

1
2
3
# Caching
fastcgi_cache_path /var/run/nginx-cache levels=1:2 keys_zone=WORDPRESS:100m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";

Selanjutnya mengubah konfigurasi server block WordPress

1
2
[root@tutorial ~]#
[root@tutorial ~]# vim /etc/nginx/conf.d/wordpress.conf

Berikut full konfigurasi server block isi dari konfigurasi ini sudah termasuk tuning dari segi konfigurasi nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
server {
        listen 80;
        listen 443 ssl http2;
        server_name wordpress.nurhamim.my.id;
        root /usr/share/nginx/wordpress;

        index index.php index.html index.htm;

        ssl_certificate /etc/letsencrypt/live/wordpress.nurhamim.my.id/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/wordpress.nurhamim.my.id/privkey.pem;
        ssl_trusted_certificate /etc/letsencrypt/live/wordpress.nurhamim.my.id/chain.pem;
        include snippets/ssl.conf;
        include snippets/letsencrypt.conf;

        gzip on;
        gzip_comp_level 5;
        gzip_min_length 256;
        gzip_proxied any;
        gzip_vary on;
        gzip_types
                application/atom+xml
                application/javascript
                application/json
                application/ld+json
                application/manifest+json
                application/rss+xml
                application/vnd.geo+json
                application/vnd.ms-fontobject
                application/x-font-ttf
                application/x-web-app-manifest+json
                application/xhtml+xml
                application/xml
                font/opentype
                image/bmp
                image/svg+xml
                image/x-icon
                text/cache-manifest
                text/css
                text/plain
                text/vcard
                text/vnd.rim.location.xloc
                text/vtt
                text/x-component
                text/x-cross-domain-policy;


        location / {
                try_files $uri $uri/ /index.php?$query_string;
        }

        # Restrictions
        # Disable logging for favicon and robots.txt
        location = /favicon.ico {
                log_not_found off;
                access_log off;
        }

        location = /robots.txt {
                allow all;
                log_not_found off;
                access_log off;
                try_files $uri /index.php?$args;
        }

        # Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
        # Keep logging the requests to parse later (or to pass to firewall utilities such as fail2ban)
        location ~ /\. {
                deny all;
        }

        # Deny access to any files with a .php extension in the uploads directory
        # Works in sub-directory installs and also in multisite network
        # Keep logging the requests to parse later (or to pass to firewall utilities such as fail2ban)
        location ~* /(?:uploads|files)/.*\.php$ {
                deny all;
        }
        # End Restrictions

        # Caching
        set $skip_cache 0;

        # POST requests and urls with a query string should always go to PHP
        if ($request_method = POST) {
        set $no_cache 1;
        }
        if ($query_string != "") {
        set $skip_cache 1;
        }

        # Don't cache uris containing the following segments
        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 $skip_cache 1;
        }

        # Don't use the cache for logged in users or recent commenters
        if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
        set $skip_cache 1;
        }
        #end Caching

        ## WordPress single site rules.
        #location / {
        # try_files $uri $uri/ /index.php?$args;
        #}

        # Add trailing slash to */wp-admin requests.
        rewrite /wp-admin$ $scheme://$host$uri/ permanent;

        # Directives to send expires headers and turn off 404 error logging.
        location ~* ^.+\.(eot|otf|woff|woff2|ttf|rss|atom|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
                access_log off; log_not_found off; expires max;
        }

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

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

        # HTML send expires headers.
        location ~* \.(html)$ {
                expires 7d;
                access_log off;
                add_header Cache-Control "public";
        }

        # Browser caching of static assets.
        location ~* \.(jpg|jpeg|png|gif|ico|css|js|pdf)$ {
                expires 7d;
                add_header Cache-Control "public, no-transform";
        }

        location ~ \.php {
                include fastcgi.conf;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/run/php-fpm/www.sock;
                # enable cache
                add_header X-WP-Cache $upstream_cache_status;
                fastcgi_cache_bypass $skip_cache;
                fastcgi_no_cache $skip_cache;
                fastcgi_cache WORDPRESS;
                fastcgi_cache_valid 200 60m;
        }

        location ~ /\.ht {
                deny all;
        }
}

Pastikan tidak ada konfigurasi yang salah di sisi server block nginx

1
2
3
4
5
[root@tutorial ~]#
[root@tutorial ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@tutorial ~]#

Selanjutnya reload Nginx

1
2
[root@tutorial ~]# nginx -s reload
[root@tutorial ~]#

Jika sudah silakan di curl jika hasilnya seperti berikut menandakan cache WordPress sudah berjalan dengan normal

Selamat mencoba 😁

comments powered by Disqus