代理,顾名思义就是通过中间代理服务器去完成客户端和服务器端的通信,因为某些资源无法直接通过客户端从服务器端请求得到,如国内访问谷歌资源,就需要用代理服务器,将国内ip转换为国外ip,然后去访问资源。
正向代理

上图显示,服务端的图片资源只能通过69网段请求得到,Nginx配置如下:
//nginx.conf
location ~ .*\.(jpg|gif|png)$ { allow 192.168.69.0/24; deny all; root /soft/code/images; }
server { listen 80; resolver 233.5.5.5; location / { proxy_pass http://$http_host$request_uri; proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
反向代理
location / {
root html;
index index.html index.htm;
#只需要加下面这段代码即可
proxy_pass http://blog.csdn.net;
}
Nginx反向代理常用配置
#... http {
log_format main '$remote_addr "$http_x_real_ip" - $remote_user [$time_local] "$request" "$http_host" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; server{
#... location /reverseproxy/ {
proxy_pass https://127.0.0.1:8081; include proxy_params; } #... } } #... //proxy_params #$host对应log_format中的$http_host,代理服务器和web服务器该值都为客户端请求的ip #proxy_set_header Host $host; #对于代理服务器,$http_host的值就是客户端请求的ip,对于web服务器,该值为代理服务器ip proxy_set_header Host $proxy_host; #对于web服务器,通过$http_x_real_ip可以获取真实客户端ip proxy_set_header X-Real-IP $remote_addr; #也可以通过下面这种方式,在web服务器的log中通过$http_x_forwarded_for获取真实客户端ip #proxy_set_header X-Forwarded-For $remote_addr; #当有多级代理的时候,显示:realclient-ip,proxy1ip,proxy2ip proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #与后端服务器建立连接的超时时间 proxy_connect_timeout 30; #向后端服务器发送请求的超时时间 proxy_send_timeout 60; #从后端服务器读取响应的超时时间 proxy_read_timeout 60; proxy_buffer_size 32k; proxy_buffering on; proxy_buffers 4 128k; proxy_busy_buffers_size 256k; proxy_max_temp_file_size 256k;
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/176801.html原文链接:https://javaforall.net
