Nginx   发布时间:2019-11-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Nginx系列二:(Nginx Rewrite 规则、Nginx 防盗链、Nginx 动静分离、Nginx+keepalived 实现高可用)大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

一、Nginx Rewrite 规则

1. Nginx rewrite规则

Rewrite规则含义就是某个URL重写成特定的URL(类似于Redirect),从某种意义上说为了美观或者对搜索引擎友好,提高收录量及排名等。

LPADding="1" align="left">Rewrite规则的flag标记主要有以下几种: 1) last : 相当于Apache里的(L)标记,表示完成rewrite;2) break: 本条规则匹配完成后,终止匹配,不再匹配后面的规则 3) redirect: 返回302临时重定向,浏览器地址会显示跳转后的URL地址 4) peRMANent:返回301永久重定向,浏览器地址栏会显示跳转后的URL地址

2. Nginx rewrite例子

a) 例如用户访问www.dbspread.com,想直接跳转到网站下面的某个页面,www.dbspread.com/new.index.html如何来实现呢?   我们可以使用Nginx Rewrite 来实现这个需求,具体如下:在server中加入如下语句即可:

{ listen .jsp .html usrlocalnginx#监听完成以后通过斜杆(</span><span style="color: #808080"&gt;/</span><span style="color: #000000"&gt;)拦截请求转发到后端的tomcat服务器 LOCATIOn </span><span style="color: #808080"&gt;/</span><span style="color: #000000"&gt; { #如果后端的服务器返回502、</span><span style="color: #800000; font-weight: bold"&gt;504</span><span style="color: #000000"&gt;、执行超时等错误,自动将请求转发到upstream负载均衡池中的另一台服务器,实现@R_340_10772@。 proxy_next_upstream http_502 http_504 error timeout invalid_header; proxy_set_header Host $host; #获取客户端的主机名存到变量Host里面,从而让tomcat取到客户端机器的信息 proxy_set_header X</span><span style="color: #808080"&gt;-</span><span style="color: #0000ff"&gt;Real</span><span style="color: #808080"&gt;-</span>IP $remote_addr; #获取客户端的主机名存到变量X<span style="color: #808080"&gt;-</span><span style="color: #0000ff"&gt;Real</span><span style="color: #808080"&gt;-</span><span style="color: #000000"&gt;IP里面,从而让tomcat取到客户端机器的信息 proxy_set_header X</span><span style="color: #808080"&gt;-</span>ForWARDed<span style="color: #808080"&gt;-</span><span style="color: #0000ff"&gt;For</span><span style="color: #000000"&gt; $proxy_add_x_forWARDed_for; <strong><span style="color: #ff0000"&gt;rewrite </span></strong></span><strong><span style="color: #ff0000"&gt;^/$ http://www.dbspread.com/new.index.html peRMANent</span></strong><span style="color: #000000"&gt;<strong><span style="color: #ff0000"&gt;;</span></strong> proxy_pass http:</span><span style="color: #808080"&gt;//</span><span style="color: #000000"&gt;web1; #跳转到对应的应用web1 }

}

效果图如下:

title="Nginx系列二:(Nginx Rewrite 规则、Nginx 防盗链、Nginx 动静分离、Nginx+keepalived 实现高可用)" alt="Nginx系列二:(Nginx Rewrite 规则、Nginx 防盗链、Nginx 动静分离、Nginx+keepalived 实现高可用)" src="https://cn.js-code.com/res/2019/02-10/10/14b200d234457e39e2a2f97daa721712.png" >

rewrite $ http: RMANentregex replacement

正则表达式说明:

代表前面0或更多个字符 1个字符

b)例如多个域名跳转到同一个域名,nginx rewrite规则写法如下:

