Nginx的反向代理,动静分离,负载均衡,服务器健康状态检查

先推荐两篇文章地址:讲述的很清楚了

https://blog.csdn.net/wjg_ww/article/details/49924353

https://blog.csdn.net/chelen_jak/article/details/52583540

关于nginx参数的详解:https://www.cnblogs.com/weishanyun/p/6305406.html

负载均衡的一些说明:https://blog.csdn.net/lxb15959168136/article/details/53113996

前面必须确保nginx能成功安装并启动。

开始:

80端口反向代理8080:

(直接替换nginx.conf里的server那段代码注意后面的})然后nginx平滑重启:nginx -s reload   #加载刚刚加入的配置

server {
    listen       80;
   # root   /usr/share/nginx/html;
    server_name  localhost 150.109.65.xx;
    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;
   location / {
          proxy_pass  http://150.109.65.xx:8080/;
          proxy_set_header Host      $host;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

  }

  }

动静分离的配置:


server {

    listen       80;
   # root   /usr/share/nginx/html;
    server_name  localhost 150.109.65.xx;
    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;
   location / {
          proxy_pass  http://150.109.65.xx:8080/;
          proxy_set_header Host      $host;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

  }

  location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|css|js|ttf|woff)$ {
          root /usr/local/tomcat7/webapps/;
          expires      3d; 
 }

}

负载均衡配置:

     upstream proxy_master{
      server 150.109.65.23:8081 weight=1;
      server 150.109.65.23:8080 weight=1;
       #ip_hash; 
   }

server {
    listen       80;
   # root   /usr/share/nginx/html;
    server_name  localhost 150.109.65.xx;
    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;
   location / {
          proxy_pass  http://proxy_master;
          proxy_set_header Host      $host;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

  }

  location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|css|js|ttf|woff)$ {
          root /usr/local/tomcat7/webapps/;
          expires      3d; 
 }

}

