什么是代理
我们一些同学使用的”梯子“ “魔法” "vpn"等其实就是一种代理方式,我使用的手机通过挂代理访问到一个国外的服务器,这个国外的服务器可以上外网,我们向这个外网服务器发送请求,比如说上youtube,这个外网服务器就会去请求youtube官网拿到资源后,再将这个资源返回给我们的手机或电脑,这就是采用的一种正向代理的方式,还有一种代理叫反向代理,他俩的区别在于服务对象不同,正向代理服务于客户端,反向代理是服务于服务器的
nginx代理常见模式
正向代理
反向代理
nginx做代理
nginx就是一个典型的代理服务器
nginx反向代理实战
192.168.208.10 代理服务器
192.168.208.20 服务器
本机localhost访问10
20服务器 配置监听在8080端口
[root@web01 conf.d]# vim /etc/nginx/conf.d/web.conf
server {
listen 8080;
server_name localhost;
location / {
root /code/8080;
index index.html;
allow all;
}
}
[root@web01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 conf.d]# systemctl restart nginx
[root@web01 ~]# mkdir -p /code/8080
[root@web01 ~]# echo "listening 8080 ..." > /code/8080/index.html
10 proxy代理服务器监听在80端口 虚拟主机
vim /etc/nginx/conf.d/proxy.conf
server{
listen 80;
server_name proxy.test.com;
location / {
proxy_pass 192.168.208.20:8080;
}
}
nginx -t
systemctl restart nginx
proxy_set_header模块
[root@proxy conf.d]# vim proxy_web_node1.conf
server {
listen 80;
server_name proxy.test.com;
location / {
proxy_pass http://192.168.175.20:8080;
proxy_set_header Host $http_host;
}
}
http1.1协议
server {
listen 80;
server_name proxy.test.com;
location / {
proxy_pass http://192.168.175.20:8080;
proxy_set_header Host $http_host;
proxy_http_version 1.1;
}
}
proxy_set_header
在生产环境中,我们必须要记录客户端的来源IP,如果所有的访问日志,全都来源于代理,那么我们根本不知道都有哪些地区的用户访问了我们什么页面。
server {
listen 80;
server_name proxy.test.com;
location / {
proxy_pass http://192.168.175.20:8080;
proxy_set_header Host http_host;
proxy_http_version 1.1;
proxy_set_header X-Forwarded-Forproxy_add_x_forwarded_for;
}
}
Comments NOTHING