欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Nginx实现负载均衡

程序员文章站 2024-01-31 23:19:16
...

1.Nginx简介

Nginx是一款高性能的http 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器。由俄罗斯的程序设计师Igor Sysoev所开发,官方测试nginx能够支支撑5万并发链接,并且cpu、内存等资源消耗却非常低,运行非常稳定。

2.Nginx应用场景

1、http服务器。Nginx是一个http服务可以独立提供http服务。可以做网页静态服务器。
2、虚拟主机。可以实现在一台服务器虚拟出多个网站。例如个人网站使用的虚拟主机。
3、反向代理,负载均衡。当网站的访问量达到一定程度后,单台服务器不能满足用户的请求时,需要用多台服务器集群可以使用nginx做反向代理。并且多台服务器可以平均分担负载,不会因为某台服务器负载高宕机而某台服务器闲置的情况。
[root@server2 ~]# ls
anaconda-ks.cfg  install.log  install.log.syslog  nginx-1.14.0.tar.gz
[root@server2 ~]# tar zxf nginx-1.14.0.tar.gz 
[root@server2 ~]# ls
anaconda-ks.cfg  install.log.syslog  nginx-1.14.0.tar.gz
install.log      nginx-1.14.0
[root@server2 ~]# cd nginx-1.14.0
[root@server2 nginx-1.14.0]# ls
auto     CHANGES.ru  configure  html     man     src
CHANGES  conf        contrib    LICENSE  README
[root@server2 ~]# cd nginx-1.14.0/src/core/
[root@server2 core]# ls
nginx.c           ngx_inet.h             ngx_radix_tree.h
nginx.h           ngx_list.c             ngx_rbtree.c


.........略
[root@server2 core]# vim nginx.h
[root@server2 core]# cd ..
[root@server2 src]# cd ..
[root@server2 nginx-1.14.0]# ls
auto     CHANGES.ru  configure  html     man     src
CHANGES  conf        contrib    LICENSE  README
[root@server2 nginx-1.14.0]# cd auto/
[root@server2 auto]# ls
cc          feature       headers  install  module   options  stubs    types
define      have          include  lib      modules  os       summary  unix
endianness  have_headers  init     make     nohave   sources  threads
[root@server2 auto]# cd cc/
[root@server2 cc]# ls
acc  bcc  ccc  clang  conf  gcc  icc  msvc  name  owc  sunc
[root@server2 cc]# vim gcc 
注释掉debug如下
# debug
#CFLAGS="$CFLAGS -g"

[root@server2 cc]# cd ..
[root@server2 auto]# cd ..
[root@server2 nginx-1.14.0]# ls
auto     CHANGES.ru  configure  html     man     src
CHANGES  conf        contrib    LICENSE  README
[root@server2 nginx-1.14.0]# ./conf
conf/      configure  
[root@server2 nginx-1.14.0]# ./configure --help

 略

[root@server2 nginx-1.14.0]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio
checking for OS
 + Linux 2.6.32-431.el6.x86_64 x86_64
checking for C compiler ... not found

./configure: error: C compiler cc is not found
解决依赖性,这里一次性解决,值得注意的是,一般来说编译过程中报的依赖性错误,包名后需要加-devel    
[root@server2 nginx-1.14.0]# yum install -y gcc pcre-devel openssl-devel


Complete!
[root@server2 nginx-1.14.0]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio
略
[root@server2 nginx-1.14.0]# make
[root@server2 nginx-1.14.0]# make install
[root@server2 nginx-1.14.0]# cd /usr/local/nginx/
[root@server2 nginx]# ls
conf  html  logs  sbin
[root@server2 nginx]# du -sh
980K    .
[root@server2 nginx]# cd sbin/
[root@server2 sbin]# pwd
/usr/local/nginx/sbin
[root@server2 sbin]# ls
nginx
创建可执行软连接
[root@server2 sbin]# ln -s /usr/local/nginx/sbin/nginx /sbin/
检查语法
[root@server2 sbin]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
打开nginx
[root@server2 sbin]# nainx
-bash: nainx: command not found
[root@server2 sbin]# nginx
关闭nginx
[root@server2 sbin]# nginx -s stop
[root@server2 sbin]# nginx
重启nginx
[root@server2 sbin]# nginx -s reload
这是nginx的发布目录,网络访问页面如下

[root@server2 sbin]# curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

Nginx实现负载均衡

[root@server2 sbin]# cd /usr/local/nginx/
[root@server2 nginx]# ls
client_body_temp  fastcgi_temp  logs        sbin       uwsgi_temp
conf              html          proxy_temp  scgi_temp
[root@server2 nginx]# cd html/
[root@server2 html]# pwd
/usr/local/nginx/html
[root@server2 html]# ls
50x.html  index.html
[root@server2 html]# vim test.html
[root@server2 html]# cat test.html 
<h1>test test.html</h1>

