正向代理代理的对象是客户端,反向代理代理的是服务端,Nginx即可以实现正向代理,也可以实现反向代理。
正向代理
反向代理
理论上Nginx可以支持正向代理上网,但是在实验中,域名访问有问题,尚未解决,只能使用ip上网
server {resolver 8.8.8.8;listen 8078;location / {proxy_pass $scheme://$http_host$request_uri;}
}
浏览器配置
通过ip访问
客户端访问服务端,但是如果使用了代理,那么服务端能看到的只是代理发送过去的请求,也即$remote_addr变成代理服务器的ip了
Nginx反向代理模块的指令是由ngx_http_proxy_module
模块进行解析,该模块在安装Nginx的时候已经自己加装到Nginx中了。
用来设置被代理服务器地址,可以是主机名称、IP地址加端口号形式;
URL:为要设置的被代理服务器地址,包含传输协议(http
,https://
)、主机名称或IP地址加端口号、URI等要素。
语法 | proxy_pass URL; |
---|---|
默认值 | — |
位置 | location |
# 172.41.100.15 nginx配置
server {listen 8078;location / {proxy_pass http://172.41.100.14:8090;}location /proxy {proxy_pass http://172.41.100.14:8090;}location /proxy2 {proxy_pass http://172.41.100.14:8090/;}
}
# 172.41.100.14 nginx配置
server {listen 8090;location / {root html;index index.html;}
}
http://172.41.100.15:8078
页面正常展示
http://172.41.100.15:8078/proxy
页面报404,因为配置的路径没有加/
,nginx去172.41.100.14服务器的nginx安装目录下的html/proxy找index.html页面
2021/08/18 13:53:15 [error] 18911#0: *55 open() "/usr/local/nginx/html/proxy" failed (2: No such file or directory), client: 172.41.100.15, server: , request: "GET /proxy HTTP/1.0", host: "172.41.100.14:8090"
http://172.41.100.15:8078/proxy2
页面正常展示
用于更改Nginx服务器接收到的客户端请求的请求头信息,然后将新的请求头发送给代理的服务器
语法 | proxy_set_header field value; |
---|---|
默认值 | proxy_set_header Host $proxy_host;proxy_set_header Connection close; |
位置 | http、server、location |
# 172.41.100.15 nginx配置
server {#防止中文乱码charset utf-8;listen 8078;location / {proxy_set_header songhongwei 宋宏伟好帅;proxy_pass http://172.41.100.14:8090;}
}# 172.41.100.14 nginx配置
server {#防止中文乱码charset utf-8;listen 8090;location / {default_type text/plain;return 200 $http_songhongwei;}
}
http://172.41.100.15:8078/
效果用来重置头信息中的"Location"和"Refresh"的值,目的是为了隐藏服务端信息,否则客户端可以获取服务端相关信息。
当上游服务器返回的响应是重定向或刷新请求(如HTTP响应码是301或者302)时,proxy_redirect可以重设HTTP头部的location或refresh字段。
| 语法 | proxy_redirect redirect replacement;
proxy_redirect default;
proxy_redirect off; |
| — | — |
| 默认值 | proxy_redirect default; |
| 位置 | http、server、location |
redirect:目标,Location的值
replacement:要替换的值
将location块的uri变量作为replacement,
将proxy_pass变量作为redirect进行替换
关闭proxy_redirect的功能
# 172.41.100.15 nginx 配置
server{listen 8079;server_name localhost;location /{proxy_pass http://172.41.100.14:8090;proxy_redirect http://172.41.100.14 http://172.41.100.15;}location /proxy {proxy_pass http://172.41.100.14:8090;}
}server {listen 80;server_name localhost;location / {proxy_pass http://172.41.100.14;}
}# 172.41.100.14 nginx配置
server {server_name localhost;listen 8090;location / {return 302 http://172.41.100.14;}
}
server {listen 80;server_name localhost;location /{root html;index index.html;}
}
http://172.41.100.15:8079/proxy
地址栏被重定向为http://172.41.100.14/
,响应头的Location返回了服务端的信息。
http://172.41.100.15:8079/
地址栏被重定向为http://172.41.100.15/
,响应头的Location未返回服务端的信息。
如果服务器1、服务器2和服务器3的内容不一样,那我们可以根据用户请求来分发到不同的服务器。
# 172.41.100.15 nginx配置,用不同的location来区分请求,这里用端口来区分服务器,实际生产可能对应多台不同的nginx主机
server {listen 8082;server_name localhost;location /server1 {proxy_pass http://172.41.100.14:9001/;}location /server2 {proxy_pass http://172.41.100.14:9002/;}location /server3 {proxy_pass http://172.41.100.14:9003/;}
}# 172.41.100.14 nginx配置
server {listen 9001;server_name localhost;default_type text/html;return 200 '172.41.100.14:9001
';
}
server {listen 9002;server_name localhost;default_type text/html;return 200 '172.41.100.14:9002
';
}
server {listen 9003;server_name localhost;default_type text/html;return 200 '172.41.100.14:9003
';
}
http://172.41.100.15:8082/server1
返回172.41.100.14:9001
http://172.41.100.15:8082/server2
返回172.41.100.14:9002
http://172.41.100.15:8082/server3
返回172.41.100.14:9003
代理服务内容一致,nginx相当于负载均衡器,这里先给出一个例子,具体的在负载均衡里详细说明
# 172.41.100.15 nginx配置,用不同的location来区分请求,这里用端口来区分服务器,实际生产可能对应多台不同的nginx主机
upstream backend{server 172.41.100.14:9001;server 172.41.100.14:9002;server 172.41.100.14:9003;
}
server {listen 8083;server_name localhost;location /{proxy_pass http://backend;}
}# 172.41.100.14 nginx配置
server {listen 9001;server_name localhost;default_type text/html;return 200 '172.41.100.14:9001
';
}
server {listen 9002;server_name localhost;default_type text/html;return 200 '172.41.100.14:9002
';
}
server {listen 9003;server_name localhost;default_type text/html;return 200 '172.41.100.14:9003
';
}
http://172.41.100.15:8083/
并不断刷新,可以看到页面的内容在变化