温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

nginx负载均衡如何配置

发布时间:2021-11-30 11:47:20 来源:亿速云 阅读:182 作者:iii 栏目:服务器

这篇文章主要介绍“nginx负载均衡如何配置”,在日常操作中,相信很多人在nginx负载均衡如何配置问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”nginx负载均衡如何配置”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

1、nginx以及相关模块源码安装:

我们先进行源码所需要的模块

  • -- nginx-1.13.7.tar.gz

  • -- openssl-1.1.0g.tar.gz

  • -- pcre-8.41.tar.gz

  • -- zlib-1.2.11.tar.gz

nginx负载均衡如何配置

对应下载地址:

wget http://nginx.org/download/nginx-1.13.7.tar.gz wget https://www.openssl.org/source/openssl-1.1.0g.tar.gz wget http://ftp.pcre.org/pub/pcre/pcre-8.41.tar.gz wget http://www.zlib.net/zlib-1.2.11.tar.gz

(1)解压nginx:

nginx负载均衡如何配置

(2)解压openssl:

nginx负载均衡如何配置

(3)解压pcre:

nginx负载均衡如何配置

(4)解压zlib:

nginx负载均衡如何配置

nginx负载均衡如何配置

(5)进行配置,先进入到nginx里面去,然后执行下面语句:

nginx负载均衡如何配置

./configure --prefix=/usr/local/nginx  --with-http_realip_module --with-http_addition_module --with-http_gzip_static_module --with-http_secure_link_module  --with-http_stub_status_module --with-stream --with-pcre=/home/txp/share/nginx/pcre-8.41 --with-zlib=/home/txp/share/nginx/zlib-1.2.11  --with-openssl=/home/txp/share/nginx/openssl-1.1.0g

然后直接make:

nginx负载均衡如何配置

然后接着再sudo make install:

nginx负载均衡如何配置

最终我们可以看到在/usr/local/nginx/目录下看到安装的nginx:

nginx负载均衡如何配置

现在我们可以试着来运行nginx,并进行访问(下面的访问成功):

nginx负载均衡如何配置

nginx负载均衡如何配置

这里小结一下:

很多开源软件的安装步骤大概都差不多是下面这样的套路(比如等下我们下面要安装的模块,也是这样安装的思路,所以这里就不造轮子了)

  • -- ./cofigure

  • -- make

  • --sudo make install

2、自己写conf文件

在平时的开发过程中,主要我们要去配置它的conf文件夹下的nginx.conf文件

root@ubuntu:/usr/local/nginx# ls client_body_temp  conf  fastcgi_temp  html  logs  proxy_temp  sbin  scgi_temp  uwsgi_temp

这个文件原本内容是:

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

这里内容比较多,我们现在自己来写一个配置文件来实现nginx的访问:

txp@ubuntu:/usr/local/nginx$ sudo mkdir demo_conf txp@ubuntu:/usr/local/nginx$ cd demo_conf/ txp@ubuntu:/usr/local/nginx$ vim demo_conf/demo.conf
worker_processes 4;#进程数量   events{         worker_connections 1024;#并发访问数量   } http{         server {                 listen 8888;#监听端口                 server_name localhost;#服务器名称                  client_max_body_size 100m;#访问的数量大小                  location / {                 root /usr/local/nginx/html/ #访问这个本地服务器里面的这个html页面                  }         }  }

现在我们来看一下我们自己配置的是否成功,先把之前的nginx关闭掉:

./sbin/nginx -s stop

然后再执行这个配置文件:

./sbin/nginx -c demo_conf/demo.conf

nginx负载均衡如何配置

这里扩展一下基础知识点:

Nginx  由配置文件中指定的指令控制的模块组成。指令分为简单指令和块指令。一个简单的指令由空格分隔的名称和参数组成,并以分号(;)结尾。块指令具有与简单指令相同的结构,但不是以分号结尾,而是以大括号({和})包围的一组附加指令结束。如果块指令可以在大括号内部有其他指令,则称为上下文(例如:events,http,server  和 location)。配置文件中放置在任何上下文之外的伪指令都被认为是主上下文。events 和 http 指令驻留在主上下文中,server 在 http  中的,而 location 在 http 块中。

3、负载均衡、反向代理和静态资源的访问演示:

--反向代理原理(ReverseProxy):它是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,简单来说就是真实的服务器不能直接被外部网络访问,想要访问必须通过代理,如下图所示:

nginx负载均衡如何配置

上图中有两个网关,一个是nginx应用层网关,一个路由器硬件网关,nginx和各服务器都是在同一个局域网里面;路由器它做了一个端口映射(nat)直接访问到nginx,给人的感觉nginx就在公网上面;

注意这里的服务器对外不提供服务的,通过nginx代理来向外提供服务;外面访问的是公网ip,然后通过端口映射找到nginx

现在我们用nginx做代理配置(比如说我这里用143的这台机器代理141这台机器):

worker_processes 4;   events{         worker_connections 1024;   } http{         server {                 listen 8888;                 server_name localhost;                  client_max_body_size 100m;                  location / {                 root /usr/local/nginx/html/;                 proxy_pass http://192.168.29.141;                 }         }  }

注意:141的那台机器也安装nginx了,然后当我访问143这台机器的时候,其实访问的是141这台机器的内容,这就是代理的使用了:

nginx负载均衡如何配置

--  负载均衡:从负载均衡四个字来看,肯定是用来减轻服务器的访问压力的;比如说当一台服务器的单位时间内的访问量越大时,服务器压力就越大,大到超过自身承受能力时,服务器就会崩溃(比如每年双十一活动,淘宝就使用了nginx的负载均衡的功能,不然当天那么多的用户活跃在淘宝上,服务器肯定吃不消啊!)。因此为了避免服务器崩溃,让用户有更好的体验,我们通过负载均衡的方式来分担服务器压力。我们可以建立很多很多服务器,组成一个服务器集群,当用户访问网站时,先访问一个中间服务器(也就是我们的nginx),在让这个中间服务器在服务器集群中选择一个压力较小的服务器,然后将该访问请求引入该服务器。如此以来,用户的每次访问,都会保证服务器集群中的每个服务器压力趋于平衡,分担了服务器压力,避免了服务器崩溃的情况。

下面我演示负载均衡案例:

worker_processes 4;   events{         worker_connections 1024;   } http{         upstream backend{                 server 192.168.29.142 weight=2;//weight表示权重                 server 192.168.29.141 weight=1;         }         server {                 listen 8888;                 server_name localhost;                  client_max_body_size 100m;                  location / {              #   root /usr/local/nginx/html/;         #       proxy_pass http://192.168.29.141;                 proxy_pass http://backend;                 }         }  }

注意:权重表示被访问的更多,这里由于我三台机器都安装了nginx,所以内容显示看不出什么不同之处来,其实142的机器被访问了2次,141的机器被访问了1次,我这里有三台机器:141、142、143:

nginx负载均衡如何配置

nginx负载均衡如何配置

-- 访问静态资源(图片和视频)

这里我在143的机器上放了几张图片,然后在/usr/local/nginx目录下创建了一个images文件夹,然后把143机器上的图片copy到images下面来:

root@ubuntu:/usr/local/nginx# ls client_body_temp  fastcgi_temp  images  proxy_temp  scgi_temp   vip_conf conf              html          logs    sbin        uwsgi_temp  root@ubuntu:/usr/local/nginx# mv /home/txp/share/nginx/*.png images/ root@ubuntu:/usr/local/nginx# ls client_body_temp  fastcgi_temp  images  proxy_temp  scgi_temp   vip_conf conf              html          logs    sbin        uwsgi_temp root@ubuntu:/usr/local/nginx# cd images/ root@ubuntu:/usr/local/nginx/images# ls 1.png  2.png  3.png

然后进行conf文件配置:

worker_processes 4;   events{         worker_connections 1024;   } http{         upstream backend{                 server 192.168.29.142 weight=2;                 server 192.168.29.141 weight=1;         }         server {                 listen 8888;                 server_name localhost;                  client_max_body_size 100m;                  location / {              #   root /usr/local/nginx/html/;         #       proxy_pass http://192.168.29.141;                 proxy_pass http://backend;                 }                 location /images/ {                 root /usr/local/nginx/;                 }          } }

实现结果如下:

nginx负载均衡如何配置

下面我在演示一下视频访问,同样,我创建一个media目录,然后把143机器上的test.mp4copy到media目录来:

root@ubuntu:/usr/local/nginx# mv /home/txp/share/nginx/test.mp4  media/ root@ubuntu:/usr/local/nginx# cd media/ root@ubuntu:/usr/local/nginx/media# ls test.mp4

conf文件配置:

worker_processes 4;   events{         worker_connections 1024;   } http{         upstream backend{                 server 192.168.29.142 weight=2;                 server 192.168.29.141 weight=1;         }         server {                 listen 8888;                 server_name localhost;                  client_max_body_size 100m;                  location / {              #   root /usr/local/nginx/html/;         #       proxy_pass http://192.168.29.141;                 proxy_pass http://backend;                 }                 location /images/ {                         root /usr/local/nginx/;                 }                 location ~\.(mp3|mp4) #这里利用了正则表达式                         root /usr/local/nginx/media/;                 }          } }

结果如下:

nginx负载均衡如何配置

到此,关于“nginx负载均衡如何配置”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI