Welcome! Log In Create A New Profile

Advanced

Setting up nginx with memcaching

Posted by akendrick451 
Setting up nginx with memcaching
October 27, 2009 06:54PM
Hi Guys,

I am trying to set up nginx with memcaching, and have been trying for about 2 weeks. I think there is an issue with my conf file, particularly with what to do if the page is not in the cache. I would appreciate any help. I have read through the documentation, and a number of help articles, but can't seem to get it right.

The nginx server works perfectly without memcaching.

I am placing this one a fedora 10 server. The version of nginx is 0.7.62

Here is the relevant part of my nginx.conf

========================================
upstream backend {
server 10.0.21.68:80;
}


# The default server
server {
listen 80;
server_name nginxserver;


location / {
root /usr/share/nginx/html;
index index.html index.htm index.php;
#add mem cache
set $memcached_key $uri;
memcached_pass 127.0.0.1:11211;
default_type text/html;
error_page 404 @fallback;
}


location @fallback {
proxy_pass http://nginxserver;
}

...
=======================================================
Any help would be greatly appreciated.

Kind Regards,
Anthony
Sydney, Australia
Attachments:
open | download - nginx1.conf (3 KB)
Re: Setting up nginx with memcaching
November 01, 2009 09:34PM
Hi there,

I have just been tinkering with this and so far I have the following working:

Request comes in, GET on memcache, if that fails then it try fcgi php. Then I have php code in some pages to set the appropriate key. Now that works apart from the content type being incorrect on CSS/JS/etc because I haven't forced them to try the file direct yet. (tips anyone?)

[code]
server {
listen 82;
server_name wordpress.f-box.org;
location / {
root /public/test/wordpress;
autoindex off;
# this will end up as wordpress:/index.php for example.
set $memcache_key wordpress:$uri;
memcached_pass 127.0.0.1:11211;
# default_type text/html;

error_page 404 405 500 502 505 = @fallback_wordpress_mc;
}
location @fallback_wordpress_mc {
internal;
root /public/test/wordpress;
autoindex off;
fastcgi_pass 127.0.0.1:2081;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /public/test/wordpress$fastcgi_script_name;
include fastcgi_params;
}
error_page 404 = /404.html;
}
[/code]

Works as a normal /fallback/wordpress/mc location as well.

Cheers,

DanB UK.
PS. The code I am using as header and footer for the pages is as follows.
memcache_header.php
[code]<?php

if($_SERVER['REQUEST_METHOD'] == "GET") {
$memcache_host = "127.0.0.1";
$memcache_port = 11211;
$memcache_timeout = 60;
$mc = new Memcache();
$mc->connect($memcache_host, $memcache_port);
ob_start();
}

?>[/code]

memcache_footer.php
[code]<?php

if($_SERVER['REQUEST_METHOD'] == "GET") {
$mc_data = ob_get_contents();
ob_end_flush();
$rtv = $mc->set("wordpress:" . $_SERVER['REQUEST_URI'], $mc_data, FALSE, $memcache_timeout);
$mc->close();
}

?>[/code]



Edited 1 time(s). Last edit at 11/01/2009 09:37PM by DanBUK.
Re: Setting up nginx with memcaching
November 01, 2009 09:36PM
Sorry I mean:

memcache_footer.php
[code]<?php

if($_SERVER['REQUEST_METHOD'] == "GET") {
$mc_data = ob_get_contents();
ob_end_flush();
$rtv = $mc->set("wordpress:" . $_SERVER['REQUEST_URI'], $mc_data, FALSE, $memcache_timeout);
$mc->close();
}

?>[/code]
Re: Setting up nginx with memcaching
November 03, 2009 08:44PM
Thanks dan, I have nearly got it all working now thanks to your example!

Will post if I have any more issues.

Thanks again,
Anthony
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 75
Record Number of Users: 6 on February 13, 2018
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready