Nginx反向代理05

Nginx反向代理

概述

正向代理的代理对象是客户端, 而反向代理的代理对象是服务端

image-20211215160706151

正向代理实战

image-20211215181140687

配置134服务器

nginx服务
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
http {
include mime.types;
default_type application/octet-stream;
keepalive_timeout 65;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
log_format proxy 'client send request from $remote_addr to $host';
server{
root /var/www/test;
listen 80;
default_type text/plain;
access_log /var/log/nginx/access.log proxy;
}
include /etc/nginx/sites-enabled/*;
}
访问页面结果

image-20211215181811763

监控访问日志

image-20211215181916864

配置135代理服务器

nginx服务
1
2
3
4
5
6
7
server{
listen 82;
location /{
# resolver 8.8.8.8; # 用于设置解析proxy_pass中域名
proxy_pass http://$host$request_uri;
}
}
客户端设置代理

1200628e43314ad0d024781-5faa8fe

监控访问日志

image-20211215182241957

反向代理配置语法

Nginx反向代理由nginx自带模块ngx_http_proxy_module进行解析, 常见指令包括

1
2
3
proxy_pass
proxy_set_header
proxy_redirect
1
文档: http://nginx.org/en/docs/http/ngx_http_proxy_module.html

proxy_pass

用于设置被代理服务器地址, 可以为主机名称、IP+端口号

语法: proxy_pass URL;

位置: location块

URL为要设置的被代理的服务器地址, 包含协议(http://, https://)、主机名称或IP+端口

1
2
proxy_pass http://www.baidu.com;
proxy_pass http://192.168.253.134/;
是否需要加/
1
2
3
4
5
6
7
8
9
server{
listen 80;
server_name localhost;
location /{
# proxy_pass http://192.168.253.134;
proxy_pass http://192.168.253.134/;
}
}
# 客户端访问 http://localhost/index.html 时, 显示访问结果一样
1
2
3
4
5
6
7
8
9
10
11
server{
listen 80;
server_name localhost;
location /server{
# proxy_pass http://192.168.253.134;
proxy_pass http://192.168.253.134/;
}
}
# 客户端访问 http://localhost/server/index.html 时
# 第一个配置会访问 http://localhost/server/index.html
# 第二个配置会访问 http://localhost/index.html

配置proxy_pass时,当在后面的url加上了/,相当于是绝对根路径,则nginx不会把location中匹配的路径部分代理走;如果没有/,则会把匹配的路径部分也给代理走

proxy_set_header

用于更改Nginx代理服务器收到的客户端的请求头信息,并将新的请求头信息发送给被代理服务器

语法: proxy_set_header field value;

默认值: proxy_set_header Host $proxy_host; proxy_set_header Connection close;

位置: http server location 块

案例:获取客户端IP

想要获取客户端的真实IP而不是代理服务器IP

代理服务器配置(192.168.253.135)

1
2
3
4
5
6
7
8
9
server{
listen 80;
location /{
# resolver 8.8.8.8; # 用于设置解析proxy_pass中域名
proxy_pass http://192.168.253.134/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

被代理服务器配置(192.168.253.134)

1
2
3
4
5
6
7
8
9
server{
root /var/www/test;
listen 80;
default_type text/plain;
access_log /var/log/nginx/access.log proxy;
location /{
return 200 "$http_host==>$http_x_real_ip==>$http_x_forwarded_for";
}
}

访问代理服务器 http://192.168.253.135/

image-20211215191626909

proxy_redirect

用于重置头信息中的 Location 和 Refresh 信息

语法: proxy_redirect redirect replacement; | proxy_redirect default; | proxy_redirect off;

默认值: default

位置: http server location 块

设置被代理服务器(192.168.253.134)

1
2
3
4
5
6
7
8
9
10
11
server{
root /var/www/test;
listen 8081;
if (!-f $request_filename){
return 302 http://192.168.253.134;
}
}
server{
root /var/www/test;
listen 80;
}

设置代理服务器(192.168.253.135)

1
2
3
4
5
6
7
8
9
10
11
12
13
server{
listen 8081;
location /{
proxy_pass http://192.168.253.134:8081/;
proxy_redirect http://192.168.253.134/ http://192.168.253.135/;
}
}
server{
listen 80;
location /{
proxy_pass http://192.168.253.134/;
}
}

从而避免暴露被代理服务器

反向代理实战

image-20211215212251372

配置代理服务器(134)

1
2
3
4
5
6
7
8
9
10
11
12
13
server{
listen 80;
server_name localhost;
location /server1{
proxy_pass http://192.168.253.135/;
}
location /server2{
proxy_pass http://192.168.253.136/;
}
location /server3{
proxy_pass http://192.168.253.137/;
}
}

配置被代理服务器(135 136 137)

1
2
3
4
5
6
7
8
9
10
server{
listen 80;
server_name localhost;
location /{
default_type text/plain;
return 200 '192.168.253.135';
# return 200 '192.168.253.136';
# return 200 '192.168.253.137';
}
}

访问结果如下

image-20211215213813825

反向代理系统优化

利用缓冲区Buffer和缓存Cache提升IO吞吐效率

1
2
缓冲主要用于解决不同设备间数据传输速度不一致, 导致性能低下的问题, 缓冲中的数据完成操作后删除
缓存主要用于备份, 当客户端再次获取相同数据时, 可以从代理服务器上获取, 满足特定条件后删除

Proxy Buffer指令

  • proxy_buffering: 用于开启或关闭代理服务缓冲区

    语法: proxy_buffering on|off;

    默认值: on

  • proxy_buffers: 用于指定单个连接从代理服务器读取响应的缓冲区个数和大小

    语法: proxy_buffers number size;

    默认值: 8 4k (与平台有关)

  • proxy_buffer_size: 用于设置从被代理服务器获取的第一部分响应数据大小

    语法: proxy_buffer_size size;

    默认值: 4k|8K (与平台有关)

    保持与proxy_buffers中size相同即可

  • proxy_busy_buffers_size: 用于限制同时处于BUSY状态的缓冲区总大小

    语法: proxy_busy_buffers_size size;

    默认值: 8k|16k

  • proxy_temp_path : 当缓冲区满后, 仍未被nginx服务器完全接受, 响应数据就会被临时存放在磁盘上

    语法: proxy_temp_path path;

    默认值: proxy_temp

    path 最大目录深度为3层

  • proxy_temp_file_write_size: 用于设置磁盘上缓冲文件大小

    语法: proxy_temp_file_write_size size;

    默认值: 8k|16k

通用配置

1
2
3
4
proxy_buffering on;
proxy_buffer_size 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;

使用OpenSSL生成证书文件

1
2
3
4
5
6
7
8
9
10
mkdri /root/cert
cd /root/cert
openssl genrsa -des3 -out server.key 1024
# 设定密码
openssl req -new -key server.key -out server.csr
# 输入密码, 验证信息
cp server.key server.key.org
openssl rsa -in server.key.org -out server.key
# 验证密码
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

生成文件: server.key | server.crt

开启SSL实例

详细官方文档: https://nginx.org/en/docs/http/ngx_http_ssl_module.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
server{
# ssl on;
listen 443 ssl;
server_name localhost;

ssl_certificate server.crt; # 指定证书文件路径
ssl_certificate_key server.key; # 指定证书key文件路径

ssl_session_cache shared:SSL:1m; # 配置用于SSL会话缓存
# off-禁用, 客户端不得重复使用会话
# none-禁用, 客户端可以重复使用, 但并没有在缓存中存储会话参数
# builtin-内置OpenSSL缓存,仅在一个工作进程中使用
# shared-所有工作进程间共享缓存, 相关信息由name和size指定
ssl_session_timeout 5m; # 开启会话后,客户端能够反复使用存储会话时间

ssl_ciphers HIGH:!aNULL:!MD5; # 允许密码格式,可以用openssl ciphers查看支持格式
ssl_prefer_server_ciphers on; # 指定是否服务器密码优先于客户端密码

location /{
root html;
index index.html index.htm;
}
}

自动转换为https协议

1
2
3
4
5
6
7
8
9
10
11
12
location /{
...
rewrite ^(.*) https://[server_name]$1;
}



server {
if ($host = [server_name]) {
return 301 https://$host$request_uri;
}
}