486 lines
15 KiB
Markdown
486 lines
15 KiB
Markdown
|
<h1><center>Nginx特性</center></h1>
|
|||
|
|
|||
|
**作者:行癫(盗版必究)**
|
|||
|
|
|||
|
------
|
|||
|
|
|||
|
## 一:Nginx Proxy代理
|
|||
|
|
|||
|
![image-20221009194316358](https://xingdian-image.oss-cn-beijing.aliyuncs.com/xingdian-image/image-20221009194316358.png)
|
|||
|
|
|||
|
#### 1.正向代理
|
|||
|
|
|||
|
正向代理的过程隐藏了真实的请求客户端,服务器不知道真实的客户端是谁,客户端请求的服务都被代理服务器代替请求。我们常说的代理也就是正向代理,正向代理代理的是请求方,也就是客户端;比如我们要访问youtube,可是不能访问,只能先安装个FQ软件代你去访问,通过FQ软件才能访问,FQ软件就叫作正向代理
|
|||
|
|
|||
|
案例:贷款
|
|||
|
|
|||
|
正向代理中,proxy和client同属一个LAN
|
|||
|
|
|||
|
<img src="https://xingdian-image.oss-cn-beijing.aliyuncs.com/xingdian-image/image-20221009194453786.png" alt="image-20221009194453786" style="zoom:50%;" />
|
|||
|
|
|||
|
#### 2.反向代理
|
|||
|
|
|||
|
反向代理的过程隐藏了真实的服务器,客户不知道真正提供服务的人是谁,客户端请求的服务都被代理服务器处理。反向代理代理的是响应方,也就是服务端;我们请求www.baidu.com时这www.baidu.com就是反向代理服务器,真实提供服务的服务器有很多台,反向代理服务器会把我们的请求分转发到真实提供服务的各台服务器。Nginx就是性能非常好的反向代理服务器,用来做负载均衡
|
|||
|
|
|||
|
<img src="https://xingdian-image.oss-cn-beijing.aliyuncs.com/xingdian-image/image-20221009194637284.png" alt="image-20221009194637284" style="zoom:50%;" />
|
|||
|
|
|||
|
反向代理中,proxy和server同属一个LAN
|
|||
|
|
|||
|
<img src="https://xingdian-image.oss-cn-beijing.aliyuncs.com/xingdian-image/image-20221009194710521.png" alt="image-20221009194710521" style="zoom:50%;" />
|
|||
|
|
|||
|
#### 3.Nginx Proxy
|
|||
|
|
|||
|
模块:ngx_http_proxy_module
|
|||
|
|
|||
|
代理配置:
|
|||
|
|
|||
|
```shell
|
|||
|
代理
|
|||
|
Syntax: proxy_pass URL; #代理的后端服务器URL
|
|||
|
Default: —
|
|||
|
Context: location, if in location, limit_except
|
|||
|
|
|||
|
缓冲区
|
|||
|
Syntax: proxy_buffering on | off;
|
|||
|
Default: proxy_buffering on; #缓冲开关
|
|||
|
Context: http, server, location
|
|||
|
proxy_buffering开启的情况下,nignx会把后端返回的内容先放到缓冲区当中,然后再返回给客户端
|
|||
|
(边收边传,不是全部接收完再传给客户端)。
|
|||
|
|
|||
|
Syntax: proxy_buffer_size size;
|
|||
|
Default: proxy_buffer_size 4k|8k; #缓冲区大小
|
|||
|
Context: http, server, location
|
|||
|
|
|||
|
Syntax: proxy_buffers number size;
|
|||
|
Default: proxy_buffers 8 4k|8k; #缓冲区数量
|
|||
|
Context: http, server, location
|
|||
|
|
|||
|
Syntax: proxy_busy_buffers_size size;
|
|||
|
Default: proxy_busy_buffers_size 8k|16k;#忙碌的缓冲区大小控制同时传递给客户端的buffer数量
|
|||
|
Context: http, server, location
|
|||
|
|
|||
|
头信息
|
|||
|
Syntax: proxy_set_header field value;
|
|||
|
Default: proxy_set_header Host $proxy_host; #设置真实客户端地址
|
|||
|
proxy_set_header Connection close;
|
|||
|
Context: http, server, location
|
|||
|
|
|||
|
超时
|
|||
|
Syntax: proxy_connect_timeout time;
|
|||
|
Default: proxy_connect_timeout 60s; #链接超时
|
|||
|
Context: http, server, location
|
|||
|
|
|||
|
Syntax: proxy_read_timeout time;
|
|||
|
Default: proxy_read_timeout 60s;
|
|||
|
Context: http, server, location
|
|||
|
```
|
|||
|
|
|||
|
启用 nginx proxy 代理:
|
|||
|
|
|||
|
nginx-1 启动网站(内容)(作为网站服务器)
|
|||
|
|
|||
|
nginx-2 启动代理程序
|
|||
|
|
|||
|
```shell
|
|||
|
[root@nginx-server ~]# vim /etc/nginx/conf.d/default.conf
|
|||
|
server {
|
|||
|
listen 80;
|
|||
|
server_name localhost;
|
|||
|
|
|||
|
location / {
|
|||
|
proxy_pass http://10.0.105.199:80;
|
|||
|
proxy_redirect default;
|
|||
|
proxy_set_header Host $http_host;
|
|||
|
proxy_set_header X-Real-IP $remote_addr;
|
|||
|
proxy_set_header REMOTE-HOST $remote_addr;
|
|||
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|||
|
|
|||
|
proxy_connect_timeout 30;
|
|||
|
proxy_send_timeout 60;
|
|||
|
proxy_read_timeout 60;
|
|||
|
|
|||
|
proxy_buffering on;
|
|||
|
proxy_buffer_size 32k;
|
|||
|
proxy_buffers 4 128k;
|
|||
|
proxy_busy_buffers_size 256k;
|
|||
|
proxy_max_temp_file_size 256k;
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
nginx proxy 具体配置详解:
|
|||
|
|
|||
|
```shell
|
|||
|
proxy_pass :真实服务器的地址,可以是ip也可以是域名和url地址
|
|||
|
proxy_redirect :如果真实服务器使用的是的真实 IP:非默认端口。则改成IP:默认端口。
|
|||
|
proxy_set_header:重新定义或者添加发往后端服务器的请求头
|
|||
|
proxy_set_header X-Real-IP :启用客户端真实地址(否则日志中显示的是代理在访问网站)
|
|||
|
proxy_set_header X-Forwarded-For:记录代理地址
|
|||
|
|
|||
|
proxy_connect_timeout::后端服务器连接的超时时间发起三次握手等候响应超时时间
|
|||
|
proxy_send_timeout:后端服务器数据回传时间就是在规定时间之内后端服务器必须传完所有的数据
|
|||
|
proxy_read_timeout :nginx接收upstream(上游/真实) server数据超时, 默认60s, 如果连续的60s内没有收到1个字节, 连接关闭。像长连接
|
|||
|
|
|||
|
proxy_buffering on;开启缓存
|
|||
|
proxy_buffer_size:proxy_buffer_size只是响应头的缓冲区
|
|||
|
proxy_buffers 4 128k; 内容缓冲区域大小
|
|||
|
proxy_busy_buffers_size 256k; 从proxy_buffers划出一部分缓冲区来专门向客户端传送数据的地方
|
|||
|
proxy_max_temp_file_size 256k;超大的响应头存储成文件
|
|||
|
```
|
|||
|
|
|||
|
## 二:Nginx负载均衡
|
|||
|
|
|||
|
随着网站、应用访问量的增加,一台服务器已经不能满足应用的需求,而需要多台服务器集群,这时就会用到负载均衡
|
|||
|
|
|||
|
#### 1.负载均衡配置
|
|||
|
|
|||
|
```shell
|
|||
|
upstream testapp {
|
|||
|
server 10.0.105.199:8081;
|
|||
|
server 10.0.105.202:8081;
|
|||
|
}
|
|||
|
server {
|
|||
|
....
|
|||
|
location / {
|
|||
|
proxy_pass http://testapp; #请求转向 testapp 定义的服务器列表
|
|||
|
}
|
|||
|
|
|||
|
upstream mysvr {
|
|||
|
server http://10.0.105.199:8081;
|
|||
|
server http://10.0.105.202:8081;
|
|||
|
}
|
|||
|
server {
|
|||
|
....
|
|||
|
location / {
|
|||
|
proxy_pass http://mysvr; #请求转向mysvr 定义的服务器列表
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
#### 2.负载均衡算法
|
|||
|
|
|||
|
轮询(默认):每个请求按时间顺序逐一分配到不同的后端服务器
|
|||
|
|
|||
|
ip_hash:每个请求按访问IP的hash结果分配,同一个IP客户端固定访问一个后端服务器
|
|||
|
|
|||
|
url_hash:按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器
|
|||
|
|
|||
|
fair:这是比上面两个更加智能的负载均衡算法。按后端服务器的响应时间来分配请求,响应时间短的优先分配
|
|||
|
|
|||
|
注意:
|
|||
|
|
|||
|
Nginx本身是不支持 fair的,如果需要使用这种调度算法,必须下载Nginx的 upstream_fair模块
|
|||
|
|
|||
|
#### 3.配置实例
|
|||
|
|
|||
|
```shell
|
|||
|
1)、热备:如果你有2台服务器,当一台服务器发生事故时,才启用第二台服务器给提供服务。服务器处理请求的顺序:AAAAAA突然A挂啦,BBBBBBBBBBBBBB.....
|
|||
|
upstream myweb {
|
|||
|
server 172.17.14.2:8080;
|
|||
|
server 172.17.14.3:8080 backup; #热备
|
|||
|
}
|
|||
|
2)、轮询:nginx默认就是轮询其权重都默认为1,服务器处理请求的顺序:ABABABABAB....
|
|||
|
upstream myweb {
|
|||
|
server 172.17.14.2:8080;
|
|||
|
server 172.17.14.3:8080;
|
|||
|
}
|
|||
|
3)、加权轮询:跟据配置的权重的大小而分发给不同服务器不同数量的请求。如果不设置,则默认为1。下面服务器的请求顺序为:BBABBABBABBABB....
|
|||
|
upstream myweb {
|
|||
|
server 172.17.14.2:8080 weight=1;
|
|||
|
server 172.17.14.3:8080 weight=2;
|
|||
|
}
|
|||
|
4、ip_hash:nginx会让相同的客户端ip请求相同的服务器。
|
|||
|
upstream myweb {
|
|||
|
server 172.17.14.2:8080;
|
|||
|
server 172.17.14.3:8080;
|
|||
|
ip_hash;
|
|||
|
}
|
|||
|
|
|||
|
5)、nginx负载均衡配置状态参数
|
|||
|
• down,表示当前的server暂时不参与负载均衡。
|
|||
|
|
|||
|
• backup,预留的备份机器。当其他所有的非backup机器出现故障或者忙的时候,才会请求backup机器,因此这台机器的压力最轻。
|
|||
|
|
|||
|
• max_fails,允许请求失败的次数,默认为1。当超过最大次数时,返回proxy_next_upstream 模块定义的错误。
|
|||
|
|
|||
|
• fail_timeout,在经历了max_fails次失败后,暂停服务的时间单位秒。max_fails可以和fail_timeout一起使用。
|
|||
|
upstream myweb {
|
|||
|
server 172.17.14.2:8080 weight=2 max_fails=2 fail_timeout=2;
|
|||
|
server 172.17.14.3:8080 weight=1 max_fails=2 fail_timeout=1;
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
#### 4.Nginx七层负载案例
|
|||
|
|
|||
|
![image-20221009215449110](https://xingdian-image.oss-cn-beijing.aliyuncs.com/xingdian-image/image-20221009215449110.png)
|
|||
|
|
|||
|
## 三:Nginx会话保持
|
|||
|
|
|||
|
#### 1.实现方式
|
|||
|
|
|||
|
ip_hash:
|
|||
|
|
|||
|
ip_hash使用源地址哈希算法,将同一客户端的请求总是发往同一个后端服务器,除非该服务器不可用
|
|||
|
|
|||
|
语法:
|
|||
|
|
|||
|
```shell
|
|||
|
upstream backend {
|
|||
|
ip_hash;
|
|||
|
server backend1.example.com;
|
|||
|
server backend2.example.com;
|
|||
|
server backend3.example.com;
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
ip_hash简单易用,但有如下问题:
|
|||
|
|
|||
|
当后端服务器宕机后,session会丢失
|
|||
|
|
|||
|
来自同一局域网的客户端会被转发到同一个后端服务器,可能导致负载失衡
|
|||
|
|
|||
|
## 四:动静分离
|
|||
|
|
|||
|
#### 1.介绍
|
|||
|
|
|||
|
一般来说,都需要将动态资源和静态资源分开,将静态资源部署在Nginx上,当一个请求来的时候,如果是静态资源的请求,就直接到nginx配置的静态资源目录下面获取资源,如果是动态资源的请求,nginx利用反向代理的原理,把请求转发给后台应用去处理,从而实现动静分离
|
|||
|
|
|||
|
处理静态的服务:nginx apache
|
|||
|
|
|||
|
处理动态的服务:web容器/web中间件 php(php)tomcat(java) uwsgi(python)
|
|||
|
|
|||
|
#### 2.实验部署
|
|||
|
|
|||
|
环境:
|
|||
|
|
|||
|
| 服务器 | 要求 |
|
|||
|
| :-------------: | :----------------: |
|
|||
|
| 192.168.182.130 | 动静分离、upstream |
|
|||
|
| 192.168.182.131 | static |
|
|||
|
| 192.168.182.132 | php-server |
|
|||
|
|
|||
|
配置代理:
|
|||
|
|
|||
|
```shell
|
|||
|
vim /etc/nginx/nginx.conf (只是写了修改的内容)
|
|||
|
upstream static {
|
|||
|
server 10.0.105.196:80 weight=1 max_fails=1 fail_timeout=60s;
|
|||
|
}
|
|||
|
upstream phpserver {
|
|||
|
server 10.0.105.200:80 weight=1 max_fails=1 fail_timeout=60s;
|
|||
|
}
|
|||
|
|
|||
|
vim /etc/nginx/conf.d/default.conf (只是写了修改的内容)
|
|||
|
#动态资源加载
|
|||
|
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|gif|jpg|png|bmp|swf|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;
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
静态配置:
|
|||
|
|
|||
|
```shell
|
|||
|
1.安装nginx
|
|||
|
2.配置nginx的配置文件,创建创展发布目录(略),写一个测试的静态网站(index.html)(略)
|
|||
|
vim /etc/nginx/conf.d/default.conf (你要修改的配置)
|
|||
|
|
|||
|
location ~ \.(html|jpg|png|js|css|gif|bmp|jpeg)$ {
|
|||
|
root /qfedu;
|
|||
|
index index.html;
|
|||
|
}
|
|||
|
3.启动服务(关闭selinux和firewalld)
|
|||
|
```
|
|||
|
|
|||
|
动态配置:
|
|||
|
|
|||
|
```shell
|
|||
|
1.安装nginx(略),安装php
|
|||
|
yum -y install php php-fpm php-mysql
|
|||
|
2.修改配置文件
|
|||
|
vim /etc/nginx/conf.d/default.conf
|
|||
|
|
|||
|
location / {
|
|||
|
root /qfedu;
|
|||
|
index index.php index.html;
|
|||
|
}
|
|||
|
|
|||
|
location ~ \.php$ {
|
|||
|
root /qfedu;
|
|||
|
fastcgi_pass 127.0.0.1:9000;
|
|||
|
fastcgi_index index.php;
|
|||
|
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
|
|||
|
include fastcgi_params;
|
|||
|
|
|||
|
}
|
|||
|
注意:删除注释,修改内容。
|
|||
|
3.创建网站目录(略),创建一个网站
|
|||
|
index.php (文件名)
|
|||
|
<?php
|
|||
|
phpinfo();
|
|||
|
?>
|
|||
|
|
|||
|
4.关闭slinux关闭firewalld,重启服务
|
|||
|
|
|||
|
5.分别对静态和动态服务器单独测试,最后使用130的机器进行测试。
|
|||
|
```
|
|||
|
|
|||
|
#### 3.如何安装高版本php
|
|||
|
|
|||
|
安装仓库:
|
|||
|
|
|||
|
```shell
|
|||
|
[root@xingdiancloud ~]# yum install http://rpms.remirepo.net/enterprise/remirelease-7.rpm
|
|||
|
```
|
|||
|
|
|||
|
安装php:
|
|||
|
|
|||
|
```shell
|
|||
|
[root@xingdiancloud ~]# yum install yum-utils
|
|||
|
[root@xingdiancloud ~]# yum-config-manager --enable remi-php72
|
|||
|
[root@xingdiancloud ~]# yum install httpd php php-gd php-json php-mysql phpcurl php-mbstring php-intl php-mcrypt php-imagick php-xml php-zip
|
|||
|
```
|
|||
|
|
|||
|
## 五:防盗链
|
|||
|
|
|||
|
#### 1.介绍
|
|||
|
|
|||
|
两个网站 A 和 B, A网站引用了B网站上的图片,这种行为就叫做盗链。 防盗链,就是要防止A引用B的图片
|
|||
|
|
|||
|
nginx防止网站资源被盗用模块:ngx_http_referer_module
|
|||
|
|
|||
|
HTTP Referer是Header的一部分,当浏览器向Web服务器发送请求的时候,一般会带上Referer,告诉服务器我是从哪个页面链接过来的,服务器借此可以获得一些信息用于处理,例如防止未经允许的网站盗链图片、文件等。因此HTTP Referer头信息是可以通过程序来伪装生成的,所以通过Referer信息防盗链并非100%可靠,但是,它能够限制大部分的盗链情况
|
|||
|
|
|||
|
#### 2.环境
|
|||
|
|
|||
|
用来做图片服务器(防盗链的部署)192.168.182.131 (A)
|
|||
|
|
|||
|
盗链(测试防盗链是否成功) 192.168.182.132(B)
|
|||
|
|
|||
|
#### 3.部署A
|
|||
|
|
|||
|
创建一个网站发布目录,上传一张图片,写一个index.html的文件
|
|||
|
|
|||
|
```shell
|
|||
|
[root@a web]# vim index.html
|
|||
|
<html>
|
|||
|
<head>
|
|||
|
<meta charset="utf-8">
|
|||
|
<title>qf.com</title>
|
|||
|
</head>
|
|||
|
<body>
|
|||
|
<center><img src="../1.JPG" alt="xingdian" width="500px" height="900px" /></center>
|
|||
|
</body>
|
|||
|
</html>
|
|||
|
```
|
|||
|
|
|||
|
修改配置文件,修改的内容如下(部分)
|
|||
|
|
|||
|
```shell
|
|||
|
location / {
|
|||
|
root /web;
|
|||
|
index index.html index.htm;
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
#### 4.部署B
|
|||
|
|
|||
|
有一个网站发布目录,有index.html的测试网站(不需要有图片 图片直接从A盗用)
|
|||
|
|
|||
|
```shell
|
|||
|
[root@b qfedu]# vim index.html
|
|||
|
<html>
|
|||
|
<head>
|
|||
|
<meta charset="utf-8">
|
|||
|
<title>qf.com</title>
|
|||
|
</head>
|
|||
|
<body style="background-color:red;">
|
|||
|
<img src="http://192.168.182.131/1.JPG" /> //这个是你在盗用那台服务器的图片
|
|||
|
</body>
|
|||
|
</html>
|
|||
|
```
|
|||
|
|
|||
|
修改配置nginx的配置文件(部分)
|
|||
|
|
|||
|
```shell
|
|||
|
location / {
|
|||
|
root /qfedu;
|
|||
|
index index.html index.htm;
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
#### 5.测试盗用
|
|||
|
|
|||
|
当你访问B的时候,会获取到A的图片,这说明盗用成功了
|
|||
|
|
|||
|
#### 6.防盗配置A
|
|||
|
|
|||
|
修改主配置文件
|
|||
|
|
|||
|
```shell
|
|||
|
[root@a ~]# 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"';
|
|||
|
```
|
|||
|
|
|||
|
修改子配置文件
|
|||
|
|
|||
|
```shell
|
|||
|
[root@a ~]# vim /etc/nginx/conf.d/default.conf (蓝色使我们要添加的关于防盗链的配置)
|
|||
|
location ~ .*\.(gif|JPG|png|jpeg|jpg)$ {
|
|||
|
root /web;
|
|||
|
|
|||
|
valid_referers none blocked 192.168.1.10;
|
|||
|
if ($invalid_referer) {
|
|||
|
return 403;
|
|||
|
}
|
|||
|
}
|
|||
|
location / {
|
|||
|
root /web;
|
|||
|
index index.html index.htm;
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
注意:
|
|||
|
|
|||
|
none : 允许没有http_refer的请求访问资源
|
|||
|
|
|||
|
blocked : 允许不是http://开头的,不带协议的请求访问资源
|
|||
|
|
|||
|
server_names : 只允许指定ip/域名来的请求访问资源(白名单)
|
|||
|
|
|||
|
重新启动服务
|
|||
|
|
|||
|
```shell
|
|||
|
[root@a ~]# nginx -s reload
|
|||
|
```
|
|||
|
|
|||
|
访问B机器,如果B机器不在白名单,那么是访问不到内容,这样我们的防盗链就成功了,B机器在白名单内,是可以正常盗用的
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|