安装nginx

nginx查看之前安装时指定的安装参数信息

[root@lavancy ~]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.20.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --with-http_ssl_module --with-http_mp4_module --prefix=/usr/local/nginx

安装最新openssl提示

[root@racknerd-d42c08 openssl-3.0.0]# ./Configure
Can't locate IPC/Cmd.pm in @INC (@INC contains: /usr/local/openssl-3.0.0/util/perl /usr/local/lib64/perl5 
/usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5
. /usr/local/openssl-3.0.0/external/perl/Text-Template-1.56/lib) at /usr/local/openssl-3.0.0/util/perl/OpenSSL/config.pm line 18.
BEGIN failed--compilation aborted at /usr/local/openssl-3.0.0/util/perl/OpenSSL/config.pm line 18. Compilation failed in require at ./Configure line 23. BEGIN failed--compilation aborted at ./Configure line 23. ########## 参考https://community.oracle.com/tech/apps-infra/discussion/4481624/got-error-when-trying-to-install-openssl-on-oracle-linux-8 If you're on Oracle Linux 7: yum install perl-IPC-Cmd If you're on Oracle Linux 8: dnf install perl-IPC-Cmd

 安装nginx启动http_ssl_module时报错

[root@racknerd-d42c08 nginx-1.20.2]# make
make -f objs/Makefile
make[1]: Entering directory `/usr/local/nginx-1.20.2'
cd /usr/local/openssl 
&& if [ -f Makefile ]; then make clean; fi 
&& ./config --prefix=/usr/local/openssl/.openssl no-shared no-threads  
&& make 
&& make install_sw LIBDIR=lib
/bin/sh: line 2: ./config: No such file or directory
make[1]: *** [/usr/local/openssl/.openssl/include/openssl/ssl.h] Error 127
make[1]: Leaving directory `/usr/local/nginx-1.20.2'
make: *** [build] Error 2
[root@racknerd-d42c08 nginx-1.20.2]# cd /usr/local/openssl
[root@racknerd-d42c08 openssl]# ll
total 20
drwxr-xr-x. 2 root root 4096 Nov 19 22:24 bin
drwxr-xr-x. 3 root root 4096 Nov 19 22:24 include
drwxr-xr-x. 5 root root 4096 Nov 19 22:24 lib64
drwxr-xr-x. 4 root root 4096 Nov 19 22:26 share
drwxr-xr-x. 5 root root 4096 Nov 19 22:24 ssl
[root@racknerd-d42c08 openssl]# cd ../
[root@racknerd-d42c08 local]# cd nginx-1.20.2
#根据错误提示的路径是找不到/usr/local/openssl/.openssl/include/openssl/ssl.h,向上减一级即--with-openssl=/usr/local/
[root@nginx-1.20.2]# ./configure --with-http_ssl_module --with-http_mp4_module --prefix=/usr/local/nginx --with-openssl=/usr/local
#事实这种方法也是行不通的,最终是明白--with-openssl=是指的openssl下载解压后的源码包即可以了

默认没有安装nginx的ssl扩展,使用证书配置,如果是已经安装了,在源码包中执行./configure --with-http_ssl_module  --prefix=/usr/local/nginx,然后make会在源码包

中的objs下重新生成nginx程序,不要make install因为会覆盖原有安装,然后将objs下的nginx拷贝取代到对应目录下sbin/nginx,监听apache多个端口时apache应在

httpd.conf中配置多个Listen分多行,然后在VirturalHost :8081、VirturalHost :8082使用

server {
        listen       80;
        listen       443 ssl;//取代 ssl on;指令
        server_name mysilverjewels.com www.mysilverjewels.com;
        ssl_certificate     certificate/www.mysilverjewels.com.crt;
        ssl_certificate_key certificate/www.mysilverjewels.com.key;
        location / {
            proxy_pass http://127.0.0.1:8081;
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Fonwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto https;//请求为https但是静态资源使用http可以使用$scheme变量替换
      }
      if ($scheme = http) {
            return 301 https://$server_name$request_uri;//当http和https写在一块时通过if判断重定向
      }

}

