logo头像

学如逆水行舟,不进则退!!!

文章目录

简单认识Nginx---负载均衡

本文于1546天之前发表,文中内容可能已经过时。

中大型项目都会考虑到分布式

中大型项目都会考虑到分布式,前面几篇文章着重介绍了数据处理的技术集群。今天来研究一下关于服务器的负载均衡–Nginx。他除了静态资源的处理外还有可以决定将请求置于那台服务上。

Nginx的安装

点我下载

  • 下载好之后我们可以直接点击进行服务开启
友情提醒:nginx的路径不能有汉字
这里写图片描述

- 或者我们通过cmd命令来开启服务。首先到D:\Chirs\Downloads\nginx-1.11.11\nginx-1.11.11目录下执行命令

nginx

- 顺便说一下关闭命令:

nginx -s stop

- 就这么简单nginx开启了。查看是否开启成功只需要打开浏览器输入127.0.0.1或者localhost

这里写图片描述

Nginx的基本命令


- 上面已经提到了启动命令: nginx.exe

重启: nginx.exe -s reload
关闭:nginx.exe -s stop
检测配置合法性:nginx.exe -t

友情提醒:尽量在重启的时候用重启命令。有的人喜欢先关闭服务在开启服务。这样做如果修改后的配置是错误的,就会影响到nginx的使用。而如果用重启命令,就算修改后的配置错误了,仅仅影响新修改的功能,之前的服务仍然可以执行。

Nginx与Tomcat实现负载均衡

  • 在现在数万访问量的大数据时代,分布式是我们所必须要考虑的一个因素。那么nginx就可以帮助我们缓解大数据的压力。我们可以通过Nginx的反向代理将请求发送到不同的tomat,这样就大大的缓解了我们服务器的压力了。

准备两个Tomcat

  • 想要实现负载均衡我们就得有多个服务器,这样nginx才能够将请求平均分配在不同的服务上。所以这里为了掩饰效果我们只准备两个服务 器(Tomcat)。配置不同的访问端口就可以了。

nginx.conf

首先贴出一段官网的windows的配置文件。下面逐条解释。


#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;

    #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        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  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$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

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


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

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


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

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

}
  • #user nobody; : user属性在windows中不用设置,人家注释写的很清楚 nobody,但是在Linux系统中我们建议写 user nginx(用户) nginx(组) 。

  • worker_processes 1; : 工作进程 正常是CPU*2

  • error_log : nginx 的错误日志记录的文件地址

  • pid : 在windows中每个进程在后台都是有一个pid的。

这里写图片描述

  • events : 里面设置一些属性比如连接数 worker_connections
  • http : http就是nginx通过设置的http实现负载均衡的

    • include mime.types; : 设定mime类型,类型由mime.type文件定义

    • default_type application/octet-stream; : 设置默认的请求类型

    • log_format : 日志的输出格式。

    • 日志格式参数解释:

    • $remote_addr$http_x_forwarded_for用以记录客户端的ip地址;

    • $remote_user:用来记录客户端用户名称;

    • $time_local: 用来记录访问时间与时区;

    • $request: 用来记录请求的url与http协议;

    • $status: 用来记录请求状态;成功是200,

    • $body_bytes_sent :记录发送给客户端文件主体内容大小;

    • $http_referer:用来记录从那个页面链接访问过来的;

    • $http_user_agent:记录客户浏览器的相关信息;

    • sendfile on; : sendfile指令指定 nginx 是否调用sendfile 函数(zero copy 方式)来输出文件,对于普通应用,必须设为on。如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络IO处理速度,降低系统uptime。  

-  `tcp_nopush on;`: 此选项允许或禁止使用socke的TCP_CORK的选项,此选项仅在使用sendfile的时候使用   
详细   


nginx.conf修改


- 上面我们已经详细的介绍了nginx的各个参数的设置了。在配置之前我们先看看nginx初始配置http的location

这里写图片描述

- 通过上面的我们可以看出,当我们访问localhost(server_name):80(listen)的时候,nginx就会自动跳转到html文件夹中的index.html或者是index.htm的页面上了。
这里写图片描述

