php language to build nginx reverse proxy, load balancing problem often encountered

1, when configured nginx configuration, build a good reverse proxy, the situation appears click on your links page can not find the path 404

The marked on this line to comment on the line, because this line will be redirected to a file ending in .php, so it will not go the following location Reverse Proxy

2, modify nginx configuration, set up load balancing

Actually very simple, only you need to configure both on it, pay attention to upstream and server are the same level, not in the server's configuration

 3, there is an important step to achieve a shared session, I realized session sharing is redis

First redis servers were set up in the server, the principle is to allow each server so that remote links are redis session is stored in a fixed redis server to be shared

Modify the configuration redis

3.1, bind 127.0.0.1 are only allowed to access the machine, it does not allow remote access redis, modified to 0.0.0.0 can be visited

redis3.2 the protected-mode version of the new configuration, the default is yes, that is open. Redis external network connection service is provided, is provided as follows:

   1, protected-mode off mode, then the external network can directly access

   2, open the protected-mode protection mode, configure or set access password bind ip

Set daemonize yes, let redis in the background from the start, not every manual start

set password

These are some of the preparatory work.

4, modify our server session storage, can be changed in php.ini, you can also use the temporary change in the ini_set page. In this proposal with ini_set modification does not affect other sites

ini_set("session.save_handler", "redis");
    // ini_set("session.bind", '0.0.0.0');
ini_set("session.save_path", "tcp://192.168.0.127:6379");
    // ini_set('default_socket_timeout', -1);
    session_start();//存入session
    $_SESSION['hu'] = '123';//连接redis
    $redis = new redis();
    $redis->connect('192.168.0.127', 6379);
    // $redis->auth('qw12!@');
    //检查session_idecho 
    echo 'session_id:' . session_id() . '<br/>';
    //redis存入的session(redis用session_id作为key,以string的形式存储)
    echo 'redis_session:' . $redis->get('PHPREDIS_SESSION:' . session_id()) . '<br/>';

    echo $_SESSION['name'];
    //php获取session值echo 'php_session:' . json_encode($_SESSION['class']); 

注意session_start();不要放在ini_set的前面,不然会报错

PHP Warning:  ini_set(): A session is active. You cannot change the session module's ini settings at this time in path\to\file.php on line 79
如果要在脚本中使用ini_set()更改PHP的session配置参数,需要在调用session_start()开会会话以前完成ini参数的修改工作,会话已经启动的时候再去修改SESSION运行时配置参数,将会触发一个警告级别的错误,并且对配置参数的修改也不会生效。
PHP Warning并不会影响后续代码,可以在ini_set()前面添加一个 @ 符号来屏蔽错误信息。

还有$_SESSION也需要在ini_set后边才能调用

 

Guess you like

Origin www.cnblogs.com/hualingyun/p/11243283.html