一、动静分离
1.作用
为了加快网站的解析速度,可以把动态页面和静态页面由不同的服务器来解析,加快解析速度,降低原来单个服务器的压力。在动静分离的tomcat的时候比较明显,因为tomcat解析静态很慢,处理动态页面比nginx快许多。简单来说,就是使用正则表达式匹配过滤,然后交给不同的服务器。
2.实验环境
三台服务器(都已安装nginx):
192.168.62.159 代理服务器
192.168.62.155 静态资源
192.168.62.157 动态资源
3.代理服务器
[root@cheng ~]# vim /etc/nginx/conf.d/default.conf //配置子配置文件
upstream static {
server 192.168.62.155:80 weight=1 max_fails=1 fail_timeout=60s;
}
upstream phpserver {
server 192.168.62.157:80 weight=1 max_fails=1 fail_timeout=60s;
}
server {
listen 80;
server_name localhost;
#动态资源加载
location ~ \.(php|jsp)$ {
#当访问动态页面时location匹配到 .\php|jsp 结尾的文件转发到后端php服务处理请求。
proxy_pass http://phpserver;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
#静态资源加载
location ~ .*\.(html|gif|jpg|png|bmp|swf|css|js)$ { #当访问静态页面的时候location 匹配到 (html|gif|jpg|png|bmp|swf|css|js) 通过转发到静态服务器,静态服务通过location的正则匹配来处理请求。
proxy_pass http://static;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
[root@cheng ~]#systemctl start nginx
## 4.静态服务器
[root@meng ~]# vim /etc/nginx/conf.d/static.conf
server {
listen 80;
server_name localhost;
location ~ \.(html|jpg|png|js|css|gif|bmp|jpeg) {
root /home/www/nginx;
index index.html index.htm;
}
}
[root@meng ~]# vim /home/www/nginx/index.html //模拟静态资源
hello 155
[root@meng ~]#systemctl start nginx
5.动态服务器
需先下载yum源,然后安装PHP相关配件
[root@x ~]# rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-release.rpm
[root@x ~]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
[root@x ~]# yum install php71w-xsl php71w php71w-ldap php71w-cli php71w-common php71w-devel php71w-gd php71w-pdo php71w-mysql php71w-mbstring php71w-bcmath php71w-mcrypt -y
[root@x ~]# yum install -y php71w-fpm
[root@x ~]# systemctl start php-fpm
[root@x ~]# systemctl enable php-fpm
[root@x ~]#vim /etc/nginx/conf.d/default.conf
server {
listen 80;
server_name localhost;
location ~ \.php$ {
root /home/nginx/html; #指定网站目录
fastcgi_pass 127.0.0.1:9000; #指定访问地址,接口
fastcgi_index index.php; #指定默认文件
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; #站点根目录,取决于root配置项
include fastcgi_params; #包含nginx常量定义
}
}
[root@x1 html]# cat /home/nginx/html/index.php //模拟动态资源
dongtai
[root@x1 html]#systemctl start nginx
6.访问测试
网址输入:192.168.62.159/index.html或192.168.62.159/index.php
二、防盗链
1.简介
两个网站 A 和 B,B网站引用了A网站上的图片,这种行为就叫做盗链。 防盗链,就是要防止B引用A的图片。
2.防盗模块
ngx_http_referer_module,是为了区分不正常的用户。HTTP Referer是Header 的一部分,当浏览器向Web服务器发送请求的时候,一般会带上Referer,告诉服 务器我是从哪个页面链接过来的,服务器借此可以获得一些信息用于处理,例如防止未经允许的网站盗链图片、文件等。因此HTTP Referer头信息是可以通过程序来 伪装生成的,所以通过Referer信息防盗链并非100%可靠,但是,它能够限制大部分的盗链情况。
3.实例
1)环境
两台机器,10.8.161.112(图片服务器),10.8.161.34(盗取服务器)
防盗格式:
Syntax: valid_referers none | blocked | server_names | string …;
Default: —
Context: server, location
其中:
none : 允许没有http_referer的请求访问资源;
blocked : 允许不是http://开头的,不带协议的请求访问资源—被防火墙过滤掉的;
server_names : 只允许指定ip/域名来的请求访问资源(白名单);
2)图片服务器初态
1>上传图片到指定目录
[root@meng ~]# ls /usr/share/nginx/html/
cc.jpg index.html
2>编辑子配置文件
[root@meng ~]# vim /etc/nginx/conf.d/default.conf
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html;
}
}
[root@meng ~]# systemctl restart nginx
3>访问
[root@meng ~]# tail -1 /var/log/nginx/access.log //查看访问产生的日志
10.8.161.48 - - [13/Jan/2021:20:01:03 +0800] "GET /cc.jpg HTTP/1.1" 200 6457 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/87.0.4280.141 Safari/537.36" "-"
3)盗取服务器
1>编辑子配置文件
[root@cheng ~]# vim /etc/nginx/conf.d/default.conf
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html;
}
}
2>编辑网页文件
[root@cheng ~]# vim /usr/share/nginx/html/index.html
<html>
<head>
<meta charset="utf-8">
<title>dai.com</title>
</head>
<body style="background-color:red;"> //设置背景为红色
<img src="http://http://10.8.161.112/cc..jpg"/> //超链接盗取图片
</body>
</html>
[root@cheng ~]#systemctl restart nginx
3>访问测试
4)图片服务器进行防盗配置
[root@meng ~]# vim /etc/nginx/conf.d/default.conf
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html;
valid_referers none blocked www.daidu.com; //允许的访问
if ($invalid_referer) {
return 404;
}
}
}
[root@meng ~]# systemctl restart nginx
测试访问:
5)Curl测试
-I 网址 //查看返回信息
-e IP/域名 //通过该IP或域名访问
[root@xiong ~]#curl -I "http://10.8.161.112/cc.jpg" //不带http_refer
HTTP/1.1 200 OK //返回值200,说明可以正常访问
Server: nginx/1.16.1
Date: Wed, 13 Jan 2021 12:31:41 GMT
Content-Type: image/jpeg
Content-Length: 6457
Last-Modified: Wed, 13 Jan 2021 12:00:53 GMT
Connection: keep-alive
ETag: "5ffee0f5-1939"
Accept-Ranges: bytes
[root@xiong ~]#curl -I "http://10.8.161.112/cc.jpg" -e www.baidu.com //www.baidu.com带有合法的http_refer
HTTP/1.1 200 OK
Server: nginx/1.16.1
Date: Wed, 13 Jan 2021 12:31:45 GMT
Content-Type: image/jpeg
Content-Length: 6457
Last-Modified: Wed, 13 Jan 2021 12:00:53 GMT
Connection: keep-alive
ETag: "5ffee0f5-1939"
Accept-Ranges: bytes
[root@xiong ~]#curl -e http://10.8.161.34 -I "http://10.8.161.112/cc.jpg" //10.8.161.34含有非法http_refer(不被允许),因此访问失败
HTTP/1.1 404 Not Found
Server: nginx/1.16.1
Date: Wed, 13 Jan 2021 12:32:47 GMT
Content-Type: text/html
Content-Length: 153
Connection: keep-alive
查看日志,每一行开头为访问IP,倒数第三个为间接网站
[root@meng ~]# tail -3 /var/log/nginx/access.log
10.8.161.142 - - [13/Jan/2021:20:31:41 +0800] "HEAD /cc.jpg HTTP/1.1" 200 0 "-" "curl/7.29.0" "-"
10.8.161.142 - - [13/Jan/2021:20:31:45 +0800] "HEAD /cc.jpg HTTP/1.1" 200 0 "www.baidu.com" "curl/7.29.0" "-"
10.8.161.142 - - [13/Jan/2021:20:32:47 +0800] "HEAD /cc.jpg HTTP/1.1" 404 0 "http://10.8.161.34" "curl/7.29.0" "-"