您的位置:首页 > 运维架构 > Nginx

LNMP--Nginx代理详解

2015-08-12 12:59 579 查看
Nginx可以实现代理的功能,我们有个需求,现在国内已经把谷歌给禁止掉了,美国可以访问,香港可以访问,那么我们现在可以做一个代理,可以实现通过一个代理去访问百度,谷歌等等任何一个网站,我们来学习代理怎么去配置:(以代理baidu为例)

要先知道百度的IP:
[root@LampLinux ~]# ping www.baidu.com
PING www.a.shifen.com (180.97.33.108) 56(84) bytes of data.
64 bytes from 180.97.33.108: icmp_seq=1 ttl=55 time=19.3 ms
64 bytes from 180.97.33.108: icmp_seq=2 ttl=55 time=18.9 ms
64 bytes from 180.97.33.108: icmp_seq=3 ttl=55 time=18.0 ms
编写代理配置文件:
[root@LampLinux ~]# vim /usr/local/nginx/conf/vhosts/proxy.conf
server
{
listen 80;
server_name www.baidu.com;

location / {
proxy_pass http://180.97.33.108/; proxy_set_header HOST $host;
}

}
检查并重加载
[root@LampLinux ~]# /usr/local/nginx/sbin/nginx -t
[root@LampLinux ~]# /usr/local/nginx/sbin/nginx -s reload
测试把百度的IP指向本机,去访问百度:
[root@LampLinux ~]# curl -x127.0.0.1:80 www.baidu.com
显示正常访问。

这就是Nginx的代理。

我们也可以指定多台机器,也就是说指定的IP(180.97.33.108)可以是多个,在这种多个的情况下,可以实现“负载均衡”,怎么去配置多个机器访问百度?
我们先看看百度他解析到了哪里

先将一个命令安装:
[root@LampLinux ~]# yum install bind*
(dig命令有“探测某个域名解析到哪个IP的”作用)

[root@LampLinux ~]# dig www.baidu.com
180.97.33.108
180.97.33.107
(找到两个IP)
我们想这两个IP我们都能去代理,都能去访问,这样去配置代理文件:
[root@LampLinux ~]# vim /usr/local/nginx/conf/vhosts/proxy.conf
upstream lnmplinux { # 绿字部分为“自定义”名称
server 180.97.33.108:80;
server 180.97.33.107:80;
}
server
{
listen 80;
server_name www.baidu.com;

location / {
proxy_pass http://lnmplinux/; # 此处要和第一句的“自定义”名称一致
proxy_set_header HOST $host; # 这句是配置多个机器“负载均衡”必须要写的
}

}
检查,并重加载
[root@LampLinux ~]# /usr/local/nginx/sbin/nginx -t
[root@LampLinux ~]# /usr/local/nginx/sbin/nginx -s reload
去访问百度:
[root@LampLinux ~]# curl -x127.0.0.1:80 www.baidu.com
成功!

这一节整理:
[root@LampLinux ~]# vim /usr/local/nginx/conf/vhosts/proxy.conf
upstream lnmplinux{
server 180.97.33.108:80;
server 180.97.33.107:80;
}
server
{
listen 80;
server_name www.baidu.com;

location / {
proxy_pass http://lnmplinux/; proxy_set_header HOST $host;
}

}
至此,LNMP的学习就结束了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Nginx代理