Skip to main content

session存redis

Nginx设置(修改nginx.conf文件)

#user  nobody;
worker_processes 1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;


events {
worker_connections 1024;
}


http {
include mime.types;
default_type application/octet-stream;

sendfile on;
keepalive_timeout 65;


# 负载均衡设置
upstream localhost {
server localhost:9200;
server localhost:9201;
}

server {
listen 80;

location / {
proxy_pass http://localhost;
}
}

# 第一个站点
server {
listen 9200;
server_name localhost:9200;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {
root html;
index index.php index.html index.htm;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}

# 第二个站点
server {
listen 9201;
server_name localhost:9201;

location / {
root html2;
index index.php index.html index.htm;
}

location ~ \.php(.*)$ {
root html2;
fastcgi_pass 127.0.0.1:9900;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}

}


}

PHP设置(修改php.ini文件)
去掉“;”分号

cgi.fix_pathinfo=1

修改

session.save_handler = redis
session.save_path = "tcp://127.0.0.1:6379"

PHP代码内

echo '111-9200';

//如果未修改php.ini下面两行注释去掉
//ini_set('session.save_handler', 'redis');
//ini_set('session.save_path', 'tcp://127.0.0.1:6379');
session_start();
$_SESSION['sessionid'] = session_id();
echo $_SESSION['sessionid'];
echo '<br/>';

$redis = new redis();
$redis->connect('127.0.0.1', 6379);
//redis用session_id作为key并且是以string的形式存储
echo $redis->get('PHPREDIS_SESSION:' . session_id());

心得:
session_id 是在负载均衡那一步生成的。不用担心访问不同的服务器,session_id不一致的情况。

问题:
nginx的配置问题吧,第二个网站ini_set() 和 redis扩展,都不能使用,不知道为啥。