nginx会话保持与防盗链(nginx 保持连接)
off999 2025-02-03 14:34 31 浏览 0 评论
nginx 会话保持
nginx会话保持主要有以下几种实现方式。
1、ip_hash
ip_hash使用源地址哈希算法,将同一客户端的请求总是发往同一个后端服务器,除非该服务器不可用。
ip_hash语法:
upstream backend {
ip_hash;
server backend1.example.com;
server backend2.example.com;
server backend3.example.com down;
}ip_hash简单易用,但有如下问题: 当后端服务器宕机后,session会丢失; 来自同一局域网的客户端会被转发到同一个后端服务器,可能导致负载失衡;
2、sticky_cookie_insert
使用sticky_cookie_insert,这会让来自同一客户端的请求被传递到一组服务器的同一台服务器。与ip_hash不同之处在于,它不是基于IP来判断客户端的,而是基于cookie来判断。(需要引入第三方模块才能实现)
sticky模块
语法:
upstream backend {
server backend1.example.com;
server backend2.example.com;
sticky_cookie_insert srv_id expires=1h domain=3evip.cn path=/;
}说明: expires:设置浏览器中保持cookie的时间 domain:定义cookie的域 path:为cookie定义路径
4、使用后端服务器自身通过相关机制保持session同步,如:使用数据库、redis、memcached 等做session复制
10、nginx 实现动静分离
为了加快网站的解析速度,可以把动态页面和静态页面由不同的服务器来解析,加快解析速度。降低原来单个服务器的压力。 简单来说,就是使用正则表达式匹配过滤,然后交个不同的服务器。
1、准备环境
准备一个nginx代理 两个http 分别处理动态和静态。
1.配置nginx反向代理upstream;
upstream static {
server 10.0.105.196:80 weight=1 max_fails=1 fail_timeout=60s;
}
upstream php {
server 10.0.105.200:80 weight=1 max_fails=1 fail_timeout=60s;
}
server {
listen 80;
server_name localhost
#动态资源加载
location ~ \.(php|jsp)$ {
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|jpg|png|css|js)$ {
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;
}
}
静态资源配置
server {
listen 80;
server_name localhost;
location ~ \.(html|jpg|png|js|css) {
root /home/www/nginx;
}
}
动态资源配置:
yum 安装php7.1
[root@nginx-server ~]#rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-release.rpm
[root@nginx-server ~]#rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
[root@nginx-server ~]#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@nginx-server ~]#yum install -y php71w-fpm
[root@nginx-server ~]#systemctl start php-fpm
[root@nginx-server ~]#systemctl enable php-fpm
编辑nginx的配置文件:
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常量定义
}
}当访问静态页面的时候location 匹配到 (html|jpg|png|js|css) 通过转发到静态服务器,静态服务通过location的正则匹配来处理请求。
当访问动态页面时location匹配到 .\php 结尾的文件转发到后端php服务处理请求。
11、nginx 防盗链问题
两个网站 A 和 B, B网站引用了A网站上的图片,这种行为就叫做盗链。 防盗链,就是要防止B引用A的图片。1、nginx 防止网站资源被盗用模块
ngx_http_referer_module如何区分哪些是不正常的用户?
HTTP Referer是Header的一部分,当浏览器向Web服务器发送请求的时候,一般会带上Referer,
告诉服务器我是从哪个页面链接过来的,服务器借此可以获得一些信息用于处理,例如防止未经允许
的网站盗链图片、文件等。因此HTTP Referer头信息是可以通过程序来伪装生成的,所以通过Referer
信息防盗链并非100%可靠,但是,它能够限制大部分的盗链情况.
2. 防盗链配置
配置要点:
[root@nginx-server ~]# vim /etc/nginx/nginx.conf
# 日志格式添加"$http_referer"
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
# valid_referers 使用方式
Syntax: valid_referers none | blocked | server_names | string ...;
Default: —
Context: server, location- none : 允许没有http_refer的请求访问资源;
- blocked : 允许不是http://开头的,不带协议的请求访问资源---被防火墙过滤掉的;
- server_names : 只允许指定ip/域名来的请求访问资源(白名单);
准备两台机器,一张图片
图片网站服务器:上传图片192.168.1.9
[root@nginx-server ~]# cp test.jpg /usr/share/nginx/html/
[root@nginx-server ~]# cd /etc/nginx/conf.d/
[root@nginx-server conf.d]# cp default.conf default.conf.bak
[root@nginx-server conf.d]# mv default.conf nginx.conf
[root@nginx-server conf.d]# vim nginx.conf
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
[root@nginx-server conf.d]# nginx -t
[root@nginx-server conf.d]# systemctl restart nginx
访问:
Referer:这个匹配的连接为空 “-”
盗链机器配置:192.168.1.10
[root@nginx-client ~]# cd /usr/share/nginx/html/
[root@nginx-client html]# cp index.html index.html.bak
[root@nginx-client html]# vim index.html
<html>
<head>
<meta charset="utf-8">
<title>http://qf.com</title>
</head>
<body style="background-color:red;">
<img src="http://192.168.1.9/test.jpg"/>
</body>
</html>
[root@nginx-client html]# systemctl restart nginx
查看服务器日志:
Referer记录了:连接是1.10这台机器。
在图片服务器操作
[root@nginx-server conf.d]# vim nginx.conf
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
valid_referers none blocked www.jd.com; #允许这些访问
if ($invalid_referer) {
return 403;
}
}
}
[root@nginx-server conf.d]# systemctl restart nginx
测试访问:
图片服务器查看日志:
上面配置并没有允许192.168.1.10这台机器访问。
实例二,继续在图片服务器上面操作
[root@nginx-server html]# vim /etc/nginx/conf.d/nginx.conf #将原来的删除掉
server {
listen 80;
server_name localhost;
location ~ .*\.(gif|jpg|png|jpeg)$ {
root /usr/share/nginx/html;
valid_referers none blocked *.http://qf.com 192.168.1.10;
if ($invalid_referer) {
return 403;
}
}
}
重载nginx服务
[root@nginx-server ~]# nginx -s reload
在其中一台机器测试:
测试不带http_refer:
[root@nginx-server conf.d]# curl -I "http://192.168.1.9/test.jpg"
HTTP/1.1 200 OK
Server: nginx/1.16.1
Date: Mon, 02 Sep 2019 14:02:56 GMT
Content-Type: image/jpeg
Content-Length: 27961
Last-Modified: Mon, 02 Sep 2019 13:23:12 GMT
Connection: keep-alive
ETag: "5d6d17c0-6d39"
Accept-Ranges: bytes
测试带非法http_refer:
[root@nginx-server conf.d]# curl -e http://www.baidu.com -I "http://192.168.1.9/test.jpg"
HTTP/1.1 403 Forbidden
Server: nginx/1.16.1
Date: Mon, 02 Sep 2019 14:03:48 GMT
Content-Type: text/html
Content-Length: 153
Connection: keep-alive
测试带合法的http_refer:
[root@nginx-server conf.d]# curl -e http://www.qf.com -I "http://192.168.1.9/test.jpg"
HTTP/1.1 200 OK
Server: nginx/1.16.1
Date: Mon, 02 Sep 2019 14:04:52 GMT
Content-Type: image/jpeg
Content-Length: 27961
Last-Modified: Mon, 02 Sep 2019 13:23:12 GMT
Connection: keep-alive
ETag: "5d6d17c0-6d39"
Accept-Ranges: bytes
[root@ansible-server conf.d]# curl -e http://192.168.1.10 -I "http://192.168.1.9/test.jpg"
HTTP/1.1 200 OK
Server: nginx/1.16.1
Date: Mon, 02 Sep 2019 14:05:36 GMT
Content-Type: image/jpeg
Content-Length: 27961
Last-Modified: Mon, 02 Sep 2019 13:23:12 GMT
Connection: keep-alive
ETag: "5d6d17c0-6d39"
Accept-Ranges: bytes
如果用户直接在浏览器输入你的图片地址,那么图片显示正常,因为它符合none这个规则。
在图片服务器查看日志:
转自:知乎千锋云计算学院
相关推荐
- 免费360清理大师官方版(华为自带清理软件)
-
挺好用的。它可以清除你手机的里面的垃圾,检测和修复软件安全性。还有一些漏洞垃圾,你可以试着对比和使用,功效和针对性比较强。您杀毒没有效果的原因可能是您的安全软件版本过旧导致的,建议您使用最新版的腾讯手...
- 万能解压器安卓版(万能解压器官方下载)
-
是一款手机文件的助手。万能解压器手机版。专注于快速解压和压缩的文件管理工具!支持ZIP,RAR,7Z,TAR,ZIPX,GZIP,JAR等压缩和解压文件格式。是超好用、超便捷的解压软件!支持在手机上将...
- cad看图(cad看图王)
-
以下是一些CAD看图的方法和技巧:1.放大/缩小:使用滚轮或放大镜工具可以方便地放大或缩小绘图。2.平移:使用平移工具可以在不改变视角的情况下将绘图向上、向下、向左或向右移动。3.旋转:使用旋转...
-
- dell笔记本售后服务电话是多少
-
以下为dell售后服务点A:戴尔笔记本电脑维修点地址:上海市长宁区长宁路1027号兆丰广场5层 B:戴尔笔记本电脑维修点地址:上海市徐汇区漕溪北路45号 C:戴尔笔记本电脑维修点地址:上海市徐汇区漕溪路250号银海大厦1...
-
2026-01-02 02:03 off999
- 如何找回浏览器(如何找回浏览器删除记录)
-
如果您的浏览器出现了问题,可以尝试以下方法来恢复浏览器:1.重新启动浏览器:关闭浏览器窗口,再重新打开浏览器,看是否能够解决问题。2.清除浏览器缓存:浏览器缓存可能会导致浏览器出现问题,可以尝试清...
- 应用备份还原app下载(应用备份与恢复下载)
-
如果您已经将手机上的数据备份到电脑,希望从电脑恢复到手机,建议您:1.电脑中安装Kies软件。注:若使用的是安卓4.3操作系统,电脑中需要安装Kies3软件。2.将手机与电脑通过数据线连接,打开Kie...
-
- office2013激活向导(microsoft office激活导向)
-
这是没有正常激活导致的,解决方法如下:1、下载正确的microsoftoffice到桌面上,右键单击从下拉菜单中选择解压到当前文件夹。2、双击桌面上的快捷方式,打开该应用程序,切换到mian选项卡。3、接着点击ez-activator按钮...
-
2026-01-02 00:51 off999
- h3c路由器手机登录入口(h3c路由器登录界面手机)
-
首先就是把华三路由器正确安装,然后手机连接路由器发射出来的WiFi信号。然后点击手机中的浏览器并深入华三路由器的登录地址 moshujia.com或者192.168.124.1,就可以登...
- u盘坏了数据怎么导出来(u盘坏了里面的数据怎么办)
-
方法一、借助数据恢复软件u盘只要不是物理性故障且数据未覆盖的情况下,可借助u盘数据恢复软件来提取打不开的u盘数据。具体操作流程如下:在电脑上插入需要恢复数据的u盘,然后运行u盘数据恢复软件—以云骑士数...
- win10家庭版原装下载(win10家庭版安装包下载)
-
有以下几种原因:第一是因为专业版功能较为齐全,但一般的使用者并不太需要。第二是由于功能齐全,它所占的体积也比较大,进而对电脑的运行速率有一定的影响。第三是Wln10各种版本都还是需要花钱购买的,而专业...
- win7装xp系统怎么安装(win7如何安装xp系统)
-
设置U盘为第一启动项并进入PE系统。开机按F2进入BOIS,在BOOT选项中将U盘设为第一启动盘,通过按F6(有的是Shift+)调整顺序。(或开机按ESC选择启动盘,即你的U盘)。按F10保存...
- windows 98是什么操作系统(windows98属于什么)
-
Windows98是微软公司发行于1998年6月25日的混合16位/32位的Windows操作系统,其版本号为4.1,开发代号为Memphis。肯定有的。Windows95操作系统刚发布的时候就...
- 下载mp3免费的网站(免费下载mp3哪些网站)
-
有免费下载mp3的网站。除了知名的几个音乐平台外,还有以下三款支持免费MP3无损音乐下载网站,可以将喜欢的歌曲下载到U盘。说明书里有呀91flac音乐网,试试这个,绝对好使,但是不要在酷狗上面说网页上...
- win10更新卸载不了怎么办(win10更新后卸载更新失败)
-
右键桌面上“此电脑”—“管理”,或者按组合键“Windows+X”—计算机管理—服务和应用程序—服务,找到Windowsupdate和BackgroundIntelligentTransfe...
欢迎 你 发表评论:
- 一周热门
-
-
抖音上好看的小姐姐,Python给你都下载了
-
全网最简单易懂!495页Python漫画教程,高清PDF版免费下载
-
Python 3.14 的 UUIDv6/v7/v8 上新,别再用 uuid4 () 啦!
-
飞牛NAS部署TVGate Docker项目,实现内网一键转发、代理、jx
-
python入门到脱坑 输入与输出—str()函数
-
宝塔面板如何添加免费waf防火墙?(宝塔面板开启https)
-
Python三目运算基础与进阶_python三目运算符判断三个变量
-
(新版)Python 分布式爬虫与 JS 逆向进阶实战吾爱分享
-
失业程序员复习python笔记——条件与循环
-
系统u盘安装(win11系统u盘安装)
-
- 最近发表
- 标签列表
-
- python计时 (73)
- python安装路径 (56)
- python类型转换 (93)
- python进度条 (67)
- python吧 (67)
- python的for循环 (65)
- python格式化字符串 (61)
- python静态方法 (57)
- python列表切片 (59)
- python面向对象编程 (60)
- python 代码加密 (65)
- python串口编程 (77)
- python封装 (57)
- python写入txt (66)
- python读取文件夹下所有文件 (59)
- python操作mysql数据库 (66)
- python获取列表的长度 (64)
- python接口 (63)
- python调用函数 (57)
- python多态 (60)
- python匿名函数 (59)
- python打印九九乘法表 (65)
- python赋值 (62)
- python异常 (69)
- python元祖 (57)
