nginx 负载均衡 及 配置问题 01

在没有实操负载均衡的时候,觉得负载均衡好流弊,完全不懂。
当技术上升到现在,觉得麻蛋,负载均衡原来也就是一个简单的转发配置。
心里顿时一万只 CNM 从心窝里飞翔.
话不多说 贴代码

配置

下面这些配置都在 http 里面
upstream nginx_fu {
    server 120.79.107.5:80 weight=20;
    server 119.23.146.108:8001 weight=20;
}
server {
    listen 80;
    server_name _;
    access_log /data/wwwlogs/access_nginx.log combined;
    root /data/wwwroot/default/;
    location / {
        proxy_pass   http://nginx_fu;
        index  index.html index.php;
    }
}
 server{
         listen       8001;
         server_name  localhost;
         root /data/wwwroot/default/;
         #charset koi8-r; 
         #access_log  logs/host.access.log  main; 
         location / {
             index index.html index.php;
         } 
         location ~ [^/]\.php(/|$) {
             #fastcgi_pass remote_php_ip:9000;
             fastcgi_pass unix:/dev/shm/php-cgi.sock;
             fastcgi_index index.php;
             include fastcgi.conf;
         } 
         #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;
         }
     }

upstream 里面配置的是 参与负载均衡的服务器
         配置方式 ip:端口 [weight=20];
参与负载均衡的方式有 轮训 跟 权重 上面的配置是按照权重分配,权重值越大,转发的概率就越高
上面的配置解释:
    所有经过本台服务器80端口的请求,都会经过 location /  通过 proxy_pass   http://nginx_fu;来转发,转发给本机子的8001端口 或者 另一台服务器的 80端口

tips:

首先匹配 =,其次匹配^~, 其次是按文件中顺序的正则匹配,最后是交给 / 通用匹配。当有匹配成功时候,停止匹配,按当前匹配规则处理请求。

= 开头表示精确匹配

^~ 开头表示uri以某个常规字符串开头,理解为匹配 url路径即可。nginx不对url做编码,因此请求为/static/20%/aa,可以被规则^~ /static/ /aa匹配到(注意是空格)。

~ 开头表示区分大小写的正则匹配

~*  开头表示不区分大小写的正则匹配

!~和!~*分别为区分大小写不匹配及不区分大小写不匹配 的正则

/ 通用匹配,任何请求都会匹配到。

多个location配置的情况下匹配顺序为(参考资料而来,还未实际验证,试试就知道了,不必拘泥,仅供参考):

你可以试试

server {
    listen 80;
    server_name _;
    access_log /data/wwwlogs/access_nginx.log combined;
    root /data/wwwroot/default/;
    location / {
        proxy_pass   http://nginx_fu;
        index  index.html index.php;
    }
}
变成
server {
    listen 80;
    server_name _;
    access_log /data/wwwlogs/access_nginx.log combined;
    root /data/wwwroot/default/;
    location / {
        proxy_pass   http://nginx_fu;
        index  index.html index.php;
    }
    location ~ [^/]\.php(/|$) {
         #fastcgi_pass remote_php_ip:9000;
         fastcgi_pass unix:/dev/shm/php-cgi.sock;
         fastcgi_index index.php;
         include fastcgi.conf;
    } 
}
你会发现,只要是php后缀的都只会访问本台机子的8001,这就是 上面所 提示的  优先级造成的后果

当你希望 你的php 文件也可以负载均衡。同理加上 proxy_pass   http://nginx_fu; 即可
    location ~ [^/]\.php(/|$) {
         #fastcgi_pass remote_php_ip:9000;
         fastcgi_pass unix:/dev/shm/php-cgi.sock;
         fastcgi_index index.php;
         include fastcgi.conf;
         proxy_pass   http://nginx_fu;
    } 

负载均衡 关于session不能共享的的问题


1、在php.ini中改下面两句:
session.save_handler = redis 
session.save_path = "tcp://192.168.159.3:6379"
如果redis服务器有密码 session.save_path = "tcp://192.168.159.3:6379" 改为:session.save_path = "tcp://192.168.159.3:6379?auth=pwd" pwd为redis服务器的密码
注意:redis 服务器为了安全,bind的是127.0.0.1,为了外网能访问到,bind可以改为服务器本身的ip

2、也可以携带式 直接走参数

猜你喜欢

转载自blog.csdn.net/qq_16142851/article/details/79485963
今日推荐