Nginx总结(二)——配置负载均衡和反向代理

nginx配置文件主要分为六个区域:

  1. main(全局设置)
  2. events(nginx工作模式)
  3. http(http设置)
  4. sever(主机设置)
  5. location(URL匹配)
  6. upstream(负载均衡服务器设置)

有兴趣的同学可以详细去了解一下,这里我们只用到了server,location,upstream区域。

server配置

#相当于在http模块再添加一个server模块
server {
	#监听绑定80端口
    listen       80; 
	#下面这个是域名,多个域名用空格隔开
    server_name  www.php20.com php20.com;
	#本网站的根路径
    root   /绝对路径;
	#下面是默认首页
    location / {
    	#配置默认页面
        index  index.html;
        #配置代理服务器
        proxy_pass
    }
	#下面是针对本站所有.php文件进行处理的配置
    location / {
		#加载fastcgi  一种处理方式
        include fastcgi_params;
		#fastcgi的参数 指定文件路径及参数,否则会有404或是file not find 提示
        fastcgi_param SCRIPT_FILENAME 
		#fastcgi的服务信息 ip:端口
        fastcgi_pass 127.0.0.1:9000;
		#fastcgi默认首页
        fastcgi_index index.php;
    }
}

location配置

用户可以通过location指令实现Nginx对动、静态网页的过滤处理。

  • location = /uri    =开头表示精确匹配,只有完全匹配上才能生效。
  • location ^~ /uri   ^~ 开头对URL路径进行前缀匹配,并且在正则之前。
  • location ~ pattern  ~ 开头表示区分大小写的正则匹配。
  • location ~* pattern  ~* 开头表示不区分大小写的正则匹配。
  • location /uri     不带任何修饰符,也表示前缀匹配,但是在正则匹配之后。
  • location /      通用匹配,任何未匹配到其它location的请求都会匹配到,相当于switch中的default。
location / {
	proxy_pass   http://tomcatserver;  
    # root   html;
    # index  index.html index.html;
}

upstream配置

在upstream参数中添加的应用服务器IP后添加指定参数即可实现负载均衡与分发策略。

upstream myServer {    
    server 127.0.0.1:8081;   
    server 127.0.0.1:8082;    
    server 127.0.0.1:8083;    
}

配置Nginx负载均衡和反向代理

第一步:启动三个tomcat

在本地启动三个tomcat,模拟三台服务器,记得修改端口号,否则会端口冲突启动不起来(一个tomcat涉及到三个端口,都需要修改,8080,8005,8009,只要这三个端口号和其他tomcat端口都不一样即可)。我修改的三个tomcat访问端口:8081,8082,8083。
在这里插入图片描述

第二步:创建页面区分访问的那个tomcat

在每个tomcat下,添加一个页面Test.html,这三个页面稍微可以不一样点,用户区分nginx会访问不同的tomcat。

<!DOCTYPE html>
<html>
	<head>
		<title>index</title>
		<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
	</head>
	<body>
		<h1>Tomcat——8081</h1>
	</body>
</html>

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

第三步:配置nginx

配置nginx的nginx.conf文件

upstream tomcatserver{    
    server 127.0.0.1:8081;   
    server 127.0.0.1:8082;    
    server 127.0.0.1:8083;    
}
server {
        listen       90;
        server_name  localhost;

        location / {
        	# 配置代理服务器,从上边三个tomcat中选择一个进行访问
			proxy_pass   http://tomcatserver;  
        }
}

第四步:启动nginx

不断访问 http://localhost:90/Test.html,就可以看到以下三个页面,依次出现
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

Nginx的负载分发策略

Nginx 的 upstream目前支持的分配算法:

  1. 轮询 ——1:1 轮流处理请求(默认)
    每个请求按时间顺序逐一分配到不同的应用服务器,如果应用服务器down掉,自动剔除,剩下的继续轮询。
  2. 权重 ——you can you up
    通过配置权重,指定轮询机率,权重和访问比率成正比,用于应用服务器性能不均的情况。
  3. ip_哈希算法
    每个请求按访问ip的hash结果分配,这样每个访客固定访问一个应用服务器,可以解决session共享的问题。

nginx其他配置

upstream myServer {    
	server 127.0.0.1:8081 down;   
    server 127.0.0.1:8082 weight=2;    
    server 127.0.0.1:8083;    
    server 127.0.0.1:8084 backup;   
}
  1. down
    表示单前的server暂时不参与负载
  2. Weight
    默认为1.weight越大,负载的权重就越大。
  3. max_fails
    允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误
  4. fail_timeout
    max_fails 次失败后,暂停的时间。
  5. Backup
    其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。