{ listen .jsp .html usrlocalnginx</span><span style="color: #0000ff"&gt;if</span> ($host <span style="color: #808080"&gt;!=</span> <span style="color: #ff0000"&gt;'</span><span style="color: #ff0000"&gt;www.dbspread.com</span><span style="color: #ff0000"&gt;'</span><span style="color: #000000"&gt; ){ rewrite </span><span style="color: #808080"&gt;^/</span>(.<span style="color: #808080"&gt;*</span>)$ http:<span style="color: #808080"&gt;//</span>www.dbspread.com<span style="color: #808080"&gt;/</span>$<span style="color: #800000; font-weight: bold"&gt;1</span> <span style="color: #0000ff"&gt;peRMANent</span><span style="color: #000000"&gt;; }

}

格式: rewrite [flag]; 关键字     正则          替代内容 flag标记说明: rewrite为固定关键字,表示开始进行rewrite匹配规则、regex部分是 ^/(.*) ,这是一个正则表达式,匹配完整的域名和后面的路径地址replacement部分是http://www.dbspread.com/$1,$1是取自regex部分( )里的内容。匹配成功后跳转到的URL。flag部分 peRMANent表示永久301重定向标记,即跳转到新的 http://www.dbspread.com/$1 地址上

二、Nginx 防盗链

1. 什么是防盗链

比如http://www.dbspread.com/download/av123.rmvb 这个视频下载地址被其他网站引用,比如在www.test.com的index.html引用download/av123.rmvb就叫盗链,我们要禁止这种引用就叫做防盗链

2. 怎么实现防盗链

在Nginx的nginx.conf的server里面配置如下代码

{ listen 80; server_name www.dbspread.com *.dbspread.com; LOCATIOn ~* \.(rmvb|jpg|png|swf|flv)$ { #rmvb|jpg|png|swf|flv表示对rmvb|jpg|png|swf|flv后缀的文件实行防盗链 valid_referers none blocked www.dbspread.com; #表示对www.dbspread.com此域名开通白名单,比如在www.test.com的index.html引用download/av123.rmvb,无效 root html/b; if ($invalid_referer) { #如果请求不是从www.dbspread.com白名单发出来的请求,直接重定向到403.html这个页面或者返回403 #rewrite ^/ http://www.dbspread.com/403.html; return 403; } }
}</span></pre>

三、Nginx 动静分离

1. 动静分离是什么

Nginx动静分离是让动态网站里的动态网页根据一定规则把不变的资源和经常变的资源区分开来,动静资源做好了拆分以后,我们就可以根据静态资源的特点将其做缓存操作,这就是网站静态化处理的核心思路。

2. 动静分离原理图

title="Nginx系列二:(Nginx Rewrite 规则、Nginx 防盗链、Nginx 动静分离、Nginx+keepalived 实现高可用)" alt="Nginx系列二:(Nginx Rewrite 规则、Nginx 防盗链、Nginx 动静分离、Nginx+keepalived 实现高可用)" src="https://cn.js-code.com/res/2019/02-10/10/21daeeda752a3db11660e8dd8a3dcd32.png" >

3. Nginx动静分离应该注意的地方

1). WEB项目开发时要注意,将静态资源尽量放在一个static文件夹2). 将static静态资源文件夹放到Nginx可以取到的位置3). 页面要建立全局变量路径,方便修改路径4). 修改nginx.conf的LOCATIOn, 匹配静态资源请求

4. Nginx动静分离步骤

4.1 准备一个静态资源button.css

{:;:;:;BACkground-color:; }

4.2  在/var/local下新建一个static文件夹用来存放静态资源button.css

title="Nginx系列二:(Nginx Rewrite 规则、Nginx 防盗链、Nginx 动静分离、Nginx+keepalived 实现高可用)" alt="Nginx系列二:(Nginx Rewrite 规则、Nginx 防盗链、Nginx 动静分离、Nginx+keepalived 实现高可用)" src="https://cn.js-code.com/res/2019/02-10/10/708e52e716182a165c55328be42008fa.png" >

4.3 在tomcat-8080/webapps/ROOT下的index.html里面引入button.css

http-equiv titletesttitle 欢迎来到8080端口tomcat

<span style="color: #0000ff"></<span style="color: #800000">body<span style="color: #0000ff">>
<span style="color: #0000ff"></<span style="color: #800000">html<span style="color: #0000ff">>

 4.4 在Nginx的nginx.conf中server节点新增静态资源分离的配置

{ listen 80; #监听80端口 server_name www.dbspread.com; #域名 #rewrite规则 index index.jsp index.html index.htm; root /usr/local/nginx/html; #定义服务器的默认网站根目录位置 #重定向 if ($host != 'www.dbspread.com' ){ rewrite ^/(.*)$ http://www.dbspread.com/$1 peRMANent; }
#防盗链
 LOCATIOn ~* \.(rmvb|jpg|png|swf|flv)$ { #rmvb|jpg|png|swf|flv表示对rmvb|jpg|png|swf|flv后缀的文件实行防盗链
            valid_referers none blocked  www.dbspread.com; #表示对www.dbspread.com此域名开通白名单,比如在www.test.com的index.html引用download/av123.rmvb,无效
            root   html/b;
            if ($invalid_referer) { #如果请求不是从www.dbspread.com白名单发出来的请求,直接重定向到403.html这个页面或者返回403 
                 #rewrite ^/ http://www.dbspread.com/403.html;
                 return 403;
            }
    }

#监听完成以后通过斜杆(/)拦截请求转发到后端的tomcat服务器
LOCATIOn / 
    {
        #如果后端的服务器返回502、504、执行超时等错误,自动将请求转发到upstream负载均衡池中的另一台服务器,实现@R_340_10772@。
        proxy_next_upstream http_502 http_504 error timeout invalid_header;
        proxy_set_header Host  $host; #获取客户端的主机名存到变量Host里面,从而让tomcat取到客户端机器的信息
        proxy_set_header X-Real-IP $remote_addr; #获取客户端的主机名存到变量X-Real-IP里面,从而让tomcat取到客户端机器的信息
        proxy_set_header X-ForWARDed-For $proxy_add_x_forWARDed_for;
        #rewrite     ^/$    http://www.dbspread.com/new.index.html  peRMANent;#用户访问www.dbspread.com,想直接跳转到网站下面的某个页面:www.dbspread.com/new.index.html
        proxy_pass http://web1; #跳转到对应的应用web1
    }

   # LOCATIOn ~ .*\.(php|jsp|cgi|shtml)?$ #动态分离 ~匹配 以.*结尾(以php JSP结尾走这段)
   #  {
   #     proxy_set_header Host  $host;
   #        proxy_set_header X-Real-IP $remote_addr;
   #        proxy_set_header X-ForWARDed-For $proxy_add_x_forWARDed_for;
   #        proxy_pass http://jvm_web2;
   # }

    <strong><span style="color: #ff0000"&gt;#静态分离 ~匹配 以.*结尾(以html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css结尾走这段),当然不是越久越好,如果有10000个用户在线,都保存几个月,系统托跨
    LOCATIOn ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ 
    {
        root /var/local/static; #静态资源存放在Nginx的安装机器上
        #proxy_pass http://www.static.com; #静态资源也可存放在远程服务器上
        expires    30d; #30天之内只要访问过一次就从缓存拿
    }</span></strong>

    #日志级别有[debug|info|notice|warn|error|crit]  error_log 级别分为 debug,info,notice,warn,error,crit  默认为crit,生产环境用error 
    #crit 记录的日志最少,而debug记录的日志最多
    access_log  /usr/local/logs/web2/access.log main;
    error_log   /usr/local/logs/web2/error.log  crit;

}</span></pre>