健康状态的检测(只是简单demo):

  http {
      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  /var/log/nginx/access.log  main;     
      sendfile            on;
     tcp_nopush          on;
     tcp_nodelay         on;
     keepalive_timeout   65;
     types_hash_max_size 2048;
     include             /etc/nginx/mime.types;

     default_type        application/octet-stream;

    proxy_connect_timeout 60s;
    proxy_read_timeout 60s;    

    proxy_next_upstream http_502 http_504 http_404 error timeout invalid_header;

  
      # Load modular configuration files from the /etc/nginx/conf.d directory.
      # See http://nginx.org/en/docs/ngx_core_module.html#include
      # for more information.
      include /etc/nginx/conf.d/*.conf;
    upstream proxy_master{
       server 150.109.65.xx:8081 weight=1;
       server 150.109.65.xx:8080 weight=1;
      #ip_hash;
    }

server {
    listen       80;
   # root   /usr/share/nginx/html;
    server_name  localhost 150.109.65.xx;
    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;
   location / {
          proxy_pass  http://proxy_master;
          proxy_set_header Host      $host;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

  }

  location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|css|js|ttf|woff)$ {
          root /usr/local/tomcat7/webapps/;
          expires      3d; 
 }

}

标红代码块也可写在 location / {}中



问题说明:可以这里面看日志文件

里面还有个error.log。一般404 之类的路径错误就能看到并解决。


一些参数说明 

动静分离配置:

在server内配置这个 防止直接访问web-inf下的静态文件     

location ~ ^/(WEB-INF)/ {

      deny all;  
    } 

 #expires定义用户浏览器缓存的时间为7天,如果静态页面不常更新,可以设置更长,这样可以节省带宽和缓解服务器的压力

开启gzip压缩来提高速度

gzip   on;   #开启gzip
gzip_min_length 1k;   #低于1kb的资源不压缩
gzip_comp_level 3;  #压缩级别【1-9】,越大压缩率越高,同时消耗cpu资源也越多,建议设置在4左右。
gzip_types text /plain  application /javascript  application /x-javascript  text /javascript  text /xml  text /css ;   #需要压缩哪些响应类型的资源,多个空格隔开。不建议压缩图片,下面会讲为什么。
gzip_disable  "MSIE [1-6]\." ;   #配置禁用gzip条件,支持正则。此处表示ie6及以下不启用gzip(因为ie低版本不支持)
gzip_vary on;   #是否添加“Vary: Accept-Encoding”响应头


gzip虽然好用,但是一下类型的资源不建议启用。


1、图片类型

原因:图片如jpg、png本身就会有压缩,所以就算开启gzip后,压缩前和压缩后大小没有多大区别,所以开启了反而会白白的浪费资源。(Tips:可以试试将一张jpg图片压缩为zip,观察大小并没有多大的变化。虽然zip和gzip算法不一样,但是可以看出压缩图片的价值并不大)


2、大文件

原因:会消耗大量的cpu资源,且不一定有明显的效果。


负载均衡说明

upstream按照轮询(默认)方式进行负载,每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能 自动剔除。虽然这种方式简便、成本低廉。但缺点是:可靠性低和负载分配不均衡。适用于图片服务器集群和纯静态页面服务器集群

除此之外,upstream还有其它的分配策略,分别如下:

    weight(权重)

    指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。如下所示,10.0.0.88的访问比率要比10.0.0.77的访问比率高一倍。

upstream linuxidc{
      server 10.0.0.77 weight=5;
      server 10.0.0.88 weight=10;
}

    ip_hash(访问ip)

    每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。

upstream favresin{
      ip_hash;
      server 10.0.0.10:8080;
      server 10.0.0.11:8080;
}

    fair(第三方)

    按后端服务器的响应时间来分配请求,响应时间短的优先分配。与weight分配策略类似。

 upstream favresin{     
      server 10.0.0.10:8080;
      server 10.0.0.11:8080;
      fair;
}

url_hash(第三方)

按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。

注意:在upstream中加入hash语句,server语句中不能写入weight等其他的参数,hash_method是使用的hash算法。

 upstream resinserver{
      server 10.0.0.10:7777;
      server 10.0.0.11:8888;
      hash $request_uri;
      hash_method crc32;
}

upstream还可以为每个设备设置状态值,这些状态值的含义分别如下:

down 表示单前的server暂时不参与负载.

weight 默认为1.weight越大,负载的权重就越大。

max_fails :允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误.

fail_timeout : max_fails次失败后,暂停的时间。

backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。

upstream bakend{ #定义负载均衡设备的Ip及设备状态
      ip_hash;
      server 10.0.0.11:9090 down;
      server 10.0.0.11:8080 weight=2;
      server 10.0.0.11:6060;
      server 10.0.0.11:7070 backup;
}

服务器健康状态检测

这个讲的比较详细:https://www.cnblogs.com/rainy-shurun/p/5416160.html

服务器健康状态的检查主要有nginx自带的两个模块ngx_http_proxy_module 模块和ngx_http_upstream_module模块,阿里的一个nginx_upstream_check_module模块。

 这里列出这两个模块中相关的指令:
ngx_http_proxy_module 模块中的 proxy_connect_timeout 指令、proxy_read_timeout指令和proxy_next_upstream指令

1
2
3
语法: proxy_connect_timeout time;
默认值:    proxy_connect_timeout 60s;
上下文:    http, server, location



设置与后端服务器建立连接的超时时间。应该注意这个超时一般不可能大于75秒。

1
2
3
语法: proxy_read_timeout time;
默认值:    proxy_read_timeout 60s;
上下文:    http, server, location



定义从后端服务器读取响应的超时。此超时是指相邻两次读操作之间的最长时间间隔,而不是整个响应传输完成的最长时间。如果后端服务器在超时时间段内没有传输任何数据,连接将被关闭。

1
2
3
语法: proxy_next_upstream error | timeout | invalid_header | http_500 | http_502 | http_503 | http_504 |http_404 | off ...;
默认值:    proxy_next_upstream error timeout;
上下文:    http, server, location



指定在何种情况下一个失败的请求应该被发送到下一台后端服务器:

1
2
3
4
5
6
7
8
9
error      # 和后端服务器建立连接时,或者向后端服务器发送请求时,或者从后端服务器接收响应头时,出现错误
timeout    # 和后端服务器建立连接时,或者向后端服务器发送请求时,或者从后端服务器接收响应头时,出现超时
invalid_header  # 后端服务器返回空响应或者非法响应头
http_500   # 后端服务器返回的响应状态码为500
http_502   # 后端服务器返回的响应状态码为502
http_503   # 后端服务器返回的响应状态码为503
http_504   # 后端服务器返回的响应状态码为504
http_404   # 后端服务器返回的响应状态码为404
off        # 停止将请求发送给下一台后端服务器



 

需要理解一点的是,只有在没有向客户端发送任何数据以前,将请求转给下一台后端服务器才是可行的。也就是说,如果在传输响应到客户端时出现错误或者超时,这类错误是不可能恢复的。

范例如下:

1
2
3
http {
proxy_next_upstream http_502 http_504 http_404 error timeout invalid_header;
}






ngx_http_upstream_module模块中的server指令

1
2
3
语法: server address [parameters];
默认值:    —
上下文:    upstream



范例如下:

1
2
3
4
        upstream name {
                server 10.1.1.110:8080 max_fails=1 fail_timeout=10s;
                server 10.1.1.122:8080 max_fails=1 fail_timeout=10s;
        }


负载的配置参数详解

nginx:    192.168.4.72           #nginx安装的位置及apache位置

apache:  192.168.4.68           #要代理的两台服务器

apache:  192.168.4.69

   1:vim /usr/local/nginx/conf/nginx.conf

   user nginx nginx;                                   #这里是nginx运行的用户

   worker_processes 1;                            #设置nginx服务的worker子进程,比如设为1(一般设置为nginx所在服务器的核心数):也可以设置成 worker_processes auto;

   error_log logs/error.log;                        #去掉前面的#,记录nginx错误日志,方便检查bug:

   pid logs/nginx.pid;                                 #nginx的pid位置

events {
             worker_connections  1024;       #每个进程允许的最多连接数,
 }
# 服务器的最大连接数= 最多连接数*进程数(单台nginx的负载量大概是5w,实际生成环境下2-3w。多了搭建集群)

http {

      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;                      #日志存放位置


#这里很关键,很多小伙伴问我 “负载均衡乍配置,为啥我配置的不能访问呢“,这里的upstream就是配置负载均衡的,当然得两台以上才叫负载,我这里的ip69和68都是

#用的apache,   也许你们的是tomcat, 没关系,按这样配置一样可以,

#这里很关键,很多小伙伴问我 “负载均衡乍配置,为啥我配置的不能访问呢“,这里的upstream就是配置负载均衡的,当然得两台以上才叫负载,我这里的ip69和68都是

#原博主用的apache,   我用的是tomcat,按这样配置都可以,

 upstream proxy_test {

   server 192.168.4.69:80 weight=2;     #如果你要测试,把这里换成你自己要代理后端的ip

   server 192.168.4.68:80 weight=1;     #weight是权重 按服务器性能设置 这样设置大概三次请求两次会被分配到69 一次68

   #ip_hash;                                              # 每个请求按照访问ip(即Nginx的前置服务器或者客户端IP)的hash结果分配,这样每访客会固定访问一个后端服务器,可以解决session一致问题。

 }

这是server段的配置

server {

    listen       80;   #监听的端口 一般都为http端口

    server_name  www.test.com;    #配置域名或者ip,当服务器域名有多个时,用空格隔开:例如 server_name www.test.com  test.com;在浏览器以这个域名访问时会进入这个server 匹配不到时会进入第一个server (第一个往往return 404)

    charset utf8;


    location / {

        proxy_pass       http://proxy_test;               #这里proxy_test是上面的负载的名称,映射到代理服务器,可以是ip加端口,   或url  如果是简单的配置反向代理 不负载  就可以直接配个http://192.168.4.69:80就行了

        proxy_set_header Host      $host;

        proxy_set_header X-Real-IP $remote_addr;

        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

      }

   }

}

保存退出!

nginx平滑重启:nginx -s reload   #加载刚刚加入的配置

 后端服务器开启,在192.168.4.69和68的网页文件位置添加测试文件test.html, 内容69上: this is test 69,     68上:this is test 68,这样方便查看访问到哪台了。

      在本地配置好host,在\Windows\System32\drivers\etc\hosts              用记事本打开,在最后一行加入:192.168.4.72 www.test.com

     然后使用cmd,ping www.test.com   是否能ping通这个192.168.4.72地址,如果ok,则继续

打开浏览器用www.test.com去访问后端服务器的文件,

如: www.test.com/test.html,

    浏览器打开显示有this...说明配置ok了。

然后F5刷新一下,如果是68和69不停的切换,说明负载ok了。






猜你喜欢

转载自blog.csdn.net/liwb94/article/details/80175776
今日推荐