#定义 Nginx 运行的用户和用户组,默认由 nobody 账号运行, windows 下面可以注释掉。 
#user  nobody; 
 
#nginx进程数,建议设置为等于CPU总核心数。可以和worker_cpu_affinity配合
worker_processes  1; 
 
#全局错误日志定义类型,[ debug | info | notice | warn | error | crit ]
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
 
#进程文件,window下可以注释掉
#pid        logs/nginx.pid;
 
# 一个nginx进程打开的最多文件描述符(句柄)数目,理论值应该是最多打开文件数(系统的值ulimit -n)与nginx进程数相除,
# 但是nginx分配请求并不均匀,所以建议与ulimit -n的值保持一致。
worker_rlimit_nofile 65535;
 
#工作模式与连接数上限
events {
    # 参考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; 
    # epoll模型是Linux 2.6以上版本内核中的高性能网络I/O模型,如果跑在FreeBSD上面,就用kqueue模型。
   #use epoll;
   #connections 20000;  # 每个进程允许的最多连接数
   # 单个进程最大连接数(最大连接数=连接数*进程数)该值受系统进程最大打开文件数限制,需要使用命令ulimit -n 查看当前设置
   worker_connections 65535;
}
 
#设定http服务器
http {
    #文件扩展名与文件类型映射表
    #include 是个主模块指令,可以将配置文件拆分并引用,可以减少主配置文件的复杂度
    #include       mime.types;
    #默认文件类型
    #default_type  application/octet-stream;
    #charset utf-8; #默认编码
 
    #定义虚拟主机日志的格式
    #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函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。
    sendfile        on;
    #autoindex on; #开启目录列表访问,合适下载服务器,默认关闭。
 
    #防止网络阻塞
    #tcp_nopush     on;
 
    #长连接超时时间,单位是秒,默认为0
    keepalive_timeout  65;
 
    # gzip压缩功能设置
    gzip on; #开启gzip压缩输出
    gzip_min_length 1k; #最小压缩文件大小
    gzip_buffers    4 16k; #压缩缓冲区
    gzip_http_version 1.0; #压缩版本(默认1.1,前端如果是squid2.5请使用1.0)
    gzip_comp_level 6; #压缩等级
    #压缩类型,默认就已经包含text/html,所以下面就不用再写了,写上去也不会有问题,但是会有一个warn。
    gzip_types text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml;
    gzip_vary on; #//和http头有关系,加个vary头,给代理服务器用的,有的浏览器支持压缩,有的不支持,所以避免浪费不支持的也压缩,所以根据客户端的HTTP头来判断,是否需要压缩
    #limit_zone crawler $binary_remote_addr 10m; #开启限制IP连接数的时候需要使用
 
    # http_proxy服务全局设置
    client_max_body_size   10m;
    client_body_buffer_size   128k;
    proxy_connect_timeout   75;
    proxy_send_timeout   75;
    proxy_read_timeout   75;
    proxy_buffer_size   4k;
    proxy_buffers   4 32k;
    proxy_busy_buffers_size   64k;
    proxy_temp_file_write_size  64k;
    # proxy_temp_path   /usr/local/nginx/proxy_temp 1 2;
 
   # 设定负载均衡后台服务器列表 
    #upstream  backend.com  { 
        #ip_hash; # 指定支持的调度算法
        # upstream 的负载均衡,weight 是权重,可以根据机器配置定义权重。weigth 参数表示权值,权值越高被分配到的机率越大。
        #server   192.168.10.100:8080 max_fails=2 fail_timeout=30s ;  
        #server   192.168.10.101:8080 max_fails=2 fail_timeout=30s ;  
    #}
 
    #虚拟主机的配置
    server {
        #监听端口
        listen       80;
        #域名可以有多个,用空格隔开
        server_name  localhost fontend.com;
        # Server Side Include,通常称为服务器端嵌入
        #ssi on;
        #默认编码
        charset utf-8;
        #定义本虚拟主机的访问日志
        #access_log  logs/host.access.log  main;
        
        # 因为所有的地址都以 / 开头,所以这条规则将匹配到所有请求
        location /usr/share/nginx/ {
            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;
        }
 
       # 图片缓存时间设置
       location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$ {
          expires 10d;
       }
 
       # JS和CSS缓存时间设置
       location ~ .*.(js|css)?$ {
          expires 1h;
       }
 
        #代理配置
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #location /proxy/ {
        #    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;
    #    }
    #}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章