4.5 访问页面查看效果

title="Nginx系列二:(Nginx Rewrite 规则、Nginx 防盗链、Nginx 动静分离、Nginx+keepalived 实现高可用)" alt="Nginx系列二:(Nginx Rewrite 规则、Nginx 防盗链、Nginx 动静分离、Nginx+keepalived 实现高可用)" src="https://cn.js-code.com/res/2019/02-10/10/ab4f2abc6b14a4b796b658f5b45db182.png" >

四、Nginx+keepalived  实现高可用

1. keepalived是什么

Keepalived软件起初是专为LVS负载均衡软件设计的,用来管理并监控LVS集群系统中各个服务节点的状态,后来又加入了可以实现高可用的VRRP (Virtual Router Redundancy Protocol,虚拟路由器冗余协议)功能。因此,Keepalived除了能够管理LVS软件外,还可以作为其他服务(例如:Nginx、Haproxy、MySQL等)的高可用解决方案软件

2. keepalived主要功能

管理LVS负载均衡软件实现LVS集群节点的健康检查作为系统网络服务的高可用性(failover)

3. keepalived@R_340_10772@

Keepalived高可用服务之间的故障切换转移,是通过 VRRP 来实现的。在 Keepalived服务正常工作时,主 Master节点会不断地向备节点发送(多播的方式)心跳消息,用以告诉备BACkup节点自己还活着,当主 Master节点发生故障时,就无法发送心跳消息,备节点也就因此无法继续检测到来自主 Master节点的心跳了,于是调用自身的接管程序,接管主Master节点的 IP资源及服务。而当主 Master节点恢复时,备BACkup节点又会释放主节点故障时自身接管的IP资源及服务,恢复到原来的备用角色。

说明:keepalived的主从切换和redis的主从切换是不一样的,keepalived的主节点挂了以后,从节点变为主节点,之前的主节点恢复以后继续做主节点。redis的主节点挂了以后,重新恢复以后变为从节点

4. keepalived高可用架构示意图

title="Nginx系列二:(Nginx Rewrite 规则、Nginx 防盗链、Nginx 动静分离、Nginx+keepalived 实现高可用)" alt="Nginx系列二:(Nginx Rewrite 规则、Nginx 防盗链、Nginx 动静分离、Nginx+keepalived 实现高可用)" src="https://cn.js-code.com/res/2019/02-10/10/8fa4559dd7a1b4befc08db17d68ce456.png" >

说明:

虚拟ip(VIp):192.168.152.200,对外提供服务的ip,也可称作浮动ip192.168.152.130:nginx + keepalived master 主192.168.152.129:nginx + keepalived BACkup 从192.168.152.129:tomcat-8080192.168.152.129:tomcat-8081

5. keepalived安装

环境准备:

centos6、jdk

虚拟ip(VIp):192.168.152.200,对外提供服务的ip,也可称作浮动ip192.168.152.130:nginx + keepalived master 主192.168.152.129:nginx + keepalived BACkup 从192.168.152.129:tomcat-8080192.168.152.129:tomcat-8081

nginx和tomcat的环境准备请查看我的前一篇关于nginx的文章

5.1 安装keepalived的步骤:

注:192.168.152.129(keepalived从节点) 与 192.168.152.130(keepalived主节点)先安装好nginx + keepalived

下载压缩包:

wget www.keepalived.org/software/keepalived-1.3.5.tar.gz

解压缩:

tar -zxvf keepalived-1.3.5.tar.gz

进入解压缩以后的文件目录:

cd keepalived-1.3.5

编译安装:./configure --prefix=/usr/local/keepalived系统提示警告 *** WARNING - this build will not support IPVS with IPv6. Please install libnl/libnl-3 dev libraries to support IPv6 with IPVs.yum -y install libnl libnl-devel再次执行./configure --prefix=/usr/local/keepalived系统提示错误 configure: error: libnfnetlink headers missingyum install -y libnfnetlink-devel再次执行./configure --prefix=/usr/local/keepalived

 make && make install

