接上一篇博客,通过nginx 实现方向代理,实现通过 www.nginx.com/local_62 可以转发到本地 62的http应用上通过 www.nginx.com/helloworld 可以转发到百度上面
1、修改配置文件
vim /opt/nginx/conf/nginx.conf
server 配置如下
server {
listen 80;
server_name www.nginx.com;
location / {
root html;
index index.html index.htm;
}
location /local_62 {
proxy_pass http://192.168.8.62/;
#62带/ 表示访问http://192.168.8.62/ 对应的首页
#62不带/ 表示访问http://www.nginx.com/local_62
}
location /helloworld {
proxy_pass https://www.baidu.com/;
# 需要注意的是如果配置的 http 则访问以后会直接跳转到百度。
#www.baidu.com带/ 表示访问https://www.baidu.com/ 对应的首页
#不带/ 表示访问http://www.nginx.com/helloworld
}
}
2、重新加载配置文件
service nginx reload
3、验证
浏览器输入
http://www.nginx.com/local_62
看到能正常转发到62服务器上,并成功进行响应
再输入 http://www.nginx.com/helloworld
有意思的是,我们打开百度以后,地址栏地址并没有跳转转到www.baidu.com
但是当我们进行搜索的时候,发现报404了 ,不能正常进行搜索
需要再添加一个搜索的location
vim /opt/nginx/conf/nginx.conf
在server www.nginx.com 里添加location
location ~*/s.* {
proxy_pass https://www.baidu.com;
}
然后再次重新加载配置文件
service nginx reload
再重新通过http://www.nginx.com/helloworld 进行搜索,
能正常搜索,并且地址栏没有跳转到百度