1、新建配置文件
vim /usr/local/nginx/conf/vhosts/proxy.conf
2、添加以下内容
server{
listen 80;
server_name www.baidu.com;
location / {
proxy_pass http://61.135.169.121/;
# proxy_set_header Host $host
}
}
3、重新加载
/usr/local/nginx/sbin/nginx -s reload
4、使用curl测试
curl -x127.0.0.1:80 www.baidu.com
查看返回结果以</html>结尾测试成功
5、配置解析一个域名对应多个ip地址的代理(相当负载均)
将配置修改成如下
upstream river{
server 61.135.169.125:80;
server 61.135.169.121:80;
}
server{
listen 80;
server_name www.baidu.com;
location / {
proxy_pass http://river/;
proxy_set_header Host $host;
}
}
6、再次使用curl测试查看返回结果</html>结尾测试成功
7、需求:当请求的目录是 /aaa/ 则把请求发送到机器a,当请求的目录为/bbb/则把请求发送到机器b,除了目录/aaa/与目录/bbb/外,其他的请求发送到机器b
upstream aaa.com
{
server 192.168.111.6;
}
upstream bbb.com
{
server 192.168.111.20;
}
server {
listen 80;
server_name li.com;
location /aaa/
{
proxy_pass http://aaa.com/aaa/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /bbb/
{
proxy_pass http://bbb.com/bbb/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /
{
proxy_pass http://bbb.com/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
8、场景:1台nginx去代理4台apache
需求:根据不同的请求uri 代理到不同的apache
upstream aa.com {
server 192.168.0.121;
server 192.168.0.122;
}
upstream bb.com {
server 192.168.0.123;
server 192.168.0.124;
}
server {
listen 80;
server_name www.abc.com;
location ~ aa.php
{
proxy_pass http://aa.com/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~ bb.php
{
proxy_pass http://bb.com/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Nginx的Web缓存服务解释 http://blog.sina.com.cn/s/blog_664553710100zmxr.html