nginx配置IP访问时遇到:使用IP可以访问但是css和js部分路径加载不过来,是因为没有配置header头,将当前访问的域带到转发服务器导致的,当使用https之后cssjs样式没用用https加载时用使用proxy_set_header X-Forward-Proto $scheme;

server {
        listen       80;
        server_name  plus.jl.jiaxiaoquan.com;
        location / {
        access_log /apps/openresty/nginx/logs/jl.access.log;
        error_log /apps/openresty/nginx/logs/jl.error.log;
        proxy_pass  http://jlweb/;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Fonwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto https;//当使用https之后cssjs样式没用用https加载时用
        }
}       

nginx代理时遇到问题

1出现

Bad Request
Your browser sent a request that this server could not understand.
Additionally, a 400 Bad Request error was encountered while trying to use an ErrorDocument to handle the request.

这种问题是因为upstream名称中用了带”_”的名称

nginx漏洞修复

user aspire;
worker_processes  16;

error_log  /apps/nginx/logs/error.log;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}



http {
    include       /apps/nginx/conf/conf.d/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" "$request_time" "$upstream_response_time" "$upstream_addr"';

    access_log  /apps/nginx/logs/access.log  main;

    sendfile        on;
    tcp_nopush     on;
    server_tokens off;//漏洞①
    #keepalive_timeout  0;
    keepalive_timeout  65;
#漏洞②Http host头部攻击容易造成污染和密码重置风险,缓存污染是指攻击者通过控制一个缓存系统来将一个恶意站点的页面返回给用户。密码重置这种攻击主要是因为发送给用户的内容是可以污染的,也就是说可以间接的劫持邮件发送内容; _ 指当前ip
#1.在Nginx里还可以通过指定一个SERVER_NAME名单,
#2.Apache也可以通过指定一个SERVER_NAME名单并开启UseCanonicalName选项。
#3.tomcat修改server.xml文件中的 Host name为域名
#如:
#      <Host name=" www.example.com "  appBase="webapps"
#           unpackWARs="true" autoDeploy="true"
#            xmlValidation="false" xmlNamespaceAware="false">
#</Host>
#4.或者使用filter拦截非本站允许的host。
    server {
       listen 80 default_server;
       server_name _;
       location / {
           return 204;
       }
    }

    include /apps/nginx/conf/conf.d/*.conf;
}

问题描述:

     项目分为java服务端部分和php服务端部分,两者使用一个域名,之前的部署是一个在nginx上进行端口转发分发到java服务的tomcat服务上和apache服务上,

而本次php端打算直接省掉apache直接对接nginx服务器,主要问题是对路径的重写和静态资源的请求上

 1 server {
 2         listen       80;
 3         server_name  xxx.xxxx.com;
 4         access_log   /var/log/nginx/access_xxt.log main;
 5 
 6         root         /var/asxpire/www/xxx_web/current;
 7         index        index.php;
 8 
 9         location / {
10 
11                 location ~ .(css|js|html|htm|jpg|gif|png|webp|jpeg|ico)$ {
12                              root /var/asxpire/www/xxt_web/current;
13                 }
14 
15 
16                 if ($uri ~ "^/(?!(data|public|(.*Tpl)|(.*.(php|html)))).*$"){
17                         rewrite "^/((/apps/[^/]+){0,1})(.*)$" /index.php?s=$3 last;
18                 }
19                 fastcgi_pass 127.0.0.1:9000;
20                 fastcgi_index index.php;
21                 fastcgi_param SCRIPT_FILENAME 
22                 $document_root$fastcgi_script_name;
23                 include fastcgi_params;
24 
25                 access_log /apps/nginx/logs/hunanweb.access.log;
26                 error_log /apps/nginx/logs/hunanweb.error.log;
27         }
28 
29          location /xxtmanageweb {
30                 access_log /apps/nginx/logs/xxtmanageweb.access.log;
31                 error_log /apps/nginx/logs/xxtmanageweb.error.log;
32                         proxy_pass   http://xxtmanageweb;
33                 }
34 
35          location /xxtweb {
36                 access_log /apps/nginx/logs/xxtweb.access.log;
37                 error_log /apps/nginx/logs/xxtweb.error.log;
38                         proxy_pass   http://xxtweb;
39                 }
40                            location /mobileweb {
41                 access_log /apps/nginx/logs/mobileweb.access.log;
42                 error_log /apps/nginx/logs/mobileweb.error.log;
43                         proxy_pass   http://mobileweb;
44                 }
45 
46          location /remote-server {
47                 access_log /apps/nginx/logs/remote-server.access.log;
48                 error_log /apps/nginx/logs/remote-server.error.log;
49                         proxy_pass   http://remote-server;
50                 }
51 
52          location /typthn {
53                 access_log /apps/nginx/logs/typthn.access.log;
54                 error_log /apps/nginx/logs/typthn.error.log;
55                         proxy_pass   http://typthn;
56                 }
57 
58         #location / {
59         #        try_files $uri $uri/ /index.php$args;
60         #}
61 
62 }
View Code

问题:原先是只针对  location ~ .php {}以.php为后缀的进行php处理,但是原先重写过的路径就不起作用了(不含.php后缀的),如果要添加模块又比较多;如这种写法location ~ .php|Home|Index {},后经过与之前其他项目的分发对比是将 / 这个路径作为php处理的依据,即所右没转发走的都走php处理,于是也使用了这个逻辑

这样针对重写的模块就都可以访问了,但是页面加载不出静态资源如images、css、js、html,这是因为对所有没有找到的资源进进入/块行了php-fpm请求,而php-fmp一般只处理php后缀的请求,所以会出现如下的错误

 *944717 FastCGI sent in stderr: "Access to the script '/var/asxpire/www/xxt_web/current/public/js/ymPrompt/ymPrompt.min.js' has been denied (see security.limit_extensions)" while reading response header from upstream, client: 192.168.10.11, server: xxx.xxx.com, request: "GET /public/js/ymPrompt/ymPrompt.min.js HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "xxx.xxx.com", referrer: "http://xxx.xxx.com/"

这个在/etc/php-fpm.d/下的配置中的security.limit_extensions = .php .php3 .php4 .php5 .php7 .js .css中定义,加了扩展实验好用,但据说增加了一定的风险,并且静态资源需要再次调用php-fpm进行处理多了一层请求,想尽可能都有nginx处理,于是针对图片进行重写

location ~ .(gif|jpg|jpeg|png|css|js|ico)$ {
    root /var/www/xx;
}

确实针对自己的网站好用,但是影响了其他走代理的项目,如location /xxtweb导致该网站的静态资源全部找不到;后通过去官网查询http://nginx.org/en/docs/http/ngx_http_core_module.html#location,文中location blocks can be nested, with some exceptions mentioned below.指明location块可以嵌套,只要嵌套了就不会影响其他模块和代理随将其移动到/块下,取得了想要的结果

使用nginx遇到问题:

25870#0: *521312 open() "/apps/aspire/www/hunan_web/current/index.html" failed (13: Permission denied), client: 192.168.10.131, server: jhx.xxx.com, request: "GET /index.html HTTP/1.1", host: "jhx.xxx.com"

nginx日志错误,是权限的问题,就将/etc/nginx/nginx.conf中的第一行改为root或www或nginx服务用户

[error] 29320#0: *521750 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 192.168.10.131, server: jxx.xxx.com, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "jxx.xxx.com"

 具体表现是:在配置的项目目录中随意创建一个index.html可以访问但是php文件访问不了;并且fastcgi是使用nginx默认配置,排查之后应该在php-fpm上,编辑php-fpm.d/www.conf中的

user = aspire
; RPM: Keep a group allowed to write in log dir.
group = aspire
#将user和group修改成nginx用户 change to Nginx user
[error] 7714#0: *530757 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 192.168.10.131, server: hunanjxy.jiaxiaoquan.com, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "xx.jxq.com"
location ~ .php$ {
            proxy_read_timeout 150;#超时设置
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }

该错误是由于nginx 代理去获取上游服务器的 返回值超时了。那么这个问题是什么导致的:该请求获取的数据比较多,后端处理该请求花费的时间较长。也可能是代理服务器与上游服务器的网络问题

后经设置proxy_read_timeout发现是程序连接了无效数据库导致超时

tar式安装
  -x,从归档中解出文件
  -f,使用归档文件或 ARCHIVE 设备
  -z,通过 gzip 过滤归档
  -v,详细地列出处理的文件
gcc用yum search gcc搜索,yum install gcc安装
需安装pecl步骤configure --prefix=/dta   make make install

安装后;再安装php,我的问题出现了

第一次出现了nginx403forbidden;经过查找原来是网站文件没有读、执行权限;

第二次出现了502badgateway和404经过查找原因是路径问题;即你在Server配置中root /data/www/,而在访问时只要输入ip就直接定位到index.php,而不用www/index.php

第三次又出现了502badgateway,经测试能访问.html格式文件但不支持php,后上网搜素查到是php-fpm的问题,删掉原来的php重新撞了一下在./configre时多添了 --enable-fpm,重装之后在php目录下有了sbin目录里边有个php-fpm,先修改php-fpm配置php-fmp.conf,然后启动/usr/local/php/sbin/php-fpm

location / {

   root /data/www;

}

location /images/ {

   root /data;

}

该location块指定/前缀与来自请求的URI比较,对于匹配到的请求,URI将被添加到root指令中指定的路径,即/data/www,如果有多个匹配的location块前缀,nginx将选择一个最长前缀的块,location块上上面是长度为1的最短前缀,所以如果所有的其它块都匹配失败,这个块才将会被用到,如果请求是以/images/开头的将会匹配到下边的location虽然location /也能匹配到,但它不是最短前缀

server {
    listen 8080;
    root /data/up1;

    location / {
    }
}
映射所有的请求到本地文件系统的/data/up1目录
location ~ .(gif|jpg|png)$ {
    root /data/images;
}
这里的参数是正则表达式匹配所有以.gif,.jpg,.png结尾的URI,一个正则表达式前边应使用~
当nginx选择一个location块去服务一个请求的时候会首先检查location指定的具体前缀(最长前缀原则),ranhou
是检查正则表达式如果匹配到nginx将使用该location
启动遇到问题


配置使用的默认值

fastcgi_pass   127.0.0.1:9000;而自己启动php-fpm启动端口不是9000造成的,将fastcgi_pass配置改成unix:/var/run/php-fpm/php-fpm.sock;或者在php-fpm的conf中配置监听端口
Nginx和PHP-FPM的进程间通信有两种方式,一种是TCP,一种是UNIX Domain Socket.
其中TCP是IP加端口,可以跨服务器.而UNIX Domain Socket不经过网络,只能用于Nginx跟PHP-FPM都在同一服务器的场景.用哪种取决于你的PHP-FPM配置:
方式1:
php-fpm.conf: listen = 127.0.0.1:9000
nginx.conf: fastcgi_pass 127.0.0.1:9000;
方式2:
php-fpm.conf: listen = /tmp/php-fpm.sock
nginx.conf: fastcgi_pass unix:/tmp/php-fpm.sock;
其中php-fpm.sock是一个文件,由php-fpm生成,类型是srw-rw----.
产生下载的问题:当访问php页面时浏览器提示下载,原因是nginx没有开启php-fpm或在nginx配置中没有开启location ~ .php$配置节点
nginx需要把php请求转发给fastcgi处理
2017/11/08 05:48:47 [error] 10210#0: *72 connect() failed (111: Connection refused) while connecting to upstream, client: 106.120.105.61, server: xhl.com, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "xhl.com:8081"

解决:先将权限问题排除,给网站根目录赋予最高权限777,还有错误

页面提示File not found.文件权限也会导致该问题,为other用户赋予x权限

       location ~ .php$ {
            root           html;#有可能导致File not found
            #root           /home/www/wp;
            #fastcgi_pass   127.0.0.1:9000;
            fastcgi_pass   unix:/var/run/php-fpm/php-fpm.sock;
            fastcgi_index  index.php;
            #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;#有可能导致File not found
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
#运行用户
user nobody;
#启动进程,通常设置成和cpu的数量相等
worker_processes  1;

#全局错误日志及PID文件
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

#工作模式及连接数上限
events {
    #epoll是多路复用IO(I/O Multiplexing)中的一种方式,
    #仅用于linux2.6以上内核,可以大大提高nginx的性能
    use   epoll; 

    #单个后台worker process进程的最大并发链接数    
    worker_connections  1024;

    # 并发总数是 worker_processes 和 worker_connections 的乘积
    # 即 max_clients = worker_processes * worker_connections
    # 在设置了反向代理的情况下,max_clients = worker_processes * worker_connections / 4  为什么
    # 为什么上面反向代理要除以4,应该说是一个经验值
    # 根据以上条件,正常情况下的Nginx Server可以应付的最大连接数为:4 * 8000 = 32000
    # worker_connections 值的设置跟物理内存大小有关
    # 因为并发受IO约束,max_clients的值须小于系统可以打开的最大文件数
    # 而系统可以打开的最大文件数和内存大小成正比,一般1GB内存的机器上可以打开的文件数大约是10万左右
    # 我们来看看360M内存的VPS可以打开的文件句柄数是多少:
    # $ cat /proc/sys/fs/file-max
    # 输出 34336
    # 32000 < 34336,即并发连接总数小于系统可以打开的文件句柄总数,这样就在操作系统可以承受的范围之内
    # 所以,worker_connections 的值需根据 worker_processes 进程数目和系统可以打开的最大文件总数进行适当地进行设置
    # 使得并发总数小于操作系统可以打开的最大文件数目
    # 其实质也就是根据主机的物理CPU和内存进行配置
    # 当然,理论上的并发总数可能会和实际有所偏差,因为主机还有其他的工作进程需要消耗系统资源。
    # ulimit -SHn 65535

}


http {
    #设定mime类型,类型由mime.type文件定义
    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 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,
    #对于普通应用,必须设为 on,
    #如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,
    #以平衡磁盘与网络I/O处理速度,降低系统的uptime.
    sendfile     on;
    #tcp_nopush     on;

    #连接超时时间
    #keepalive_timeout  0;
    keepalive_timeout  65;
    tcp_nodelay     on;

    #开启gzip压缩
    gzip  on;
    gzip_disable "MSIE [1-6].";

    #设定请求缓冲
    client_header_buffer_size    128k;
    large_client_header_buffers  4 128k;


    #设定虚拟主机配置
    server {
        #侦听80端口
        listen    80;
        #定义使用 www.nginx.cn访问
        server_name  www.nginx.cn;

        #定义服务器的默认网站根目录位置
        root html;

        #设定本虚拟主机的访问日志
        access_log  logs/nginx.access.log  main;

        #默认请求
        location / {
            
            #定义首页索引文件的名称
            index index.php index.html index.htm;   

        }

        # 定义错误提示页面
        error_page   500 502 503 504 /50x.html;
        location = /50x.html {
        }

        #静态文件,nginx自己处理
        location ~ ^/(images|javascript|js|css|flash|media|static)/ {
            
            #过期30天,静态文件不怎么更新,过期可以设大一点,
            #如果频繁更新,则可以设置得小一点。
            expires 30d;
        }

        #PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI默认配置.
        location ~ .php$ {
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include fastcgi_params;
        }

        #禁止访问 .htxxx 文件
            location ~ /.ht {
            deny all;
        }

    }
}

 自己动手搭建nginx负载均衡服务器感悟

负载均衡的机制round-robin(循环,请求被循环分发到各服务器)默认、least-connected(最少连接,下一个请求被分配给当前活动连接最少的服务器)、ip-hash(使用一个hash函数决定哪台服务器去接受请求,基于客户端IP)

自己搭建情况,由于80端口被apache占用nginx运行在8081上,按照视频配置成

 http {
      upstream xhl
      {
         #  server 142.4.105.129:8081;primary server
            server 142.4.105.129:8082;#department server
            server 142.4.105.129:8083;#department server
      }
      include       mime.types;
   。。。

原来的理解是错的把自己的主服务器也配置到了upstream中因此造成链接过多内容不可达的情况

正确的配置应该是通过访问8081,而把请求分发到其他两台服务器,8081相当是分发服务器

    server {
        listen       8081;
        server_name  xhl.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        access_log  logs/host.leeleeshow.access.log  main;

        root   /home/www/wp;

        location / {
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_pass http://xhl;
            index  index.html index.htm index.php;
        }

        #error_page  404              /404.html;
这是分发服务器的配置
=================================
 server {
             listen 8082;
             server_name _;
             root /home/www/shop/web;
             location / {
                add_header REAL_SERVER 8082;
                index index.html index.php;
             }        
             location ~ .php$ {
                 add_header REAL_SERVER 8082;
                 fastcgi_pass  unix:/var/run/php-fpm/php-fpm.sock;
                 fastcgi_index index.php;
                 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                 include fastcgi_params;
             }


   }
    server {
             listen 8083;
             server_name _;
             root /home/www/shop/web;
             location / {
                 add_header REAL_SERVER 8083;
                 index index.html index.php;
             }
             location ~ .php$ {
                 add_header REAL_SERVER 8083;
                 fastcgi_pass  unix:/var/run/php-fpm/php-fpm.sock;
                 fastcgi_index index.php;
                 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                 include fastcgi_params;
             }
    
    
    }    

所有的请求都被代理到服务器组里xhl,nginx使用hTTp负载均衡去分发请求

upstream myapp1 {
        least_conn;
        server srv1.example.com;
        server srv2.example.com;
        server srv3.example.com;
    }

这是最少请求配置least_conn;

session持久化,请注意到循环和最少请求配置负载均衡,每个后续的客户端请求可能被分配到不同的服务器上,不能保证

相同的客户端总是指向相同的服务器,如果需要客户端与一个特殊的应用服务器绑定,换句话说,使客户端的session黏贴或永久的选择一个特殊的服务器,那么使用ip-hash负载均衡机制可以实现

当使用ip-hash时客户端的ip被使用作为一个hash key去决定服务器群里哪台服务器器将被选中为当前的客户请求服务,确保相同的客户请求总是直接防卫相同的服务器,除非服务不可用

upstream myapp1 {
    ip_hash;
    server srv1.example.com;
    server srv2.example.com;
    server srv3.example.com;
}

按权重负载,上边的例子中服务器权重不配置的话意味着所有的服务器都是相同的权重

round-robin也或多或少意味着相同权重分配

 upstream myapp1 {
        server srv1.example.com weight=3;
        server srv2.example.com;
        server srv3.example.com;
    }

如上的配置每有5个新请求,3个将被分配给srv1,一个分给srv2,一个分给srv3

原文地址:https://www.cnblogs.com/zhiguopingtianxia/p/3979412.html