Nginx是一款以轻量级、低内存开销、支持缓存、支持反向代理,负载均衡,电子邮件服务而著称。对于鲜为人知的是,它还可以作为一个简单易用的正向代理服务器。
一、配置nginx正向代理服务端配置 yum install nginix -y
[root@ecs-766a62bd-920b ~]# systemctl enable nginx --now
[root@ecs-766a62bd-920b ~]# vim /etc/nginx/conf.d/proxy.conf server {
listen 8080; ##指定一个非缺省端口用于提供代理服务
server_name localhost;
resolver 114.114.114.114; ##指定DNS服务器IP
location / {
proxy_pass $scheme://$host$request_uri;
#proxy_set_header Host $http_host; #这个参数是是否启用代理IP访问过去
##proxy_pass:设置代理服务器的协议和地址以及位置应映射到的可选URI。协议可指定http或https
##proxy_set_header:与许字段重新定义或附加请求标头传递给代理服务器
proxy_buffers 256 4k; ## Author : yuanzhang
proxy_max_temp_file_size 0; ## Blog : https://myit.icu
##proxy_buffers:为单个连接设置用于从代理服务器读取响应的缓冲区个数和缓冲区大小
##proxy_max_temp_file_size:禁用缓冲对临时文件的响应
proxy_connect_timeout 30; ##代理连接超时时间
proxy_cache_valid 200 302 10m; ##为不同的响应代码设置缓存时间
proxy_cache_valid 301 1h;
proxy_cache_valid any 1m;
}
}
[root@ecs-766a62bd-920b ~]# systemctl reload nginx.service [root@ecs-766a62bd-920b ~]# systemctl restart nginx [root@ecs-766a62bd-920b ~]# ss -nltp|grep nginx LISTEN 0 128 :80 :* users:(("nginx",pid=12901,fd=7),("nginx",pid=12900,fd=7),("nginx",pid=12899,fd=7)) LISTEN 0 128 :8080 :* users:(("nginx",pid=12901,fd=6),("nginx",pid=12900,fd=6),("nginx",pid=12899,fd=6)) LISTEN 0 128 :::80 :::* users:(("nginx",pid=12901,fd=8),("nginx",pid=12900,fd=8),("nginx",pid=12899,fd=8))
二、客户端配置 [root@idc ~]#export http_proxy=http://42.51.227.134:8080 [root@idc ~]#curl -I http://www.baidu.com HTTP/1.1 200 OK Server: nginx/1.16.1 Date: Wed, 06 Jan 2021 08:58:11 GMT Content-Type: text/html Content-Length: 277 Connection: keep-alive Accept-Ranges: bytes Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform Etag: "575e1f60-115" Last-Modified: Mon, 13 Jun 2016 02:50:08 GMT Pragma: no-cache
[root@idc ~]#unset http_proxy [root@idc ~]#wget -e "http_proxy=http://42.51.227.134:8080" www.baidu.com --2021-01-06 16:58:45-- http://www.baidu.com/ Connecting to 42.51.227.134:8080... connected. Proxy request sent, awaiting response... 200 OK Length: 2381 (2.3K) [text/html] Saving to: ‘index.html’
100%[==================================================================================================================================================================================================>] 2,381 --.-K/s in 0s
2021-01-06 16:58:45 (216 MB/s) - ‘index.html’ saved [2381/2381]
[root@idc ~]#curl -x http://42.51.227.134:8080 -I http://www.baidu.com HTTP/1.1 200 OK Server: nginx/1.16.1 Date: Wed, 06 Jan 2021 08:58:59 GMT Content-Type: text/html Content-Length: 277 Connection: keep-alive Accept-Ranges: bytes Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform Etag: "575e1f60-115" Last-Modified: Mon, 13 Jun 2016 02:50:08 GMT Pragma: no-cache