参考配置:
server { location / { set $memcached_key "$uri?$args"; memcached_pass host:11211; error_page 404 502 504 = @fallback; } location @fallback { proxy_pass http://backend; } }
实际应用的配置:
#user nobody;
worker_processes 4;
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
worker_rlimit_nofile 65536;
worker_cpu_affinity 0001 0010 0100 1000;
events {
worker_connections 4096;
use epoll;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
gzip on;
gzip_min_length 1000;
gzip_comp_level 2;
gzip_buffers 4 8k;
gzip_http_version 1.1;
gzip_types text/plain application/x-javascript text/css application/xml;
upstream mem_backend {
server 10.148.37.10:22110;
#server 10.148.15.91:22110;
ip_hash;
keepalive 2048;
}
server {
listen 80;
#server_name plugin.shopal.jp;
server_name localhost;
charset utf8;
#access_log logs/host.access.log main;
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
root html;
expires 30d;
}
#location ~ .*\.(js|css)$ {
# root html;
# expires 1d;
#}
location / {
root html;
index index.php;
}
location ~ \.php$ {
if ( $query_string !~* 'key=' ){
proxy_pass http://127.0.0.1:8000;
break;
}
set $memc_cmd 'get';
set $memc_key $arg_key;
memc_pass mem_backend;
default_type application/json;
error_page 404 500 502 503 504 = @cache404;
}
location @cache404 {
proxy_pass http://127.0.0.1:8000;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
}
location /status {
set $memc_cmd stats;
memc_pass mem_backend;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
error_page 404 /404.html;
location = /50x.html {
root html;
}
location = /404.html {
root html;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
server {
listen 8000;
server_name localhost;
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
include fastcgi.conf;
}
}
# HTTPS server
#
#server {
# listen 443;
# server_name localhost;
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_timeout 5m;
# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
把数据放到缓存服务器中:
if(empty($user_id)){
//$uri_str = $_SERVER["REQUEST_URI"];
$cache_key = @$_GET['key'];
if(!empty($cache_key)){
$memcache1 = new Memcache;
$memcache1->addServer('10.148.37.10', 22110);
$memcache1->add($cache_key,$result,false,3600*24);
$memcache2 = new Memcache;
$memcache2->addServer('10.148.15.91', 22110);
$memcache2->add($cache_key,$result,false,3600*24);
}
}
安装组建 http://nginx.org/en/docs/http/ngx_http_memcached_module.html