【Nginx】负载均衡、反向代理、动静分离配置(详细)

环境说明:

Centos 7版本

主机名

IP地址

nginx

192.168.10.150

nginx-2

192.168.10.151

nginx-3

192.168.10.152

nginx-4

192.168.10.153

准备Nginx的页面内容,方便观察

#nginx-2、Nginx-3和Nginx-4的Nginx的页面内容
[root@nginx-2 ~]# vim /usr/local/nginx/html/index.html 
This is 192.168.10.151
[root@nginx-3 ~]# vim /usr/local/nginx/html/index.html 
This is 192.168.10.152
[root@nginx-4 ~]# vim /usr/local/nginx/html/index.html 
This is 192.168.10.153

一、反向代理配置

[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
#proxy_pass就会把请求转发
        proxy_pass http://192.168.10.151;
#有proxy_pass,就不会访问下面的root
            #root   html;
            #index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
[root@nginx ~]# systemctl restart nginx

#验证

如果改为proxy_pass http://www.baidu.com;结果如下:

#清楚浏览器缓存,并且改为baidu.com形式,访问的是www.bai.com,也会有重定向,访问百度时,可能是因为https模式,就会有重定向,如果只是改百度的ip就会是http模式,结果如下:

二、负载均衡配置

  1. 基于轮询

[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
#upstream用于配置负载均衡后端服务器的地址,合成一个组,会有一个名称,proxy_pass就会把请求转发到这个组
    upstream backend {
      server 192.168.10.151:80;
      server 192.168.10.152:80;
}    
    server {
        listen       80;
        server_name  localhost;
        location / {
        proxy_pass http://backend;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
[root@nginx ~]# systemctl restart nginx

#验证

#不断刷新地址

  1. 基于权重

#在轮询的基础上加上weight设置就行

[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    upstream backend {
      server 192.168.10.151:80 weight=8;
      server 192.168.10.152:80 weight=6;
      server 192.168.10.153:80 weight=4;
}    
    server {
        listen       80;
        server_name  localhost;
        location / {
        proxy_pass http://backend;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
[root@nginx ~]# systemctl restart nginx

#验证

down参数

#表示服务器下线,不再参与

[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    upstream backend {
      server 192.168.10.151:80 weight=8 down;
      server 192.168.10.152:80 weight=6;
      server 192.168.10.153:80 weight=4;
}    
    server {
        listen       80;
        server_name  localhost;
        location / {
        proxy_pass http://backend;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
[root@nginx ~]# systemctl restart nginx

#验证

backup参数

#标记一个服务器为备用服务器,当主服务器不可用时,Nginx会自动将请求到备用服务器上

[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    upstream backend {
      server 192.168.10.151:80 weight=8 down;
      server 192.168.10.152:80 weight=6;
      server 192.168.10.153:80 weight=4 backup;
}    
    server {
        listen       80;
        server_name  localhost;
        location / {
        proxy_pass http://backend;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
[root@nginx ~]# systemctl restart nginx

#验证

  1. ip_hash-->会话保持

#客服端的IP地址hash处理,结果的值一样转发后端同一台服务器,用于保持会话。用法:

upstream backend {
    ip_hash;    
    server backend1.example.com;
    server backend2.example.com;
}
  1. least_conn

#最小连接数,选择后端服务器连接数最少的进行转发。

upstream backend {   
    least_conn;   
    server backend1.example.com;
    server backend2.example.com;
}
  1. fair(第三方模块)

#根据后端响应时间分配请求,优先分配响应时间短,需要下载nginx-upstream-fair

upstream backend {
    fair;    
    server backend1.example.com;
    server backend2.example.com;
}
  1. url_hash-->会话保持

#根据uri,也就是域名与IP地址后面部分,如果一样,则转发后端同台服务器;

upstream backend {
    hash $request_uri;   
    server backend1.example.com;
    server backend2.example.com;
}
  1. cookie_jsessionid-->会话保持

#一般后端Tomcat会给客服端一个cookie,名称叫jsessionid,根据cookie是否一致,一致则转发同台服务器,如果后端是Nginx服务器,则需要第三方模块,分发cookie,如sticky模块

upstream backend {
    hash $cookie_jsessionid; 
    server backend1.example.com;
    server backend2.example.com;
}
  1. sticky模块(第三方模块)-->会话保持

#基于cookie的,分发和识别cookie,一样则转发同台服务器,需要下载Nginx-sticky-module模块,下载地址nginx-goodies / nginx-sticky-module-ng / Downloads — Bitbucket,默认标识名为route,有过期时间。

[root@nginx ~]# wget https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/get/c78b7dd79d0d099e359c5c4394d13c9317b9348f.tar.gz
[root@nginx ~]# tar -xzvf c78b7dd79d0d099e359c5c4394d13c9317b9348f.tar.gz 
#重新编译Nginx
[root@nginx ~]# cd nginx-1.22.1
[root@nginx nginx-1.22.1]# ./configure --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_gunzip_module --with-http_gzip_static_module --add-module=/root/nginx-goodies-nginx-sticky-module-ng-c78b7dd79d0d/
[root@nginx nginx-1.22.1]# make
[root@nginx nginx-1.22.1]# make install
[root@nginx ~]# systemctl restart nginx
1、如果编译出现了下面操作:
./configure: error: invalid option "--add-moudle=/root/nginx-goodies-nginx-sticky-module-ng-c78b7dd79d0d/"
解决方案:
[root@nginx nginx-1.22.1]#yum -y install gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel --setopt=protected_multilib=false
[root@nginx nginx-1.22.1]# ./configure --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_gunzip_module --with-http_gzip_static_module --add-module=/root/nginx-goodies-nginx-sticky-module-ng-c78b7dd79d0d/
2、如果遇到定义问题,是由于版本过老
解决方案:
[root@nginx ~]# vim nginx-goodies-nginx-sticky-module-ng-c78b7dd79d0d/ngx_http_sticky_misc.c
添加头文件
#include <openssl/sha.h>
#include <openssl/md5.h>

#会自动备份以前的版本,如下

#可以看到Nginx编译一些信息,如下

用法:

#可以是sticky name=名称来修该名称;

upstream backend {
    sticky;
    server backend1.example.com;
    server backend2.example.com;
}
[root@nginx ~]# systemctl restart nginx

#验证,结果如下

#如果cookie没过期,再打开一个页面,会跟后面的服务器一直保持会话。

三、动静分离

#后端

[root@nginx-2 ~]# vim /usr/local/nginx/html/index.html 
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>My Website</title>
    <link rel="stylesheet" href="css/style.css">
</head>
<body>
    <h1>Hello World!</h1>
    <p>Welcome to my website. This is 192.168.10.151.</p>
    <button id="myButton">Click me!</button>
    <script src="js/script.js"></script>
</body>
</html>

#访问该页面结果如下:

#把css,js文件静态资源写在前端,根据prox_pass把请求转发后端处理

#css文件

[root@nginx ~]# cd /usr/local/nginx/html/
[root@nginx html]# mkdir css
[root@nginx html]# cd css
[root@nginx css]# vim style.css
h1 {
    color: blue;
}
p {
    font-size: 18px;
}
button {
    background-color: green;
    color: white;
    padding: 10px;
    border: none;
    border-radius: 5px;
    cursor: pointer;
}

#js文件

[root@nginx css]# cd ..
[root@nginx html]# mkdir js
[root@nginx html]# cd js
[root@nginx js]# vim script.js
document.getElementById("myButton").addEventListener("click", function() {
    alert("You clicked the button!");
});
  • 配置nginx.conf文件

[root@nginx html]# vim /usr/local/nginx/conf/nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
        proxy_pass http://192.168.10.151;
        }
        location /css {
            root   html;
            index  index.html index.htm;
        }
        location /js {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
[root@nginx html]# systemctl restart nginx

#验证

localtion匹配优先级

  1. 精确匹配 =

  1. 前缀匹配 ^~

  1. 正则匹配:~、~*

  1. 不带任何修饰符的前缀匹配

可以根据正则匹配规则把刚刚的写配置文件里的location /css{} 和 location /js {} 写成一个location ~*/(js|css) {}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
nginx是一款强大的开源Web服务器软件,除了支持常规的Web服务器功能外,还可以用作正向代理、反向代理负载均衡以及动静分离配置。 正向代理是指客户端通过代理服务器发送请求,然后由代理服务器转发到目标服务器,从而隐藏客户端的真实身份。在nginx中,可以通过配置修改proxy_pass指令来实现正向代理功能。 反向代理是指客户端发送请求到代理服务器,代理服务器再将请求转发到后端的服务器,然后将后端服务器的响应返回给客户端,客户端无法直接访问到后端服务器。在nginx中,可以通过修改proxy_pass指令来实现反向代理,并且可以使用负载均衡来分配请求到多个后端服务器负载均衡是指将请求分发到多个服务器上,以达到均衡负载的目的。在nginx中,可以使用upstream模块配置多个后端服务器,并通过配置proxy_pass和proxy_redirect实现请求的负载均衡nginx支持多种负载均衡算法,如轮询、IP哈希等。 动静分离是指将动态资源和静态资源分别部署在不同的服务器上,以提高系统性能和并发能力。在nginx中,可以通过配置location指令将动态请求和静态请求分别代理到不同的后端服务器或直接返回静态文件,从而实现动静分离。 总结来说,nginx通过配置实现了正向代理、反向代理负载均衡动静分离等功能,在提供Web服务的同时,能够提高系统的性能、并发能力和安全性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

维运

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值