nginx_Proxy

nginx 代理实现的模块

ngx_http_proxy_module

 

正向代理

正向代理时,访问的域名不固定(如:www.cnblogs.com,www.baidu.com等),令任何域名都可以访问正向代理服务器的这个虚拟机(默认)上。

server {
    listen 80 default_server;
    resolver  119.29.29.29;                    # 国内的通用DNS地址。

    location / {
        proxy_pass   http://$host$request_uri;  # 客户端的访问url,同时也是正向代理向外网访问的url
   }
}

server {
resolver 114.114.114.114;
listen 443;

location / {
proxy_pass https://$host$request_uri; # 代理 https 协议
proxy_buffers 256 4K;
proxy_max_temp_file_size 0K;
proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_next_upstream error timeout invalid_header http_502;
}
}

 

指令语法:resolver   address;

参考:http://blog.51cto.com/liqingbiao/2069726

 

客户端 windows 设置

IE浏览器   --> 工具  --> Internet选项  --> 局域网设置 ---- 代理服务器 

 

linux 客户端 设置

curl 访问

curl -I  --proxy proxy_server:80  http://baidu.com
curl -I --proxy proxy_server:443 https://cnblogs.com

 

yum 设置代理

方法 1:

vim /etc/yum.conf proxy=http://192.168.6.90:80 # 代理服务器的IP:端口 proxy=ftp://192.168.6.90:80 #proxy_username=username # 代理的用户名 #proxy_password=password

方法 2:
vim /etc/yum.conf
proxy=http://username:password@proxy_server:80/ # 该写法是 http协议中的通用访问 url 的语法

 

wget 设置代理

vim /etc/wgetrc
http_proxy=192.168.6.90:80
http_proxy=192.168.6.90:443

 

全局 设置代理

vim /etc/profile
http_proxy = http://192.168.6.90:80
http_proxy = http://192.168.6.90:443
ftp_proxy = http://192.168.6.90:80/
export http_proxy
export ftp_proxy

 

 

 

反向代理

配置如下:

server  {
    listen                80;
    server_name     cnblogs.com;

    location  /   {
        proxy_pass          http://10.0.0.30/;     # 内网的或外网的服务器IP
        proxy_set_header    Host             $host;      #  客户端请求的域名,若没有这个字段,那么若后端的服务器有多个虚拟主机(多个域名时)时,nginx则反馈默认的虚拟机(或配置文件中的第一个虚拟主机)
        proxy_set_header    X-Real-IP        $remote_addr;                 #  解决多层代理时 X-Forward-Fox 被过程中的某一级代理服务器的IP所替代
        proxy_set_header    X-Forwarded-For  $proxy_add_x_forwarded_for;   #  经过每一次代理做一次转发,X-Forward-For就会在后面追加一个代理IP,以逗号分隔的ip串,以转发顺序排序。格式:clientIP,proxy1_ip,proxy2_ip ...
   }
}

 

后端的Nginx的配置文件中日志记录变量

nginx可以定义$http_x_real_ip变量接收前面代理的x-real-ip,例如:
log_format main '$http_x_real_ip - $remote_user '
                '[$time_local] "$request_method $scheme://$host$request_uri $server_protocol" '
                '$status $body_bytes_sent "$http_referer" '
                '"$http_user_agent" "$http_x_forwarded_for" - $remote_addr';

apache可以通过%{X-Real-Ip}i来获取:
LogFormat "%{X-Real-Ip}i %l %u %t \"%m http://%v%U%q\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" - %h" combinedall

 

 

参考:

http://blog.51cto.com/nicking/1431056

https://www.cnblogs.com/lianye920129/p/7889254.html

https://www.cnblogs.com/mypath/articles/5239687.html

https://imququ.com/post/x-forwarded-for-header-in-http.html

 

转载于:https://www.cnblogs.com/BoNuo/articles/10274476.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值