Nginx实现负载均衡

[aaa@qq.com ~]# cd /usr/local/nginx/
[aaa@qq.com nginx]# ls
client_body_temp  fastcgi_temp  logs        sbin       uwsgi_temp
conf              html          proxy_temp  scgi_temp
[aaa@qq.com nginx]# cd conf/
[aaa@qq.com conf]# ls
fastcgi.conf            koi-win             scgi_params
fastcgi.conf.default    mime.types          scgi_params.default
fastcgi_params          mime.types.default  uwsgi_params
fastcgi_params.default  nginx.conf          uwsgi_params.default
koi-utf                 nginx.conf.default  win-utf



``查看本机cpu
[aaa@qq.com conf]# lscpu
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                1
On-line CPU(s) list:   0
Thread(s) per core:    1
Core(s) per socket:    1
Socket(s):             1
NUMA node(s):          1
Vendor ID:             GenuineIntel
CPU family:            6
Model:                 42
Stepping:              1
CPU MHz:               2491.904
BogoMIPS:              4983.80
Hypervisor vendor:     KVM
Virtualization type:   full
L1d cache:             32K
L1i cache:             32K
L2 cache:              4096K
NUMA node0 CPU(s):     0
[aaa@qq.com conf]# vim nginx.conf
[aaa@qq.com conf]# sysctl -a|grep file
fs.file-nr = 448098863
fs.file-max = 98863

[aaa@qq.com conf]# vim /etc/security/limits.conf 
[aaa@qq.com conf]# vim nginx.conf
[aaa@qq.com conf]# useradd -M -d /usr/local/nginx/ nginx     添加用户
[aaa@qq.com conf]# id nginx
uid=500(nginx) gid=500(nginx) groups=500(nginx)
[aaa@qq.com conf]# nginx -s reload
[aaa@qq.com conf]# ps ax 

[aaa@qq.com conf]# vim nginx.conf     主配置文件


user  nginx;            ##用户
#cpu worker          
worker_processes  1;




events {
    worker_connections  65535;          进程文件数
}

