正向代理和反向代理的区别
如图中:
正向代理:client和proxy在同一个局域网,server只知道proxy的存在,不晓得client的存在。正向代理会隐藏掉client的信息。
反向代理:proxy和server在同一个局域网,client只知道proxy的存在,不晓得server的存在。反向代理会隐藏掉server的信息。
nginx配置反向代理
#user nobody;
worker_processes 4;
error_log logs/debug.log debug;
events {
worker_connections 1024;
}
http {
#include mime.types;
#default_type application/octet-stream;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
server {
client_max_body_size 4G;
listen 80;
root /data;
location ~*.mp4$ {
proxy_pass http://127.0.0.1:6666/${uri}; # 代理到本机的6666端口
autoindex on;
autoindex_exact_size on;
autoindex_localtime on;
}
}
server {
client_max_body_size 4G;
listen 127.0.0.1:6666;
root /data;
location ~*.mp4$ {
autoindex on;
autoindex_exact_size on;
autoindex_localtime on;
}
}
}
client访问80端口是会通过proxy_pass代理到本地127.0.0.1的6666端口,则可以简单模拟从对外的80端口反向代理到对内的6666端口的实现。
通过curl测试
# curl -L -v http://192.168.116.130:80/a.mp4 -o 1.mp4
* Trying 192.168.116.130...
* TCP_NODELAY set
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* Connected to 192.168.116.130 (192.168.116.130) port 80 (#0)
> GET /a.mp4 HTTP/1.1
> Host: 192.168.116.130
> User-Agent: curl/7.58.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Server: nginx/1.18.0
< Date: Sat, 29 Aug 2020 04:03:43 GMT
< Content-Type: text/plain
< Content-Length: 41780
< Connection: keep-alive
< Last-Modified: Wed, 24 Jun 2020 12:11:53 GMT
< ETag: "5ef34309-a334"
< Accept-Ranges: bytes
<
{ [13795 bytes data]
100 41780 100 41780 0 0 1854k 0 --:--:-- --:--:-- --:--:-- 2040k
* Connection #0 to host 192.168.116.130 left intact
开启nginx debug日志可以发现,nginx内部代理到了本机6666端口去请求了
2020/08/28 21:03:43 [debug] 5072#0: *1 http proxy header:
"GET //a.mp4 HTTP/1.0
Host: 127.0.0.1:6666
Connection: close
User-Agent: curl/7.58.0
Accept: */*
"