120秒搞懂 nginx 反向代理
接下来说说我的个人理解,所谓代理,其实就是帮助你访问你原本访问不到的地址,简单点说,你想访问世界上最大的p站,但是你爱国,你访问不了,怎么办,没关系,你开了个代理.让代理去给你请求你想要的东西.就像小时候,金秋时节,别人家的柿子熟了,想吃一个,够不到,怎么办?拿根棍子啊,一棍子下去,咚咚几声,地下就会爬伏几坨黄黄的东西.....这根棍子,就是你开的代理,它帮你打柿子
我现在想这样,我访问172.17.0.3/hello的时候,走172.17.0.4
[localhost ~] curl 172.17.0.3
8081
[localhost ~] curl 172.17.0.3/hello.html
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.19.0</center>
</body>
</html>
[localhost ~] curl 172.17.0.4
8082
[localhost ~]
[localhost ~] cat conf1/conf.d/default.conf
server {
listen 80;
server_name localhost;
location /hello.html {
proxy_pass http://172.17.0.4;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
[localhost ~]
[localhost ~] docker exec -it nginx8081 bash
ffe603d426f2:/ service nginx restart
[....] Restarting nginx: nginx[localhost ~]
[localhost ~] curl 172.17.0.3
8081
[localhost ~] curl 172.17.0.4
8082
[localhost ~] curl 172.17.0.3/hello.html
hello
[localhost ~] curl 172.17.0.4/hello.html
hello
[localhost ~]
[localhost ~] cat conf1/conf.d/default.conf
server {
listen 80;
server_name localhost;
location /hello.html {
proxy_pass http://172.17.0.4;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /a/b/c/d/hello {
proxy_pass http://172.17.0.4;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
[localhost ~] docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1a4fc8f64447 nginx "/docker-entrypoint.…" 4 hours ago Up 4 hours 0.0.0.0:8083->80/tcp nginx8083
56dcd4def721 nginx "/docker-entrypoint.…" 4 hours ago Up 4 hours 0.0.0.0:8082->80/tcp nginx8082
ffe603d426f2 nginx "/docker-entrypoint.…" 4 hours ago Up 42 minutes 0.0.0.0:8081->80/tcp nginx8081
72aacbe3c63a nginx "/docker-entrypoint.…" 4 hours ago Up 3 hours 0.0.0.0:8080->80/tcp nginx8080
2a7229a5ef30 hello-world "/hello" 7 hours ago Exited (0) 7 hours ago dreamy_hamilton
54368a05aabf hello-world "/hello" 7 hours ago Exited (0) 7 hours ago mystifying_hawking
查看被代理的服务器日志
[localhost ~] docker logs -f 56dcd4def721
172.17.0.1 - - [02/Jul/2020:18:20:34 +0000] "GET /hello.html HTTP/1.1" 200 6 "-" "curl/7.29.0" "-"
2020/07/02 18:20:49 [error] 2121: *32 open() "/usr/share/nginx/html/a/b/c/d/hello" failed (2: No such file or directory), client: 172.17.0.3, server: localhost, request: "GET /a/b/c/d/hello HTTP/1.0", host: "172.17.0.4"
location /static/admin {
return 503;
alias /opt/app/public/static/admin;
proxy_pass http://127.0.0.1:8086/static/;
}
相信你一定可以看明白的
[localhost ~] cat conf1/conf.d/default.conf
缓存路径 levels=1:2代表缓存的目录结构为2级目录
proxy_cache_path /usr/share/nginx/html/cache/cache levels=1:2 keys_zone=cache:20m max_size=1g;
proxy_cache_key "$host$request_uri";
server {
listen 80;
server_name localhost;
add_header X-Via $server_addr;
add_header X-Cache $upstream_cache_status;
location /hello.html {
proxy_pass http://172.17.0.4;
反向代理缓存设置命令
proxy_cache cache;
设置指定状态码的缓存时间 状态200 缓存10分钟
proxy_cache_valid any 10m;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /a/b/c/d/hello {
proxy_pass http://172.17.0.4;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
[localhost cache] ls
[localhost cache] pwd
/root/www1/cache
[localhost cache]
接下来访问第一次,发现目录下已有缓存文件,两级目录,文件内容就是hello.html的内容,
[localhost cache] curl 172.17.0.3/hello.html
hello
[localhost cache] cd cache/
[localhost cache] ls
2
[localhost cache] cat 2/17/0568faac7d714cd625a00aebf6968172
"5efe1ea5-6"
KEY: 172.17.0.3/hello.html
HTTP/1.1 200 OK
Server: nginx/1.19.0
Date: Thu, 02 Jul 2020 19:35:32 GMT
Content-Type: text/html
Content-Length: 6
Last-Modified: Thu, 02 Jul 2020 17:51:33 GMT
Connection: close
ETag: "5efe1ea5-6"
Accept-Ranges: bytes
hello
[localhost cache] curl -i 172.17.0.3/hello.html
HTTP/1.1 200 OK
Server: nginx/1.19.0
Date: Thu, 02 Jul 2020 19:41:08 GMT
Content-Type: text/html
Content-Length: 6
Connection: keep-alive
Last-Modified: Thu, 02 Jul 2020 17:51:33 GMT
ETag: "5efe1ea5-6"
X-Via: 172.17.0.3
X-Cache: HIT
Accept-Ranges: bytes
hello
[localhost ~] curl -i 172.17.0.3/hello.html
HTTP/1.1 200 OK
Server: nginx/1.19.0
Date: Thu, 02 Jul 2020 19:44:09 GMT
Content-Type: text/html
Content-Length: 6
Connection: keep-alive
Last-Modified: Thu, 02 Jul 2020 17:51:33 GMT
ETag: "5efe1ea5-6"
X-Via: 172.17.0.3
X-Cache: HIT
Accept-Ranges: bytes
hello
[localhost ~] curl -i 172.17.0.4/hello.html
HTTP/1.1 200 OK
Server: nginx/1.19.0
Date: Thu, 02 Jul 2020 19:44:17 GMT
Content-Type: text/html
Content-Length: 10
Last-Modified: Thu, 02 Jul 2020 19:44:03 GMT
Connection: keep-alive
ETag: "5efe3903-a"
Accept-Ranges: bytes
hello8802
[localhost ~]
本文使用 mdnice 排版