# Nginx memcached setting

**URL:** <https://www.ruby-forum.com/t/nginx-memcached-setting/199447>\
**Category:** NGINX\
**Created:** [November 21, 2010, 6:30pm UTC](https://www.ruby-forum.com/t/nginx-memcached-setting/199447 "2010-11-21T18:30:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![okkezSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/85f322/32.png) [@okkezSS](https://www.ruby-forum.com/u/okkezSS)\
**Post date:** [November 21, 2010, 6:30pm UTC](https://www.ruby-forum.com/t/nginx-memcached-setting/199447/1 "2010-11-21T18:30:51Z")

</div>

My server running Nginx and php-fpm for wordpress. Today i installed  
memcached but i dont know how to setting the memcached thing into conf  
file. Here is my wordpres conf file

```auto
server{
        listen 80;
        server_name www.mywebsite.com;
        access_log /var/log/nginx/mywpt.access_log;
        error_log /var/log/nginx/mywpt.error_log;

    root /home/mywebsite/public_html;

        location / {
          root /home/mywebsite/public_html/;
          index index.html index.htm index.php;

          # this serves static files that exist without running other
rewrite tests
          if (-f $request_filename) {
              expires 30d;
              break;
          }

          # this sends all non-existing file or directory requests to
index.php
          if (!-e $request_filename) {
              rewrite ^(.+)$ /index.php?q=$1 last;
          }
        }

        location ~ \.php$ {
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME
/home/mywebsite/public_html/$fastcgi_script_name;
            include fastcgi_params;
        }

}

```

where to put this code ?

server {  
location / {  
set $memcached\_key $uri;  
memcached\_pass name:11211;  
default\_type text/html;  
error\_page 404 @fallback;  
}

location @fallback {  
proxy\_pass backend;  
}  
}

Posted at Nginx Forum:

> **[Nginx memcached setting](https://forum.nginx.org/read.php?2,152247,152247#msg-152247)**
>
> My server running Nginx and php-fpm for wordpress. Today i installed memcached but i dont know how to setting the memcached thing into conf file. Here is my wordpres conf file \[code\] server{ listen 80; server\_name www.mywebsite.com; access\_l

---

<div class="post-metadata">

**Author:** ![sastro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5fc32e/32.png) [@sastro](https://www.ruby-forum.com/u/sastro)\
**Post date:** [November 22, 2010, 12:09pm UTC](https://www.ruby-forum.com/t/nginx-memcached-setting/199447/2 "2010-11-22T12:09:25Z")

</div>

Please someone help me

Posted at Nginx Forum:

> **[Re: Nginx memcached setting](https://forum.nginx.org/read.php?2,152247,152361#msg-152361)**
>
> My server running Nginx and php-fpm for wordpress. Today i installed memcached but i dont know how to setting the memcached thing into conf file. Here is my wordpres conf file \[code\] server{ listen 80; server\_name www.mywebsite.com; access\_l

---

<div class="post-metadata">

**Author:** ![sastro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5fc32e/32.png) [@sastro](https://www.ruby-forum.com/u/sastro)\
**Post date:** [November 22, 2010, 11:59pm UTC](https://www.ruby-forum.com/t/nginx-memcached-setting/199447/3 "2010-11-22T23:59:52Z")

</div>

On 11/21/10 12:29 PM, sastro wrote:

> ```
> root /home/mywebsite/public_html;
> }
> fastcgi_index index.php;
> 
> ```
> 
> where to put this code ?  
> proxy\_pass backend;  
> }  
> }

Nowhere. Use the WordPress memcached plugin at  
[Memcached Object Cache – WordPress plugin | WordPress.org](http://wordpress.org/extend/plugins/memcached/) and it will do the work  
for you. Do make certain that you have memcached installed (and running)  
as well as the PECL-memcache PHP extension. See [http://memcached.org/](http://memcached.org/)  
and [PECL :: Package :: memcache](http://pecl.php.net/package/memcache).

BTW, if you really want efficiency for your site, and since you’re  
asking about using memcached it seems that you are, you should rewrite  
the config to get rid of the if’s, and use locations and try\_files  
instead. See [GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/IfIsEvil).

A more efficient config might be (not tested so YMMV):

server{  
listen 80;  
server\_name [www.mywebsite.com](http://www.mywebsite.com);  
access\_log /var/log/nginx/mywpt.access\_log;  
error\_log /var/log/nginx/mywpt.error\_log;

```
root /home/mywebsite/public_html;

```

#will capture most static files and serve them. Add other types if you  
need to

```
     location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {
 expires 30d;

```

}

```
     location / {
       root /home/mywebsite/public_html/;
       index index.html index.htm index.php;

       # this will capture and serve any other static file types

```

missed above and send the rest to your fallback rewrite  
try\_files $uri $uri/ /index.php?q=$request\_uri;  
}

```
     location ~ \.php$ {
         fastcgi_pass 127.0.0.1:9000;
         fastcgi_index index.php;
         fastcgi_param SCRIPT_FILENAME

```

/home/mywebsite/public\_html/$fastcgi\_script\_name;  
include fastcgi\_params;  
}

}

> Posted at Nginx Forum:  
> [http://forum.nginx.org/read.php?2,152247,152247#msg-152247](http://forum.nginx.org/read.php?2,152247,152247#msg-152247)

–  
Jim O.

> > Why?  
> > Because it breaks the logical flow of conversation, plus makes  
> > messages unreadable.
> > 
> > > Top-Posting is evil.

---

<div class="post-metadata">

**Author:** ![sastro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5fc32e/32.png) [@sastro](https://www.ruby-forum.com/u/sastro)\
**Post date:** [November 25, 2010, 10:01am UTC](https://www.ruby-forum.com/t/nginx-memcached-setting/199447/4 "2010-11-25T10:01:49Z")

</div>

Please help me

Posted at Nginx Forum:

> **[Re: Nginx memcached setting](https://forum.nginx.org/read.php?2,152247,153191#msg-153191)**
>
> My server running Nginx and php-fpm for wordpress. Today i installed memcached but i dont know how to setting the memcached thing into conf file. Here is my wordpres conf file \[code\] server{ listen 80; server\_name www.mywebsite.com; access\_l

---

<div class="post-metadata">

**Author:** ![sastro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5fc32e/32.png) [@sastro](https://www.ruby-forum.com/u/sastro)\
**Post date:** [November 23, 2010, 9:54pm UTC](https://www.ruby-forum.com/t/nginx-memcached-setting/199447/5 "2010-11-23T21:54:34Z")

</div>

Thanks for replied.  
I’m not using memcached with php but stand alone. I want to using nginx  
and memcached + php-fpm and eaccelerator.  
i did tried this for my low traffic website and it works

```auto
server{
        listen 80;
        server_name www.lowtraffic.net;
        access_log /var/log/nginx/lowtraffic.net.access_log;
        error_log /var/log/nginx/lowtraffic.net.error_log;

    root /home/lowtraffic/public_html/;

  location / {
    set $memcached_key $uri;
    memcached_pass 127.0.0.1:11211;
    default_type text/html;
    error_page 404 405 = @fallback;
  }

  location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {
      expires 30d;
      access_log off;
    }
        location ~ \.php$ {
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME
/home/lowtraffic/public_html/$fastcgi_script_name;
            include fastcgi_params;
        }
    location @fallback {
    rewrite ^ /index.php?q=$uri last;
     }
}
```

But when i using with \>1000 visitors online on the same time, i got  
request time out.  
What’s wrong?

Posted at Nginx Forum:

> **[Re: Nginx memcached setting](https://forum.nginx.org/read.php?2,152247,152776#msg-152776)**
>
> My server running Nginx and php-fpm for wordpress. Today i installed memcached but i dont know how to setting the memcached thing into conf file. Here is my wordpres conf file \[code\] server{ listen 80; server\_name www.mywebsite.com; access\_l
