nginx实现负载均衡+文件服务器

这是我参与更文挑战的第26天,活动详情查看: 更文挑战php

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

Nginx的安装

点我下载nginx

  • 下载好以后咱们能够直接点击进行服务开启

友情提醒:nginx的路径不能有汉字 这里写图片描述web

  • 或者咱们经过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 -tspring

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

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*2markdown

  • 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的图片。 这里写图片描述

这里写图片描述

相关文章
相关标签/搜索