- 我们现在要做的就是在访问nginx是随机的跳转到我们指定的Tomcat上。相信大家都知道了,就是修改location的映射路径就行了。但是我们的映射路径是个选择器,所以我们首先构造一个选择器出来

upstream mynginxserver { server 192.168.1.183:8888 weight=2; server 192.168.1.183:8080 weight=1; }

- 其中weight是权重,就是nginx在随机选择的时候回根据这个权重去选择服务。

- 然后我们将location的映射路径映射到mynginserver上就行了。

location / { proxy_pass http://mynginxserver; }

注意: proxy_pass 后必须以http://开头。
  • 一切配置好之后我们重启nginx(nginx.exe -s reload)。这个时候我们先分别看看两个Tomcat访问的效果
    注意看路径的端口是不同的
    Tomcat1:
    这里写图片描述

Tomcat2:
这里写图片描述

然后我们这个时候在访问nginx的端口:http://192.168.1.183:802/springtests/

这里写图片描述
这里写图片描述

  • 上面的效果就是同一个请求但是请求的页面是两个页面,实际上是请求的两个Tomcat。我们实际运行中会在两个Tomcat上放置完全相同的项目。这样用户体验就是使用的同一个项目,但是我们已经实现了负载均衡了。

负载均衡的几个策略

  • 上面我们已经实现了负载均衡。nginx给我提供了关于负载均衡的策略。

  • 默认策略–轮询:

upstream mynginxserver {
        server 192.168.1.183:8888;
        server 192.168.1.183:8080;
    }

通过请求的时间顺序请求不同的Tomcat。如果某一个宕机了,则自动忽略。

  • 最少链接 : 顾名思义就是在选择的时候谁的连接数最少,就选择谁
upstream mynginxserver {
    least_conn;
        server 192.168.1.183:8888;
        server 192.168.1.183:8080;
    }
  • weight权重: 这种方式也是我上面实现负载均衡采用的方式。默认值是1 。就是在选择具体发送到哪一个Tomcat的时候是根据Tomcat的权重判断的。
upstream mynginxserver {
        server 192.168.1.183:8888 weight=2;
        server 192.168.1.183:8080 weight=1;
    }
  • ip_hash : 这个就是根据当前请求的ip,根据ip算出对应的hash值,然后在根据hash值选择对应的Tomcat。这种效果就是一台客户端至始至终访问的都是同一台Tomcat。这里的session就是一样的。
upstream mynginxserver {
    ip_hash;
        server 192.168.1.183:8888 ;
        server 192.168.1.183:8080 ;
    }
  • url_hash: 和ip_hash是一个效果
upstream mynginxserver {
    hash $request_url;
        server 192.168.1.183:8888;
        server 192.168.1.183:8080;
    }
  • fair:根据响应时间来定的。谁的响应时间短,就连谁。
upstream mynginxserver {
        server 192.168.1.183:8888 ;
        server 192.168.1.183:8080 ;
        fair;
    }

nginx地址映射

  • nginx除了作为服务器的负载均衡外,还有一个亮点就是地址映射。作为资源服务器来使用。在我们的web开发中经常需要上传资源到服务上。我们总不能将资源放在Tomcat上。这样会大大增加Tomcat的压力的。而且这样数据很容易丢失的。nginx就可以解决这个问题。

  • 其实在上面实现负载均衡的时候就已经实现了地址映射。location就是地址映射的桥梁。

location ~ ^/images/(.*) 

         # location ~ ^/images/(.*\.jpg) 

         #“.”表示任何字符,“*”表示任意数量,

         #“\.jpg”表示jpg后缀名的文件

      {

         expires 1s;

         alias D:/zxh/test/$1;   #“$1”表是location后面()的内容

         index  index.html index.htm;

         break;

      }

上面的location表示在通过server+port+^images^.^的形式就会映射到D:/zxh/test这个文件夹下。
比如我在浏览器中访问:http://192.168.1.183:802/images/test.jpg

这个时候nginx就会去访问D:/zxh/test下时候有test.jpg的图片。
这里写图片描述

这里写图片描述

以上是在网上参考别人写的整理。不喜勿喷!@       

上一篇
坚持原创技术分享,您的支持将鼓励我继续创作!

评论系统未开启,无法评论!