nginx_apache cache not working

knoll

Verified User
Joined
Sep 26, 2005
Messages
140
Location
Belgium
Hey,
i'm 3 days trying to get nginx cache working
i installed nginx_apache
apache is caching with opcache

but nginx cache isn't working

[root@gsi2 ~]# curl --head http://www.mydomain/
HTTP/1.1 200 OK
Server: nginx
Date: Tue, 23 Oct 2018 09:56:23 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
Vary: Accept-Encoding
X-Powered-By: PHP/7.2.11
Link: <http://www.mydomain/wp-json/>; rel="https://api.w.org/"
Cache-Control: max-age=0, no-cache
Vary: Accept-Encoding,User-Agent

i tryed almost every howto online not working
i want this because i want later to change to https but than the site drops speed by 300%

[root@gsi2 ~]# cat /usr/local/directadmin/conf/directadmin.conf |grep nginx_proxy_buffering
nginx_proxy_buffering=1


this is my nginx.conf:


#user nginx;

worker_processes auto;
pid /var/run/nginx.pid;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

events {
include /etc/nginx/nginx-events.conf;
}


http {
include /etc/nginx/mime.types;

# For user configurations not maintained by DirectAdmin. Empty by default.
include /etc/nginx/nginx-includes.conf;

# Supplemental configuration
include /etc/nginx/nginx-modsecurity-enable.conf;
include /etc/nginx/nginx-defaults.conf;
include /etc/nginx/nginx-gzip.conf;
include /etc/nginx/nginx-proxy.conf;
include /etc/nginx/directadmin-ips.conf;
include /etc/nginx/directadmin-settings.conf;
include /etc/nginx/nginx-vhosts.conf;
include /etc/nginx/directadmin-vhosts.conf;
include /etc/nginx/sites-enabled/*;
fastcgi_cache_path /var/cache/nginx2 levels=1:2 keys_zone=microcache:5m max_size=1000m;
log_format cache '$remote_addr - $remote_user [$time_local] "$request" '
'$status $upstream_cache_status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

# Enable session tickets
ssl_session_tickets on;

}

------------------
nginx-proxy.conf:

[root@gsi2 nginx]# cat nginx-proxy.conf
proxy_connect_timeout 150s;
proxy_send_timeout 150s;
proxy_read_timeout 1800s;
proxy_buffer_size 128k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
proxy_cache_key $host$uri$is_args$args;
proxy_cache_valid 200 10m;

-----------------------------
i added sites-available and sites-enabled (dirs not created by DA)

this is mydomein.tld.conf in directory sites-available and sites-enabled symlinked to there:

#
# your website
#
server {
listen 80;
server_name mydomain.tld www.mydomain.tld;
access_log off;
error_log /var/log/httpd/mydomain.tld-error_log;
root /home/historics/public_html/;
location / {
index index.php index.html index.htm;
}
if (!-e $request_filename) {
rewrite ^(.+)$ /index.php?q=$1 last;
}
location ~ \.php$ {
# Setup var defaults
set $no_cache "";
# If non GET/HEAD, don't cache & mark user as uncacheable for 1 second via cookie
if ($request_method !~ ^(GET|HEAD)$) {
set $no_cache "1";
}
# Drop no cache cookie if need be
# (for some reason, add_header fails if included in prior if-block)
if ($no_cache = "1") {
add_header Set-Cookie "_mcnc=1; Max-Age=2; Path=/";
add_header X-Microcachable "0";
}
# Bypass cache if no-cache cookie is set
if ($http_cookie ~* "_mcnc") {
set $no_cache "1";
}
# Bypass cache if flag is set
fastcgi_no_cache $no_cache;
fastcgi_cache_bypass $no_cache;
fastcgi_cache microcache;
fastcgi_cache_key $server_name|$request_uri;
fastcgi_cache_valid 404 30m;
fastcgi_cache_valid 200 10s;
fastcgi_max_temp_file_size 1M;
fastcgi_cache_use_stale updating;
fastcgi_pass localhost:9000;
fastcgi_pass_header Set-Cookie;
fastcgi_pass_header Cookie;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
#fastcgi_intercept_errors on;
include fastcgi_params;
}
}

----------------------------------------------------

i even tryed this guide:
https://guides.wp-bullet.com/how-to-configure-nginx-reverse-proxy-wordpress-cache-apache/

with this reverse thing in sites-enabled:

# WP Bullet nginx proxy cache
# Author Mike from https://guides.wp-bullet.com
#fix 504 gateway timeouts, can go in nginx.conf
send_timeout 600;
#set the location of the cached files, zone, name, size (1000 MB) and how long to cache for 600 minutes
proxy_cache_path /var/run/proxy_cache levels=1:2 keys_zone=WORDPRESS-PROXY:10m max_size=1000m inactive=600m use_temp_path=off;
#prevent header too large errors
#httpoxy exploit protection
proxy_set_header Proxy "";
# add forwarded for header
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

server {
listen 80 default;
access_log /var/log/nginx/proxy-access.log;
error_log /var/log/nginx/proxy-error.log;
# show cache status and any skip cache reason
add_header WP-Bullet-Proxy-Cache $upstream_cache_status;
add_header Cache-BYPASS-Reason $skip_reason;

# define nginx variables
set $do_not_cache 0;
set $skip_reason "";
set $bypass 0;

# security for bypass so localhost can empty cache
if ($remote_addr ~ "^(127.0.0.1|185.244.39.54)$") {
set $bypass $http_secret_header;
}

# skip caching WordPress cookies
if ($http_cookie ~* "comment_author_|wordpress_(?!test_cookie)|wp-postpass_" ) {
set $do_not_cache 1;
set $skip_reason Cookie;
}

# Don't cache URIs containing the following segments
if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|sitemap(_index)?.xml") {
set $skip_cache 1;
set $skip_reason URI;
}

location / {
proxy_set_header Host $host;
# may need to comment out proxy_redirect if get login redirect loop
proxy_redirect off;
proxy_cache WORDPRESS-PROXY;
proxy_cache_revalidate on;
proxy_ignore_headers Expires Cache-Control;
proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;
proxy_cache_bypass $bypass $do_not_cache;
proxy_no_cache $do_not_cache;
proxy_cache_valid 200 301 302 500m;
proxy_cache_valid 404 1m;
#can rename PURGE to whatever you want, should restrict it to backend server requests for security
# pass requests onto your PHP backend
proxy_pass http://127.0.0.1:8080;
}

# allows purging via special URL
location ~ /purge(/.*) {
allow 127.0.0.1;
allow 185.244.39.54;
deny all;
}
}
------------- (btw i had to remove a few duplicated things to get nginx worked) ------

but nginx cache is not working...

I hope someone can help me i don't want to try 7days 5 hours a day :)

many thanks on advance !!!!
greets alex
 
Hello,

First of all why do you need the debian-style folder here? It's directadmin and it runs its own directory and configs structure. And your configs might conflict with Directadmin and/or override.

Secondly, your configs are a way difficult for reading, please use formatting, and put all the configuration lines between CODE /CODE tags...
 
im sorry i do my best but coding and stuff isn't my thing ;)
possible to give a good example for me for nginx.conf and domainname.conf.tld?
something i can do something with it i agree my things are messed thats why i don't understand what to do i guess :)
to bad there isn't a howto that is complete basic things that says where to put what.
Sometimes you see info but don't know where to put
I hope you can do something for me many thanks on advance
ps not that i'm lazy i just don't know what to do

and i guess many people can learn something about it here i think not everyone gets its done
grz alex
 
i don't think i will go further here...
i guess many people know but some don't know so any help wanted please thanks on advance ! grz alex
 
Hey, I do posting on the forums when I have time for it, and I don't work for Directadmin. If you have an emergencry requests you can contact me privately through my site or open a ticket with Directadmin support.

The best way to clean nginx configs is to remove /etc/nginx/ and re-install nginx from custombuild again.

Code:
cd /usr/local/directadmin/custombuild
./build update
./build nginx_apache

I don't have a guide on how to enable proxy_caching in nginx+apache scheme on Diretadmin servers. But I do it for my customers.

I add proxy_cache instructions under location / in nginx templates copied to /usr/local/directadmin/data/templates/custom/ from /usr/local/directadmin/data/templates/, their names start from nginx_server.

Common instructions such as proxy_cache_path can be added into /etc/nginx/nginx-includes.conf

Sorry, I don't have time now for a more detailed guide.
 
Hey,
oh ok i didn't know you where working for directadmin ;).

Ok i will try to start over thanks for the guide.
if you set this up for customer how much it costs ? just the cache system and for example 1 site ? so i can do the others :)
i understand your time problem its almost the same for me.
thanks for everything

Greets
alex
Hey, I do posting on the forums when I have time for it, and I don't work for Directadmin. If you have an emergencry requests you can contact me privately through my site or open a ticket with Directadmin support.

The best way to clean nginx configs is to remove /etc/nginx/ and re-install nginx from custombuild again.

Code:
cd /usr/local/directadmin/custombuild
./build update
./build nginx_apache

I don't have a guide on how to enable proxy_caching in nginx+apache scheme on Diretadmin servers. But I do it for my customers.

I add proxy_cache instructions under location / in nginx templates copied to /usr/local/directadmin/data/templates/custom/ from /usr/local/directadmin/data/templates/, their names start from nginx_server.

Common instructions such as proxy_cache_path can be added into /etc/nginx/nginx-includes.conf

Sorry, I don't have time now for a more detailed guide.
 
A while ago one good man sent me a link to this article: https://www.nginx.com/blog/benefits-of-microcaching-nginx/ so I've started with it. Actually NGINX team has many useful articles there including this one: https://www.nginx.com/blog/cache-placement-strategies-nginx-plus/

When using directadmin templates /usr/local/directadmin/data/templates/custom/ from /usr/local/directadmin/data/templates/ the changes will effect all existing sites.

And I usually work on a templates level. Not everything can be done in a custom HTTPd section of Directadmin.
 
ok thanks for the big efford so in /etc/nginx/nginx-includes.conf i put:
proxy_cache_path /var/cache/nginx keys_zone=cache:10m levels=1:2 inactive=600s max_size=100m;

and i copy nginx_server.conf to -> /usr/local/directadmin/data/templates/custom/
than edit /usr/local/directadmin/data/templates/custom/nginx_server.conf
and do something like :

[root@gsi2 custom]# cat nginx_server.conf
|CUSTOM1|
|?DOCROOT=`HOME`/domains/`DOMAIN`/public_html|
|?REALDOCROOT=`HOME`/domains/`DOMAIN`/public_html|
|?OPEN_BASEDIR_PATH=`HOME`/:/tmp:/var/tmp:/usr/local/lib/php/|
server
##add by alex
proxy_cache one;
proxy_cache_lock on;
proxy_cache_valid 200 1s;
proxy_cache_use_stale updating;
##end add alex
{
|CUSTOM|

listen |IP|:|PORT_80|;
|MULTI_IP|

server_name |DOMAIN| www.|DOMAIN| |SERVER_ALIASES|;

access_log /var/log/nginx/domains/|DOMAIN|.log;
access_log /var/log/nginx/domains/|DOMAIN|.bytes bytes;
error_log /var/log/nginx/domains/|DOMAIN|.error.log;


root |DOCROOT|;

index index.php index.html index.htm;

|NGINX_PHP_CONF|

|*if HAVE_NGINX_PROXY="1"|
location /
{
|CUSTOM2|
|LOCATION_INSERT|
# access_log off;
proxy_buffering |PROXY_BUFFERING|;
proxy_pass http://|PROXY_IP|:|PORT_8080|;
proxy_set_header X-Client-IP $remote_addr;
proxy_set_header X-Accel-Internal /nginx_static_files;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_hide_header Upgrade;
}
location /nginx_static_files/
{
# access_log /var/log/nginx/access_log_proxy;
alias |DOCROOT|/;
internal;
}
|*else|
|NGINX_REDIRECTS|
|PROTECTED_DIRECTORIES|
|EXTRA_LOCATIONS|
|*endif|

|CUSTOM3|

include /etc/nginx/webapps.conf;

|CUSTOM4|
}
 
why i add between server is because your sended manual said this:

Together with the caching directives we added previously, this give us the following server configuration:
server {
proxy_cache one;
proxy_cache_lock on;
proxy_cache_valid 200 1s;
proxy_cache_use_stale updating;
# ...
}
 
Yes, it should work, but as I wrote earlier I add proxy_cache instructions under location /

In my case no static files get cached, you should decide whether or not you want the same.

And you might want to store cache for more than 1 second.
 
hm ok so on top is let static also cached i think about it if i see trouble i can add it still under location /

ok will raise it to a higher level ;-)
but don't i don't have to specify what sites to cache or do they all cache ?
don't i need this things?:
# ...
location / {
proxy_cache my_cache;
proxy_pass http://my_upstream;
 
ok thanks for the big efford so in /etc/nginx/nginx-includes.conf i put:
proxy_cache_path /var/cache/nginx keys_zone=cache:10m levels=1:2 inactive=600s max_size=100m;

and i copy nginx_server.conf to -> /usr/local/directadmin/data/templates/custom/
than edit /usr/local/directadmin/data/templates/custom/nginx_server.conf
and do something like :

[root@gsi2 custom]# cat nginx_server.conf
|CUSTOM1|
|?DOCROOT=`HOME`/domains/`DOMAIN`/public_html|
|?REALDOCROOT=`HOME`/domains/`DOMAIN`/public_html|
|?OPEN_BASEDIR_PATH=`HOME`/:/tmp:/var/tmp:/usr/local/lib/php/|
server
##add by alex
proxy_cache one;
proxy_cache_lock on;
proxy_cache_valid 200 1s;
proxy_cache_use_stale updating;
##end add alex
{
|CUSTOM|

listen |IP|:|PORT_80|;
|MULTI_IP|

server_name |DOMAIN| www.|DOMAIN| |SERVER_ALIASES|;

access_log /var/log/nginx/domains/|DOMAIN|.log;
access_log /var/log/nginx/domains/|DOMAIN|.bytes bytes;
error_log /var/log/nginx/domains/|DOMAIN|.error.log;


root |DOCROOT|;

index index.php index.html index.htm;

|NGINX_PHP_CONF|

|*if HAVE_NGINX_PROXY="1"|
location /
{
|CUSTOM2|
|LOCATION_INSERT|
# access_log off;
proxy_buffering |PROXY_BUFFERING|;
proxy_pass http://|PROXY_IP|:|PORT_8080|;
proxy_set_header X-Client-IP $remote_addr;
proxy_set_header X-Accel-Internal /nginx_static_files;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_hide_header Upgrade;
}
location /nginx_static_files/
{
# access_log /var/log/nginx/access_log_proxy;
alias |DOCROOT|/;
internal;
}
|*else|
|NGINX_REDIRECTS|
|PROTECTED_DIRECTORIES|
|EXTRA_LOCATIONS|
|*endif|

|CUSTOM3|

include /etc/nginx/webapps.conf;

|CUSTOM4|
}


with this in my system i runned:
ab -c 10 -t 30 -n 100000 -k http://www.opelhistorics.be/
no files are created in /var/cache/nginx/
its totally empty while doing it and no speed upgrade yet :)

no mather you many times i try it looks not caching anything i think:
[root@gsi2 ~]# curl --head http://www.opelhistorics.be/
HTTP/1.1 200 OK
Server: nginx
Date: Thu, 25 Oct 2018 16:39:19 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
Vary: Accept-Encoding
X-Powered-By: PHP/7.2.11
Link: <http://www.opelhistorics.be/wp-json/>; rel="https://api.w.org/"
Cache-Control: max-age=0, no-cache
Vary: Accept-Encoding,User-Agent

[root@gsi2 ~]# curl --head http://www.opelhistorics.be/
HTTP/1.1 200 OK
Server: nginx
Date: Thu, 25 Oct 2018 16:39:20 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
Vary: Accept-Encoding
X-Powered-By: PHP/7.2.11
Link: <http://www.opelhistorics.be/wp-json/>; rel="https://api.w.org/"
Cache-Control: max-age=0, no-cache
Vary: Accept-Encoding,User-Agent
 
maybe something interesting while doing the ab i see this in top:

top - 18:42:49 up 3 days, 6:38, 2 users, load average: 3.98, 1.54, 0.86
Tasks: 281 total, 8 running, 273 sleeping, 0 stopped, 0 zombie
%Cpu(s): 72.6 us, 14.0 sy, 0.0 ni, 13.2 id, 0.0 wa, 0.0 hi, 0.3 si, 0.0 st
KiB Mem : 32600832 total, 5814984 free, 2320692 used, 24465156 buff/cache
KiB Swap: 16383996 total, 16371964 free, 12032 used. 29309836 avail Mem

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
13478 apache 20 0 5805504 70336 14980 S 317.5 0.2 1:01.22 httpd
13693 histori+ 20 0 316944 26128 12512 S 57.9 0.1 0:08.37 php-cgi72
13691 histori+ 20 0 316548 25768 12512 S 52.0 0.1 0:07.59 php-cgi72
13695 histori+ 20 0 316680 25900 12512 R 52.0 0.1 0:07.86 php-cgi72
13688 histori+ 20 0 316680 25952 12512 R 45.0 0.1 0:07.95 php-cgi72
13692 histori+ 20 0 316548 25792 12512 R 45.0 0.1 0:07.61 php-cgi72
13689 histori+ 20 0 316548 25760 12512 R 42.4 0.1 0:07.48 php-cgi72
13690 histori+ 20 0 316548 25836 12512 R 41.1 0.1 0:07.63 php-cgi72
13682 histori+ 20 0 316548 25728 12512 R 32.8 0.1 0:07.08 php-cgi72
12870 nginx 20 0 101636 23572 1196 S 2.6 0.1 0:01.41 nginx
12869 nginx 20 0 101636 23600 1200 S 2.3 0.1 0:02.14 nginx
13681 root 20 0 91452 3524 2528 S 1.3 0.0 0:00.25 ab
12867 nginx 20 0 101636 23476 1200 S 1.0 0.1 0:00.33 nginx
12868 nginx 20 0 101636 23536 1196 S 0.7 0.1 0:00.90 nginx

maybe you see something here :)
 
i think cache 'works' it creates dirs and files in /var/cache/nginx but it looks like containing only smail files like:
[root@gsi2 71]# cat c639f989d02d88ade79c767ae15c2719
▒_▒/▒EN▒▒▒▒▒Z▒▒[▒▒▒▒mAccept-Encoding,User-Agent▒Fʖ
}
KEY: http://myip:8080/?wordfence_lh=1&hid=somekey78798927922
HTTP/1.1 200 OK
Date: Thu, 25 Oct 2018 17:28:57 GMT
Server: Apache/2
X-Powered-By: PHP/7.2.11
Link: <http://www.opelhistorics.be/wp-json/>; rel="https://api.w.org/"
Connection: close
X-Robots-Tag: noindex
Vary: Accept-Encoding,User-Agent
Upgrade: h2,h2c
Connection: Upgrade
Content-Length: 0
Cache-Control: max-age=172800, s-maxage=10
Expires: Sat, 27 Oct 2018 17:28:57 GMT
Content-Type: text/javascript;charset=UTF-8

[root@gsi2 71]#

i don't find any php files or images etc maybe i should do something more? :)
with this at the moment here my speed went from:

Document Path: /
Document Length: 65141 bytes

Concurrency Level: 10
Time taken for tests: 30.001 seconds
Complete requests: 8595
Failed requests: 0
Keep-Alive requests: 8513
Total transferred: 562525150 bytes
HTML transferred: 559886895 bytes
Requests per second: 286.49 [#/sec] (mean)
Time per request: 34.906 [ms] (mean)
Time per request: 3.491 [ms] (mean, across all concurrent requests)
Transfer rate: 18310.55 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.0 0 1
Processing: 14 35 82.5 28 2052
Waiting: 14 35 82.5 28 2052
Total: 14 35 82.5 28 2052

Percentage of the requests served within a certain time (ms)
50% 28
66% 30
75% 31
80% 32
90% 36
95% 39
98% 44
99% 50
100% 2052 (longest request)
[root@gsi2 ~]#

to ->

Server Port: 80

Document Path: /
Document Length: 65141 bytes

Concurrency Level: 10
Time taken for tests: 30.013 seconds
Complete requests: 1915
Failed requests: 0
Keep-Alive requests: 1899
Total transferred: 125332840 bytes
HTML transferred: 124745015 bytes
Requests per second: 63.81 [#/sec] (mean)
Time per request: 156.724 [ms] (mean)
Time per request: 15.672 [ms] (mean, across all concurrent requests)
Transfer rate: 4078.13 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.0 0 0
Processing: 14 145 802.1 15 7027
Waiting: 14 145 802.1 15 7027
Total: 14 145 802.1 15 7028

Percentage of the requests served within a certain time (ms)
50% 15
66% 15
75% 15
80% 15
90% 16
95% 18
98% 5016
99% 5020
100% 7028 (longest request)

so @ this moment it looks slower but i think i have to do some last thing ;-) many thanks for the help i'm almost there i think
 
Back
Top