轮叫配置
http {  
    upstream westos {
    #ip_hash;
    server 172.25.53.3:80 weight=2;
    server 172.25.53.4:80;
    server 127.0.0.1:80 backup;
    }
    include       mime.types;
    default_type  application/octet-stream;



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


    }


    server {
        listen 80;
        server_name www.westos.org;

        location / {
        proxy_pass http://westos;
        }
    }
}
检查语法
[aaa@qq.com conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[aaa@qq.com conf]# nginx -s reload


----------


[aaa@qq.com 777]# for i in {1..10}; do curl www.westos.org; done
www.westos.org - server4 
www.westos.org - server3
www.westos.org - server4 
www.westos.org - server3
www.westos.org - server4 
www.westos.org - server3
www.westos.org - server4 
www.westos.org - server3
www.westos.org - server4 
www.westos.org - server3


----------


Done掉server3后
[aaa@qq.com 777]# for i in {1..10}; do curl www.westos.org; done
www.westos.org - server4 
www.westos.org - server4 
www.westos.org - server4 
www.westos.org - server4 
www.westos.org - server4 
www.westos.org - server4 
www.westos.org - server4 
www.westos.org - server4 
www.westos.org - server4 
www.westos.org - server4 


----------


都done掉后
[aaa@qq.com 777]# curl www.westos.org
<html>
<head><title>502 Bad Gateway</title></head>
<body bgcolor="white">
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx/</center>
</body>
</html>


----------


打开server4后
[aaa@qq.com 777]# for i in {1..10}; do curl www.westos.org; done
www.westos.org - server4 
www.westos.org - server4 
www.westos.org - server4 
www.westos.org - server4 
www.westos.org - server4 
www.westos.org - server4 
www.westos.org - server4 
www.westos.org - server4 
www.westos.org - server4 
www.westos.org - server4 


----------


添加权重后
[aaa@qq.com 777]# for i in {1..10}; do curl www.westos.org; done
www.westos.org - server3
www.westos.org - server4 
www.westos.org - server3
www.westos.org - server3
www.westos.org - server4 
www.westos.org - server3
www.westos.org - server3
www.westos.org - server4 
www.westos.org - server3
www.westos.org - server3


----------


添加
server 127.0.0.1:80 backup; 后,nginx发布自己的
[aaa@qq.com 777]# curl www.westos.org
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>


----------

添加ip_hash;
ip_hash

             每个请求按照客户端IP的hash结果分配,新的请求到达时,先将客户端IP通过哈希算法哈希出一个值,在随后的的客户端请求中,客户IP的哈希值只要相同,就会被分配至同一台服务器,该调度算法可以解决动态网页的session共享问题,但是会导致请求分配不均

  (在upstream里配置,如果是ip_hash,不能有weight和bakcup)

网页粘滞


Server3和server4只需安装httpd并打开,发布目录写东西即可


















为查看    sticky;效果,我们在另一个路径下安装nginx,停掉原来的,互不影响,只是为了执行时分清,用根目录方式执行

[aaa@qq.com ~]# ls
anaconda-ks.cfg install.log.syslog nginx-1.14.0.tar.gz
install.log nginx-1.14.0
[aaa@qq.com ~]# ls
anaconda-ks.cfg nginx-1.10.1.tar.gz nginx-sticky-module-ng.tar.gz
install.log nginx-1.14.0
install.log.syslog nginx-1.14.0.tar.gz
[aaa@qq.com ~]# nginx -s stop
[aaa@qq.com ~]# tar zxf nginx-1.10.1.tar.gz
[aaa@qq.com ~]# ls
anaconda-ks.cfg nginx-1.10.1 nginx-1.14.0.tar.gz
install.log nginx-1.10.1.tar.gz nginx-sticky-module-ng.tar.gz
install.log.syslog nginx-1.14.0
[aaa@qq.com ~]# cd nginx-1.10.1
nginx-1.10.1/ nginx-1.10.1.tar.gz
[aaa@qq.com ~]# cd nginx-1.10.1
[aaa@qq.com nginx-1.10.1]# ls
auto CHANGES.ru configure html man src
CHANGES conf contrib LICENSE README
[aaa@qq.com nginx-1.10.1]# cd
[aaa@qq.com ~]# ls
anaconda-ks.cfg nginx-1.10.1 nginx-1.14.0.tar.gz
install.log nginx-1.10.1.tar.gz nginx-sticky-module-ng.tar.gz
install.log.syslog nginx-1.14.0
[aaa@qq.com ~]# tar zxf nginx-sticky-module-ng.tar.gz
[aaa@qq.com ~]# ls
anaconda-ks.cfg nginx-1.10.1 nginx-1.14.0.tar.gz
install.log nginx-1.10.1.tar.gz nginx-sticky-module-ng
install.log.syslog nginx-1.14.0 nginx-sticky-module-ng.tar.gz
[aaa@qq.com ~]# cd nginx-1.10.1
[aaa@qq.com nginx-1.10.1]# ./configure –prefix=/opt/nginx –with-http_ssl_module –with-http_stub_status_module –with-threads –with-file-aio –add-module=/root/nginx-sticky-module-ng
[aaa@qq.com nginx-1.10.1]# make
[aaa@qq.com nginx-1.10.1]# make install
[aaa@qq.com nginx-1.10.1]# cd /opt/nginx/
[aaa@qq.com nginx]# ls
conf html logs sbin
[aaa@qq.com nginx]# cd conf/
[aaa@qq.com conf]# ls
fastcgi.conf koi-win scgi_params
fastcgi.conf.default mime.types scgi_params.default
fastcgi_params mime.types.default uwsgi_params
fastcgi_params.default nginx.conf uwsgi_params.default
koi-utf nginx.conf.default win-utf
[aaa@qq.com conf]# cp /usr/local/nginx/conf/nginx.conf .
cp: overwrite `./nginx.conf’? y
[aaa@qq.com conf]# vim nginx.conf
[aaa@qq.com conf]# ll /sbin/nginx
lrwxrwxrwx 1 root root 27 Jul 31 15:40 /sbin/nginx -> /usr/local/nginx/sbin/nginx

[aaa@qq.com conf]# /opt/nginx/sbin/nginx -t
nginx: the configuration file /opt/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx/conf/nginx.conf test is successful
[aaa@qq.com conf]# /opt/nginx/sbin/nginx
[aaa@qq.com conf]# /opt/nginx/sbin/nginx -s reload




http {
        upstream westos {
        #ip_hash;
        sticky;
        server 172.25.53.3:80 weight=2;
        server 172.25.53.4:80;
        #server 127.0.0.1:80 backup;
        }

效果为网页访问www.westos.org时,稳定在同一页面,但curl时则不是
![这里写图片描述](https://img-blog.csdn.net/20180801144533829?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2F3b3lhb2M=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)

[aaa@qq.com ~]# for i in {1..10}; do curl www.westos.org; done
www.westos.org - server4
www.westos.org - server3
www.westos.org - server3
www.westos.org - server4
www.westos.org - server3
www.westos.org - server3
www.westos.org - server4
www.westos.org - server3
www.westos.org - server3
www.westos.org - server4
“`