百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术资源 > 正文

nginx会话保持与防盗链(nginx 保持连接)

off999 2025-02-03 14:34 13 浏览 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这个规则。
在图片服务器查看日志:

转自:知乎千锋云计算学院

相关推荐

咱村里有个老爷子,居然自学起了Python编程

咱村里有个老爷子,没什么文化,居然自学起了Python编程,还搞出个“智能喂鸡系统”,这事儿可把整个村子都惊到了。要说这老爷子,平时就爱琢磨些新鲜玩意儿。一开始,大家还以为他是瞎折腾,毕竟都一把年纪了...

真上头!清华打造的最全Python教程,通俗易懂,学不会我退出IT圈

前言随着人工智能的发展,Python近两年也是大火,越来越多的人加入到Python学习大军,对于毫无基础的人该如何入门Python呢?小编这里整理了一套python编程零基础自学教程,清华大佬196小...

如何学好Python技术(怎么才能学会python)

现在python发展势头很猛,都想快速学好它,其实学任何一个语言没有太多好的秘诀,一般情况下,还是少不了你努力刻苦的样子。学好一门技术并不容易,很多人推荐学习python,在于比其他语言的约束,或者...

如何高效且系统地自学Python?(自己学python怎么学)

关于这个问题,我也算有些话语权吧!5年多经验的我,今天和大家分享一套系统性学习Python的方法,几周内系统性地学会Python并不是啥难事!首先,学习Python确立明确的学习目标至关重要。要系统性...

使用 Python 监控文件系统(基于python的监控系统)

前言在我们使用服务器的时候,有时候需要监控文件或文件夹的变化。例如,定期扫描文件夹下是否有某一类型的文件生成。今天,我们介绍如何使用Python来监控文件系统。在Python中,主要有两个监控...

Python文件读写最佳实践:关键操作的异常处理

在Python中进行文件操作时,合理的异常处理是保证程序健壮性的关键。以下是针对文件操作异常处理的全面指南。一、为什么需要异常处理?文件操作可能失败的常见原因:文件不存在(FileNotFoundEr...

Python编程笔记(python编程入门与案例详解)

1.Python简介Python是一种解释型、高级和通用的编程语言。它通过显著的缩进使用来强调代码的可读性。#HelloWorldprogramprint("Hello,World...

Python目录与文件操作教程(python word目录)

大家好,我是ICodeWR。今天要记录的是如何使用Python进行常见的目录和文件操作。Python提供了强大的内置模块来处理文件和目录操作。1.基本模块介绍Python中主要使用以下模块进行文件...

自动创建 Python 的 requirements.txt 文件

技术背景在Python开发中,requirements.txt文件用于记录项目所依赖的第三方库及其版本,方便在不同环境中部署项目。然而,当从GitHub下载Python源代码时,有时会缺...

Python文件操作指南(python 操作文件)

一、核心函数open()精解基本语法open(file,mode='r',encoding=None,errors=None,newline=None)关键参数解析1.f...

Python 实现从文本文件提取数据并分析保存

一、引言在日常的数据处理工作中,我们经常会遇到从文本文件中提取特定信息并进行分析的需求。本文将详细介绍如何使用Python编写代码,从一个包含用户网络使用信息的文本文件中提取用户姓名、入站流量和出...

22-3-Python高级特性-上下文管理器

4-上下文管理器4-1-概念上下文管理器是一种实现了`__enter__()`和`__exit__()`方法的对象;用于管理资源的生命周期,如文件的打开和关闭、数据库连接的建立和断开等。使用...

python:最简单爬虫之使用Scrapy框架爬取小说

python爬虫框架中,最简单的就是Scrapy框架。执行几个命令就能生成爬虫所需的项目文件,我们只需要在对应文件中调整代码,就能实现整套的爬虫功能。以下在开发工具PyCharm中用简单的Demo项目...

Python爬取小说技术指南(python爬取文章)

在Python中爬取小说需要遵循法律法规和网站的服务条款,请确保你有权获取目标内容。以下是使用Python爬取小说的通用技术流程安装依赖库pipinstallrequestsbeauti...

python原始套接字socket下载http网页文件到txt

python原始套接字socket下载http网页文件到txtimportsocketdefdownload_webpage(url,output_file):try:...

取消回复欢迎 发表评论: