Help needed for proxy_cache

vlijmenlive

Verified User
Joined
Nov 13, 2013
Messages
76
Location
Belgium
Hi Guys,

I'm hosting a website for some friends on my server and because they are getting more and more visitors by the day I started looking into nginx proxy caching. I think this solution would be easier then going for Nginx-Varnish-Nginx-Apache due to SSL. Especially because I don't have any experience with Varnish but a little bit with Nginx I thought this would be a good start.

But I'm getting stuck at the start already. I tried to follow multiple tutorials I found online but I doesn't seem to get it working. I have set nginx_proxy_buffering=1 and tried to set the proxy_cache location and settings but the result is 0 caching (config rewrite done).

Is there anyone here that could point me in the right direction (a tutorial) that has been written to work with default Custombuild2.0 settings and Nginx reverse prox. PHPFPM? Especially because the correct files and on what line I have to put what information isn't really clear to me.

Hope someone can help me out and give my server a bit more air during peak hours.

Thanks in advance for you feedback.

Regards,
Niels
 
Hi Alex,

Thank you for your feedback, it pointed me in the right direction (I think). While I first started with proxy_cache I've read a bit further and understood that using fastcgi_cache would be a better option. Wich one is better in your opinion? Or maybe both together (no idea if that is a good thing).

Using proxy_cache I managed to get a HIT/MISS/BYPASS result in the headers, not sure if that really proofs it works but it's a start. Now I changed to fastcgi_cache I don't get that any more. Let me show you my config files, that probably helps.
/etc/nginx/nginx-includes.conf
fastcgi_cache_path /var/nginx/cache levels=1:2 keys_zone=my_cache:512m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
add_header X-Fastcgi-Cache $upstream_cache_status;
nginx_server_secure / nginx_server_secure_sub / nginx_server / nginx_server_sub (.conf)
|CUSTOM1|
|?DOCROOT=`HOME`/domains/`DOMAIN`/private_html|
|?REALDOCROOT=`HOME`/domains/`DOMAIN`/private_html|
|?OPEN_BASEDIR_PATH=`HOME`/:/tmp:/var/tmp:/usr/local/lib/php/|

server
{
|CUSTOM|

listen |IP|:|PORT_443| ssl http2;

|$/usr/local/bin/php
<?php
$data = <<<END
|MULTI_IP|
END;
$data = str_replace("ssl;", "ssl http2;", $data);
echo $data;
?>
DONE|

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;

ssl on;
ssl_certificate |CERT|;
ssl_certificate_key |KEY|;

|NGINX_PHP_CONF|

|*if HAVE_NGINX_PROXY="1"|
location /
{
|CUSTOM2|

|LOCATION_INSERT|
include /etc/nginx/aphotrax-cache.conf;
# access_log off;
proxy_buffering |PROXY_BUFFERING|;
proxy_pass https://|IP|:|PORT_8081|;
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;
}
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.ssl.conf;

|CUSTOM4|
}
/etc/nginx/aphotrax-cache.conf
fastcgi_cache my_cache;
fastcgi_cache_valid any 1m;
fastcgi_cache_lock on;
fastcgi_cache_lock_age 5s;
fastcgi_cache_lock_timeout 5s;
fastcgi_max_temp_file_size 5m;
set $skip_cache 0;

# POST requests and urls with a query string should always go to PHP
if ($request_method = POST) {
set $skip_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-.*.php|/feed/|index.php|sitemap(_index)?.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;
}
fastcgi_no_cache $cookie_PHPSESSID $skip_cache;
fastcgi_cache_bypass $cookie_PHPSESSID $skip_cache;
 
The ngx_http_fastcgi_module module allows passing requests to a FastCGI server. One of the main use-cases of FastCGI proxying within Nginx is for PHP processing. Why do you think it will work with proxying to Apache?
 
Let's keep it to a beginner mistake. :D

So if I understand it correctly using proxy cache would be the solution when using nginx reverse proxy with phpfpm? I switched back and now I get a hit result again. Unfortunately a bit lower traffic now to test the results but hopefully this will take the load off a bit at peak moments. Can I assume that if I get a hit header it really comes from the cache and the setup is correct?

Thanks for your help!
 
Hi Alex,

Thanks for the help, so I think I finally managed to optimize nginx a bit further. After I managed to get this working I started trying to let some static images get handled by nginx instead of apache and also that seems to work and lower the load. So more and more happy.

I'm looking forward to hit the 1.000 concurrent visitors again to see the difference. :)

Regards,
Niels
 
Back
Top