真正的障碍是克服浏览器中ajax请求的非端口80/443限制;即使使用Webdis解决方案,因为它通过defaul,t在端口7379上运行,并且如果从端口80运行,则会与您的Apache或Nginx进程冲突。
我的建议是使用nginx proxy_pass指向webdis过程。您可以将流量重定向到端口 80 并执行 ajax 请求,而不会出现令人讨厌的安全问题。
下面是一个示例NGINX配置,它似乎为我做了这个把戏。
upstream WebdisServerPool
{
server 127.0.0.1:7379; #webdis server1
server 192.168.1.1:7379; #webdis server 2
}
server {
listen 80; #
root /path/to/my/php/code/;
index index.php;
server_name yourServerName.com;
location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
expires max;
log_not_found off;
}
location / {
# Check if a file exists, or route it to index.php.
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /path/to/my/php/code/$fastcgi_script_name;
}
location /redis {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
rewrite /(.*)/(.*)/(.*)$ /$2/$3 break; #ignore the /redis
proxy_redirect off;
proxy_pass http://webdisServerPool;
}
}
在前端,下面是获取所有密钥的示例。例如,所有 redis 请求都将通过 /redis:
$.ajax({
url: "/redis/KEYS/*",
method: 'GET',
dataType: 'json',
success:function(data)
{
$each(data.KEYS,function(key,value){
$('body').append(key+"=>"+value+" <br> ");
});
}
});
或
您可以使用:
http://wiki.nginx.org/HttpRedis 并自行解析响应。