到此keepalived安装完成,

Configuration file '/etc/keepalived/keepalived.conf' is not a regular non-executable file

title="Nginx系列二:(Nginx Rewrite 规则、Nginx 防盗链、Nginx 动静分离、Nginx+keepalived 实现高可用)" alt="Nginx系列二:(Nginx Rewrite 规则、Nginx 防盗链、Nginx 动静分离、Nginx+keepalived 实现高可用)" src="https://cn.js-code.com/res/2019/02-10/10/f9bb8039aaaaa3f3042d2951f802c51b.png" >

安装完成后,进入安装目录的etc目录下,将keepalived相应的配置文件拷贝到系统相应的目录当中。keepalived启动时会从/etc/keepalived目录下查找keepalived.conf配置文件

@H_763_4@mkdir /etc/keepalived

cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived

5.2 修改keepalived主节点192.168.152.130的/etc/keepalived/keepalived.conf配置文件

{ notification_email { leesmalllvshttp_port { script "usrlocalsrccheck_nginx_pid.sh" #心跳执行的脚本,检测nginx是否启动 interval { state MASTER # 指定keepalived的角色,MASTER为主,BACKUP为备 interface eth0 # 当前进行vrrp通讯的网络接口卡(当前centos的网卡) 用ifconfig查看你具体的网卡 virtual_router_id 默认为1s(vrrp组播周期秒数) #授权访问 authentication { auth_type PASS #设置验证类型和密码,MASTER和BACKUP必须使用相同的密码才能正常通信 auth_pass { chk_http_port #(调用检测脚本) } virtual_ipaddress { .p),可多设,每行一个 } }

5.3 修改keepalived从节点192.168.152.129的/etc/keepalived/keepalived.conf配置文件

{ notification_email { leesmalllvsBACkup # 设置nginx BACkup的id,在一个网络应该是唯一的 } #检测脚本 vrrp_script chk_http_port { script "usrlocalsrccheck_nginx_pid.sh" #心跳执行的脚本,检测nginx是否启动 interval { state BACKUPBACKUP为备 interface eth0 # 当前进行vrrp通讯的网络接口卡(当前centos的网卡) 用ifconfig查看你具体的网卡 virtual_router_id 默认为1s(vrrp组播周期秒数) #授权访问 authentication { auth_type PASS #设置验证类型和密码,MASTER和BACKUP必须使用相同的密码才能正常通信 auth_pass { chk_http_port #(调用检测脚本) } virtual_ipaddress { .p),可多设,每行一个 } }

5.4 检查nginx是否启动的sHell脚本

/usr/local/src/check_nginx_pid.sh

#!binx是否启动了 A`ps C nginx ;usrlocalnginxsbin ;

5.5 192.168.152.130(keepalived主节点)和 192.168.152.129(keepalived从节点)的nginx的配置文件nginx.conf

你的电脑CPU数,一般8个 worker_cpu_affinity usrlocalnginxlogs工作模式及连接数上限

events
{
<span style="color: #0000ff">use epoll; #多路复用IO 基于LINUX2.6以上内核,可以大大提高NGINX的性能 uname <span style="color: #808080">-<span style="color: #000000">a查看内核版本号
worker_connections <span style="color: #800000; font-weight: bold">102400; #单个worker process最大连接数,其中NGINX最大连接数=连接数<span style="color: #808080">*<span style="color: #000000">进程数,一般1GB内存的机器上可以打开的最大数大约是10万左右
multi_accept <span style="color: #0000ff">on<span style="color: #000000">; #尽可能多的接受请求,默认是关闭状态
}

处理http请求的一个应用配置段

http
{

引用mime.types,这个类型定义了很多,当web服务器收到静态的资源文件请求时,依据请求文件的后缀名在服务器的MIME配置文件中找到对应的MIME #Type,根据MIMETYPE设置并response响应类型(Content<span style="color: #808080">-<span style="color: #000000">type)

include mime.types;
default_type application<span style="color: #808080">/octet<span style="color: #808080">-<span style="color: #000000">stream; #定义的数据流,有的时候默认类型可以指定为text,这跟我们的网页发布还是资源下载是有关系的
fastcgi_intercept_errors <span style="color: #0000ff">on; #表示接收fastcgi输出的http <span style="color: #800000; font-weight: bold">1.0<span style="color: #000000"> response code
charset utf<span style="color: #808080">-<span style="color: #800000; font-weight: bold">8<span style="color: #000000">;
server_names_hash_bucket_size <span style="color: #800000; font-weight: bold">128<span style="color: #000000">; #保存服务器名字的hash表

用来缓存请求头信息的,容量4K,如果header头信息请求超过了,nginx会直接返回400错误,先根据client_header_buffer_size配置的值分配一个buffer,如果##分配的buffer无法容纳request_line<span style="color: #808080">/request_header,那么就会##再次根据large_client_header_buffers配置的参数分配large_buffer,如果large_buffer还是无#法容纳,那么就会返回414(处理request_line)<span style="color: #808080">/<span style="color: #800000; font-weight: bold">400<span style="color: #000000">(处理request_header)错误。

client_header_buffer_size 4k;
large_client_header_buffers <span style="color: #800000; font-weight: bold">4<span style="color: #000000"> 32k;
client_max_body_size 300m; #允许客户端请求的最大单文件字节数 上传文件时根据需求设置这个参数

指定NGINx是否调用这个函数来输出文件,对于普通的文件我们必须设置为ON,如果NGINX专门做为一个下载端的话可以关掉,好处是降低磁盘与网络的IO处理数及#系统的UPTIME

sendfile <span style="color: #0000ff">on<span style="color: #000000">;

autoindex <span style="color: #0000ff">on<span style="color: #000000">;开启目录列表访问,适合下载服务器

tcp_nopush <span style="color: #0000ff">on<span style="color: #000000">; #防止网络阻塞

非常重要,根据实际情况设置值,超时时间,客户端到服务端的连接持续有效时间,60秒内可避免重新建立连接,时间也不能设太长,太长的话,若请求数10000##,都占用连接会把服务托死

keepalive_timeout <span style="color: #800000; font-weight: bold">60<span style="color: #000000">;
tcp_nodelay <span style="color: #0000ff">on<span style="color: #000000">; #提高数据的实时响应性
clienT_Body_buffer_size 512k; #缓冲区代理缓冲用户端请求的最大字节数(请求多)

proxy_connect_timeout <span style="color: #800000; font-weight: bold">5<span style="color: #000000">; #nginx跟后端服务器连接超时时间(代理连接超时)
proxy_read_timeout <span style="color: #800000; font-weight: bold">60<span style="color: #000000">; #连接成功后,后端服务器响应时间(代理接收超时)
proxy_send_timeout <span style="color: #800000; font-weight: bold">5<span style="color: #000000">; #后端服务器数据回传时间(代理发送超时)
proxy_buffer_size 16k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小
proxy_buffers <span style="color: #800000; font-weight: bold">4<span style="color: #000000"> 64k; #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
proxy_busy_buffers_size 128k; #高负荷下缓冲大小
proxy_temp_file_write_size 128k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传

gzip <span style="color: #0000ff">on<span style="color: #000000">; #NGINX可以压缩静态资源,比如我的静态资源有10M,压缩后只有2M,那么浏览器下载的就少了
gzip_min_length 1k;
gzip_buffers <span style="color: #800000; font-weight: bold">4<span style="color: #000000"> 16k;
gzip_http_version <span style="color: #800000; font-weight: bold">1.1<span style="color: #000000">;
gzip_comp_level <span style="color: #800000; font-weight: bold">2<span style="color: #000000">; #压缩级别大小,最小1,最大9.值越小,压缩后比例越小,CPU处理更快,为1时,原10M压缩完后8M,但设为9时,压缩完可能只有2M了。一般设置为2
gzip_types <span style="color: #0000ff">text<span style="color: #808080">/plain application<span style="color: #808080">/x<span style="color: #808080">-javascript <span style="color: #0000ff">text<span style="color: #808080">/css application<span style="color: #808080">/xml; #压缩类型:<span style="color: #0000ff">text<span style="color: #000000">,js css xml 都会被压缩
gzip_vary <span style="color: #0000ff">on<span style="color: #000000">; #作用是在http响应中增加一行目的是改变反向代理服务器的缓存策略

日志格式

log_format main <span style="color: #ff0000">'<span style="color: #ff0000">$remote_addr - $remote_user [$time_local] "$request" <span style="color: #ff0000">'<span style="color: #000000"> #ip 远程用户 当地时间 请求URL
<span style="color: #ff0000">'<span style="color: #ff0000">$status $body_bytes_sent "$http_referer" <span style="color: #ff0000">'<span style="color: #000000"> #状态 发送的大小 响应的头
<span style="color: #ff0000">'<span style="color: #ff0000">"$http_user_agent" $request_time<span style="color: #ff0000">'<span style="color: #000000">; #客户端使用的浏览器 页面响应的时间

动态转发

upstream web1 {

每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。配置了ip_hash就没有负载均衡的效果了,每次访问的都是同一个tomcat

#ip_hash; 
#转发的后端的tomcat服务器,weight表示转发的权重,越大转发的次数越多,机器性能不一样配置的weight值不一样     
 server   </span><span style="color: #800000; font-weight: bold"&gt;192.168</span>.<span style="color: #800000; font-weight: bold"&gt;152.129</span>:<span style="color: #800000; font-weight: bold"&gt;8080</span> weight<span style="color: #808080"&gt;=</span><span style="color: #800000; font-weight: bold"&gt;1</span> max_fails<span style="color: #808080"&gt;=</span><span style="color: #800000; font-weight: bold"&gt;2</span> fail_timeout<span style="color: #808080"&gt;=</span><span style="color: #000000"&gt;30s;
 server   </span><span style="color: #800000; font-weight: bold"&gt;192.168</span>.<span style="color: #800000; font-weight: bold"&gt;152.129</span>:<span style="color: #800000; font-weight: bold"&gt;8081</span> weight<span style="color: #808080"&gt;=</span><span style="color: #800000; font-weight: bold"&gt;1</span> max_fails<span style="color: #808080"&gt;=</span><span style="color: #800000; font-weight: bold"&gt;2</span> fail_timeout<span style="color: #808080"&gt;=</span><span style="color: #000000"&gt;30s;

}
upstream web2 {
server <span style="color: #800000; font-weight: bold">192.168.<span style="color: #800000; font-weight: bold">152.129:<span style="color: #800000; font-weight: bold">8090 weight<span style="color: #808080">=<span style="color: #800000; font-weight: bold">1 max_fails<span style="color: #808080">=<span style="color: #800000; font-weight: bold">2 fail_timeout<span style="color: #808080">=<span style="color: #000000">30s;
server <span style="color: #800000; font-weight: bold">192.168.<span style="color: #800000; font-weight: bold">152.129:<span style="color: #800000; font-weight: bold">8091 weight<span style="color: #808080">=<span style="color: #800000; font-weight: bold">1 max_fails<span style="color: #808080">=<span style="color: #800000; font-weight: bold">2 fail_timeout<span style="color: #808080">=<span style="color: #000000">30s;
}

server {
listen <span style="color: #800000; font-weight: bold">80<span style="color: #000000">; #监听80端口
server_name www.dbspread.com; #域名

rewrite规则

</span><span style="color: #0000ff"&gt;index</span>  <span style="color: #0000ff"&gt;index</span>.jsp <span style="color: #0000ff"&gt;index</span>.html <span style="color: #0000ff"&gt;index</span><span style="color: #000000"&gt;.htm;
root   </span><span style="color: #808080"&gt;/</span>usr<span style="color: #808080"&gt;/</span>local<span style="color: #808080"&gt;/</span>nginx<span style="color: #808080"&gt;/</span><span style="color: #000000"&gt;html; #定义服务器的默认网站根目录位置
#重定向
</span><span style="color: #0000ff"&gt;if</span> ($host <span style="color: #808080"&gt;!=</span> <span style="color: #ff0000"&gt;'</span><span style="color: #ff0000"&gt;www.dbspread.com</span><span style="color: #ff0000"&gt;'</span><span style="color: #000000"&gt; ){ 
        rewrite </span><span style="color: #808080"&gt;^/</span>(.<span style="color: #808080"&gt;*</span>)$  http:<span style="color: #808080"&gt;//</span>www.dbspread.com<span style="color: #808080"&gt;/</span>$<span style="color: #800000; font-weight: bold"&gt;1</span>  <span style="color: #0000ff"&gt;peRMANent</span><span style="color: #000000"&gt;;
        }

#防盗链
 LOCATIOn </span><span style="color: #808080"&gt;~*</span> \.(rmvb<span style="color: #808080"&gt;|</span>jpg<span style="color: #808080"&gt;|</span>png<span style="color: #808080"&gt;|</span>swf<span style="color: #808080"&gt;|</span>flv)$ { #rmvb<span style="color: #808080"&gt;|</span>jpg<span style="color: #808080"&gt;|</span>png<span style="color: #808080"&gt;|</span>swf<span style="color: #808080"&gt;|</span>flv表示对rmvb<span style="color: #808080"&gt;|</span>jpg<span style="color: #808080"&gt;|</span>png<span style="color: #808080"&gt;|</span>swf<span style="color: #808080"&gt;|</span><span style="color: #000000"&gt;flv后缀的文件实行防盗链
            valid_referers none blocked  www.dbspread.com; #表示对www.dbspread.com此域名开通白名单,比如在www.test.com的index.html引用download</span><span style="color: #808080"&gt;/</span><span style="color: #000000"&gt;av123.rmvb,无效
            root   html</span><span style="color: #808080"&gt;/</span><span style="color: #000000"&gt;b;
            </span><span style="color: #0000ff"&gt;if</span><span style="color: #000000"&gt; ($invalid_referer) { #如果请求不是从www.dbspread.com白名单发出来的请求,直接重定向到403.html这个页面或者返回403 
                 #rewrite </span><span style="color: #808080"&gt;^/</span> http:<span style="color: #808080"&gt;//</span>www.dbspread.com<span style="color: #808080"&gt;/</span><span style="color: #800000; font-weight: bold"&gt;403</span><span style="color: #000000"&gt;.html;
                 </span><span style="color: #0000ff"&gt;return</span> <span style="color: #800000; font-weight: bold"&gt;403</span><span style="color: #000000"&gt;;
            }
    }

#监听完成以后通过斜杆(</span><span style="color: #808080"&gt;/</span><span style="color: #000000"&gt;)拦截请求转发到后端的tomcat服务器
LOCATIOn </span><span style="color: #808080"&gt;/</span><span style="color: #000000"&gt; 
    {
        #如果后端的服务器返回502、</span><span style="color: #800000; font-weight: bold"&gt;504</span><span style="color: #000000"&gt;、执行超时等错误,自动将请求转发到upstream负载均衡池中的另一台服务器,实现@R_340_10772@。
        proxy_next_upstream http_502 http_504 error timeout invalid_header;
        proxy_set_header Host  $host; #获取客户端的主机名存到变量Host里面,从而让tomcat取到客户端机器的信息
        proxy_set_header X</span><span style="color: #808080"&gt;-</span>ForWARDed<span style="color: #808080"&gt;-</span><span style="color: #0000ff"&gt;For</span><span style="color: #000000"&gt; $proxy_add_x_forWARDed_for;
        #rewrite     </span><span style="color: #808080"&gt;^/</span>$    http:<span style="color: #808080"&gt;//</span>www.dbspread.com<span style="color: #808080"&gt;/</span>new.<span style="color: #0000ff"&gt;index</span>.html  <span style="color: #0000ff"&gt;peRMANent</span>;#用户访问www.dbspread.com,想直接跳转到网站下面的某个页面:www.dbspread.com<span style="color: #808080"&gt;/</span>new.<span style="color: #0000ff"&gt;index</span><span style="color: #000000"&gt;.html
        proxy_pass http:</span><span style="color: #808080"&gt;//</span><span style="color: #000000"&gt;web1; #跳转到对应的应用web1
    }

   # LOCATIOn </span><span style="color: #808080"&gt;~</span> .<span style="color: #808080"&gt;*</span>\.(php<span style="color: #808080"&gt;|</span>jsp<span style="color: #808080"&gt;|</span>cgi<span style="color: #808080"&gt;|</span>shtml)?$ #动态分离 <span style="color: #808080"&gt;~</span>匹配 以.<span style="color: #808080"&gt;*</span><span style="color: #000000"&gt;结尾(以php JSP结尾走这段)
   #  {
   #     proxy_set_header Host  $host;
   #        proxy_set_header X</span><span style="color: #808080"&gt;-</span><span style="color: #0000ff"&gt;Real</span><span style="color: #808080"&gt;-</span><span style="color: #000000"&gt;IP $remote_addr;
   #        proxy_set_header X</span><span style="color: #808080"&gt;-</span>ForWARDed<span style="color: #808080"&gt;-</span><span style="color: #0000ff"&gt;For</span><span style="color: #000000"&gt; $proxy_add_x_forWARDed_for;
   #        proxy_pass http:</span><span style="color: #808080"&gt;//</span><span style="color: #000000"&gt;jvm_web2;
   # }

    #静态分离 </span><span style="color: #808080"&gt;~</span>匹配 以.<span style="color: #808080"&gt;*</span>结尾(以html<span style="color: #808080"&gt;|</span>htm<span style="color: #808080"&gt;|</span>gif<span style="color: #808080"&gt;|</span>jpg<span style="color: #808080"&gt;|</span>jpeg<span style="color: #808080"&gt;|</span>bmp<span style="color: #808080"&gt;|</span>png<span style="color: #808080"&gt;|</span>ico<span style="color: #808080"&gt;|</span>txt<span style="color: #808080"&gt;|</span>js<span style="color: #808080"&gt;|</span><span style="color: #000000"&gt;css结尾走这段),当然不是越久越好,如果有10000个用户在线,都保存几个月,系统托跨
    LOCATIOn </span><span style="color: #808080"&gt;~</span> .<span style="color: #808080"&gt;*</span>\.(html<span style="color: #808080"&gt;|</span>htm<span style="color: #808080"&gt;|</span>gif<span style="color: #808080"&gt;|</span>jpg<span style="color: #808080"&gt;|</span>jpeg<span style="color: #808080"&gt;|</span>bmp<span style="color: #808080"&gt;|</span>png<span style="color: #808080"&gt;|</span>ico<span style="color: #808080"&gt;|</span>txt<span style="color: #808080"&gt;|</span>js<span style="color: #808080"&gt;|</span><span style="color: #000000"&gt;css)$ 
    {
        root </span><span style="color: #808080"&gt;/</span><span style="color: #ff00ff"&gt;var</span><span style="color: #808080"&gt;/</span>local<span style="color: #808080"&gt;/</span><span style="color: #000000"&gt;static; #静态资源存放在Nginx的安装机器上
        #proxy_pass http:</span><span style="color: #808080"&gt;//</span><span style="color: #000000"&gt;www.static.com; #静态资源也可存放在远程服务器上
        expires    30d;
    }

    #日志级别有</span><span style="color: #ff0000"&gt;[</span><span style="color: #ff0000"&gt;debug|info|notice|warn|error|crit</span><span style="color: #ff0000"&gt;]</span><span style="color: #000000"&gt;  error_log 级别分为 debug,而debug记录的日志最多
    access_log  </span><span style="color: #808080"&gt;/</span>usr<span style="color: #808080"&gt;/</span>local<span style="color: #808080"&gt;/</span>logs<span style="color: #808080"&gt;/</span>web2<span style="color: #808080"&gt;/</span>access.<span style="color: #ff00ff"&gt;log</span><span style="color: #000000"&gt; main;
    error_log   </span><span style="color: #808080"&gt;/</span>usr<span style="color: #808080"&gt;/</span>local<span style="color: #808080"&gt;/</span>logs<span style="color: #808080"&gt;/</span>web2<span style="color: #808080"&gt;/</span>error.<span style="color: #ff00ff"&gt;log</span><span style="color: #000000"&gt;  crit;

}

}

 到这一步环境准备已完成,相关的配置也修改完成,下面我们来查看效果

5.6 配置hosts域名映射

192.168.152.200  www.dbspread.com

paddress配置的虚拟ip

paddress { .p),可多设,每行一个 }

5.7 分别启动192.168.152.129的两个tomcat

5.8 分别启动192.168.152.130(keepalived主节点)和 192.168.152.129(keepalived从节点)的keepalived的

 启动命令:/usr/local/keepalived/sbin/keepalived

 

title="Nginx系列二:(Nginx Rewrite 规则、Nginx 防盗链、Nginx 动静分离、Nginx+keepalived 实现高可用)" alt="Nginx系列二:(Nginx Rewrite 规则、Nginx 防盗链、Nginx 动静分离、Nginx+keepalived 实现高可用)" src="https://cn.js-code.com/res/2019/02-10/10/a5ad3190926972fa4c2fc42347ded950.png" >

可以看到keepalived和nginx都启动了

在浏览器输入www.dpspread.com域名访问

title="Nginx系列二:(Nginx Rewrite 规则、Nginx 防盗链、Nginx 动静分离、Nginx+keepalived 实现高可用)" alt="Nginx系列二:(Nginx Rewrite 规则、Nginx 防盗链、Nginx 动静分离、Nginx+keepalived 实现高可用)" src="https://cn.js-code.com/res/2019/02-10/10/21a5f2f49db5bfd199d3189677fbcf23.png" >

5.9 下面我们停掉主节点192.168.152.130的keepalived和nginx

 

title="Nginx系列二:(Nginx Rewrite 规则、Nginx 防盗链、Nginx 动静分离、Nginx+keepalived 实现高可用)" alt="Nginx系列二:(Nginx Rewrite 规则、Nginx 防盗链、Nginx 动静分离、Nginx+keepalived 实现高可用)" src="https://cn.js-code.com/res/2019/02-10/10/e4523bf6fa0ff3a37accca41847d6a2a.png" >

可以看到从节点变为主节点了

title="Nginx系列二:(Nginx Rewrite 规则、Nginx 防盗链、Nginx 动静分离、Nginx+keepalived 实现高可用)" alt="Nginx系列二:(Nginx Rewrite 规则、Nginx 防盗链、Nginx 动静分离、Nginx+keepalived 实现高可用)" src="https://cn.js-code.com/res/2019/02-10/10/1297df2d70e66d6da7a3740df4626e8c.png" >

 在浏览器输入地址www.dpspread.com访问,可以看到访问正常

title="Nginx系列二:(Nginx Rewrite 规则、Nginx 防盗链、Nginx 动静分离、Nginx+keepalived 实现高可用)" alt="Nginx系列二:(Nginx Rewrite 规则、Nginx 防盗链、Nginx 动静分离、Nginx+keepalived 实现高可用)" src="https://cn.js-code.com/res/2019/02-10/10/fe25a2785342c7121ce4635d4fe2411b.png" >

5.10 下面我们重新启动主节点192.168.152.130

 

title="Nginx系列二:(Nginx Rewrite 规则、Nginx 防盗链、Nginx 动静分离、Nginx+keepalived 实现高可用)" alt="Nginx系列二:(Nginx Rewrite 规则、Nginx 防盗链、Nginx 动静分离、Nginx+keepalived 实现高可用)" src="https://cn.js-code.com/res/2019/02-10/10/804fcd55c514e9558622ef81332a68ed.png" >

可以看到主节点重新启动以后变为主节点了

title="Nginx系列二:(Nginx Rewrite 规则、Nginx 防盗链、Nginx 动静分离、Nginx+keepalived 实现高可用)" alt="Nginx系列二:(Nginx Rewrite 规则、Nginx 防盗链、Nginx 动静分离、Nginx+keepalived 实现高可用)" src="https://cn.js-code.com/res/2019/02-10/10/20dd62f41871eaf853f380539d70f9d3.png" >

之前变为主节点的从节点又变回从节点了

title="Nginx系列二:(Nginx Rewrite 规则、Nginx 防盗链、Nginx 动静分离、Nginx+keepalived 实现高可用)" alt="Nginx系列二:(Nginx Rewrite 规则、Nginx 防盗链、Nginx 动静分离、Nginx+keepalived 实现高可用)" src="https://cn.js-code.com/res/2019/02-10/10/642af82912d8d66668ecde2d4a52d07c.png" >

大佬总结

以上是大佬教程为你收集整理的Nginx系列二:(Nginx Rewrite 规则、Nginx 防盗链、Nginx 动静分离、Nginx+keepalived 实现高可用)全部内容,希望文章能够帮你解决Nginx系列二:(Nginx Rewrite 规则、Nginx 防盗链、Nginx 动静分离、Nginx+keepalived 实现高可用)所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: