千家信息网

nginx如何实现负载均衡和动静分离

发表于:2024-09-22 作者:千家信息网编辑
千家信息网最后更新 2024年09月22日,这篇文章主要介绍nginx如何实现负载均衡和动静分离,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!以下是我的项目用到的一份配置文件#user nobody;worker_pro
千家信息网最后更新 2024年09月22日nginx如何实现负载均衡和动静分离

这篇文章主要介绍nginx如何实现负载均衡和动静分离,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

以下是我的项目用到的一份配置文件

#user nobody;worker_processes 4; #进程数,一般cpu是几核就写多少#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; proxy_connect_timeout 15s;  proxy_send_timeout 15s;  proxy_read_timeout 15s; fastcgi_buffers 8 128k; gzip on; client_max_body_size 30m; gzip_min_length 1k; gzip_buffers 16 64k; gzip_http_version 1.1; gzip_comp_level 6; gzip_types text/plain application/x-javascript text/css application/xml application/javascript image/jpeg image/gif image/png image/webp; gzip_vary on; #第一个集群 upstream xdx.com{ server 119.10.52.28:8081 weight=100; server 119.10.52.28:8082 weight=100;  } #第二个集群,用于上传图片所用 upstream xdxfile.com{ server 119.10.52.28:8081;#关于文件上传的请求均访问这个集群 } #第三个集群 upstream xdx8082.com{ server 119.10.52.28:8082;#8082 } #第四个集群 upstream xdxali.com{ server 139.196.235.228:8082;#阿里云 } #第五个集群 upstream xdxaliws.com{ server 139.196.235.228:8886;#阿里云websocket }#第一个代理服务器,监听的是80端口,监听的域名是www.wonyen.com或者wonyen.com server { listen 80;#监听的端口 server_name www.wonyen.com wonyen.com;#监听的域名 #charset koi8-r; #access_log logs/host.access.log main; #location指的是访问的路径,下面这条配置表示当访问网站的根目录,即访问wonyen.com或者www.wonyen.com的时候,就去根目录为html的下面去寻找index.html或者index.htm。在index.html这个页面里面你可以做一些重定向的工作,跳转到指定页面 #也可以自定义到某个集群 # location / {  # root html;  # index index.html index.htm; #} #所有静态请求都交由nginx处理,存放目录为webapps下的root,过期时间为30天  location ~ \.(css|js|gif|jpg|jpeg|png|bmp|swf|eot|svg|ttf|woff|mp3|mp4|wav|wmv|flv|f4v|json)$ {   root apache-tomcat-8.0.9-windows-x86-yipin-8081/apache-tomcat-8.0.9/webapps/ROOT;   expires 30d;  } #配置以Att结尾的请求的处理集群为http://xdxfile.com location ~ ^/\w+Att{  proxy_pass http://xdxfile.com; } #配置以Fill结尾的请求的处理集群为http://xdxfile.com location ~ ^/\w+Fill{  proxy_pass http://xdxfile.com; } #精准配置,如果请求名为/crowdFundSave,则 location = /crowdFundSave{  proxy_pass http://xdxfile.com; } #精确配置,同上 location = /crowdFundRewardSave{  proxy_pass http://xdxfile.com; } #精确配置,同上 location = /garbageCategorySave{  proxy_pass http://xdxfile.com; } #精确配置,同上 location = /mailTestAjax{  proxy_pass http://xdx8082.com; } #精确配置,同上 location = /mailSendAjax{  proxy_pass http://xdx8082.com; } #精确配置,同上 location = /mailOldAjax{  proxy_pass http://xdx8082.com; } #精确配置,同上 #location = /wechatAuthority{  #proxy_pass http://xdxali.com; #} location ~ ^/ueditor1_4_3{  proxy_pass http://xdxfile.com; } #其他所有请求都访问 http://xdx.com的集群   location ~ .*$ {  index index;  proxy_pass http://xdx.com;  } #404页面访问/Error404.jsp这个location error_page 404  /Error404.jsp; #500等页面也访问 /Error404.jsp这个location error_page 500 502 503 504 /Error404.jsp; #配置请求/Error404.jsp就访问http://xdxfile.com集群 location = /Error404.jsp {  proxy_pass http://xdxfile.com; } # 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 #另外一个代理服务器,监听8886接口,监听的域名为www.wonyen.com或者wonyen.com server { listen 8886; server_name www.wonyen.com wonyen.com;#配置若请求为wonyen.com:8086(根目录),就让他去访问http://xdxaliws.com这个集群,这边配置的是websocket的服务端 location / {  proxy_pass http://xdxaliws.com;  proxy_http_version 1.1;  proxy_set_header Upgrade $http_upgrade;  proxy_set_header Connection "upgrade"; } } # 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; # } #}}

以上就是我的一个配置。基本上需要注意的都在配置文件中注解。我把几个重要的地方单独拿出来讲一下。

1.集群的配置,我在上面的配置里有定义多个集群,集群按字面的意思理解就是由多台服务器构成的一个集合,典型的例子如

upstream xdx.com{ server 119.10.52.28:8081 weight=100; server 119.10.52.28:8082 weight=100;  }

这样的一个配置,这个集群包含了两个分支,我们可以在两台服务器上搭建相同的项目(上述的例子是在同样的服务器,不同的端口部署相同的项目,因为笔者的服务器有限),当有请求是需要这个集群来处理的时候,nginx会随机分配,当然也可以配置权重来设置两个server的访问概率。这就是负载均衡的原理。我们在多台服务器上部署相同的项目,利用nginx对请求进行转发,这样可以降低只有一台服务器所造成的的负载过大,而且当其中一台服务器挂掉以后,nginx会分配另外一台服务器来工作,这样就不会造成服务停止了。

2.server配置项代表的是一个代理服务器,上面的文件中我们配置了两个文件,分别监听wonyen.com(www.wonyen.com)这两个域名的80和8886端口,所有访问wonyen.com:80(即wonyen.com)这个域名下的请求,都按照第一个server所定义的规则去转发,而所有访问wonyen.com:8886下的请求,则会按照第二个server所定义的规则去转发。

3.我们甚至可以通过配置来处理多个域名,看以下的例子。下面的例子我配置了两个域名的规则,一个是iis服务器,一个是tomcat服务器,主要目的是为了解决80端口只能被一个程序使用的问题。如果iis用了80,tomcat就用不了,反之亦然。所以我给iis和tomcat都分配除了80以外的端口,而把80端口留给niginx。由nginx来分配请求给不同的网站。

复制代码

#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; client_max_body_size 30m; gzip_min_length 1k; gzip_buffers 16 64k; gzip_http_version 1.1; gzip_comp_level 6; gzip_types text/plain application/x-javascript text/css application/xml application/javascript image/jpeg image/gif image/png image/webp; gzip_vary on; upstream achina.com{ server 120.76.129.218:81;  } upstream qgrani.com{ server 120.76.129.218:8080;  } server { listen 80; server_name www.achinastone.com achinastone.com; #charset koi8-r; #access_log logs/host.access.log main; location / {  root html;  index index.html index.htm; }  #其他请求   location ~ .*$ {  index index;  proxy_pass http://achina.com;  } #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 80; server_name www.qgranite.com qgranite.com; location / {  root html;  index index.html index.htm; }  #所有静态请求都交由nginx处理,存放目录为webapp  location ~ \.(css|js|gif|jpg|jpeg|png|bmp|swf|eot|svg|ttf|woff|mp3|mp4|wav|wmv|flv|f4v)$ {   root apache-tomcat-8.0.9\webapps\ROOT;   expires 30d;  }  #其他请求   location ~ .*$ {  index index;  proxy_pass http://qgrani.com;  } } # 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; # } #}}

4.还有一个就是动静分离,说得通俗一点就是,把请求数据(动)与请求图片(静)分开,在tomcat里,当我们没有做动静分离的时候,tomcat把对图片的请求也会当成一个动态的请求,而处理动态请求是比较费性能的(至于为什么,我也不太清楚)。所以我们可以使用nginx配置来实现动静分离。

我的做法是把其中一个tomcat项目放在nginx的根目录下,这样,我们就可以通过以下方式来配置,实现当我们访问图片,js,css等静态资源的时候,都到一个指定的目录去访问。这样做的好处除了节省性能,还有一个就是我们不需要在所有的负载均衡服务器中都同步保留这些静态资源,只需要在一个地方保留就好了。配置如下

#所有静态请求都交由nginx处理,存放目录为webapps下的root,过期时间为30天  location ~ \.(css|js|gif|jpg|jpeg|png|bmp|swf|eot|svg|ttf|woff|mp3|mp4|wav|wmv|flv|f4v|json)$ {   root apache-tomcat-8.0.9-windows-x86-yipin-8081/apache-tomcat-8.0.9/webapps/ROOT;   expires 30d;  }

5.既然读取静态资源是从这个目录读取的,那么我们必须考虑如何存储静态资源,特别是当我们做了负载均衡以后,在我们的 项目中上传图片的请求有可能在任意一个集群的分支中被调用,比如我们的集群中有A,B两台服务器,他们都有可能做上传图片这件事情,如果A调用了上传图片这个请求,则图片则被上传到了A这台服务器上,反之就是B上面。这样势必导致A,B两台服务器上的静态图片是不同步的,当我们要访问这些图片的时候,(假设此时我们还没做动静分离)就有可能出现访问不到的情况。由于上一步我们做了动静分离,现在的问题就演变为,如何把这些A,B服务器上传的图片,都同步到我们做动静分离的那个文件夹下。人工或者程序去同步都很麻烦,我的做法是指定一台服务器(也就是nginx安装的那台服务器)的tomcat项目(也就是部署在nginx根目录下的那个tomcat项目),让它专门来负责上传图片的工作,这样所有的图片都由这个tomcat项目来上传,也就保证了静态库中的图片就是完整的图片。为此我配置了一个集群,如下。

#第二个集群,用于上传图片所用 upstream xdxfile.com{ server 119.10.52.28:8081;#关于文件上传的请求均访问这个集群 }然后在location中我这样配置: #配置以Att结尾的请求的处理集群为http://xdxfile.com location ~ ^/\w+Att{  proxy_pass http://xdxfile.com; } #配置以Fill结尾的请求的处理集群为http://xdxfile.com location ~ ^/\w+Fill{  proxy_pass http://xdxfile.com; }

因为我把所有涉及到附件上传的请求都加上了Att或者Fill的后缀,当nginx捕获这些后缀名的请求的时候,就会把他们都交给 http://xdxfile.com这个集群,也就是119.10.52.28:8081这个项目。

6.做了负载均衡以后,有一个不得不面临的问题就是内存数据的同步,我们在程序中有时候会把一些数据存放在内存中,典型的一类数据就是session。如何让session数据在集群的各个分支中共享session呢,这边要用到一个新的东西,叫做redis。

以上是"nginx如何实现负载均衡和动静分离"这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注行业资讯频道!

0