Nginx反向代理添加代码示例及常见问题
nginx反代有很多好处,例如,隐藏真实服务器、实习负载均衡等。我是被迫使用反代。网站备案在A,A带宽不错,但硬盘很渣(不错,就是阿里),又有主机L,整体不错,线路还行(不错,就是Linode),为什么没有直接解析到B?L到部分地区的网络偶尔会抽一下。而A到L直接的网络一直很顺畅!于是,就开始了不归之路...
1、简单的反代
server {
listen 80;
server_name www.gaaaa.com;
access_log off;
location / {
proxy_pass http://node1.gaaaa.com/;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
2、入门的反代(支持替换)
server {
listen 80;
server_name www.gaaaa.com;
access_log off;
location / {
proxy_pass http://node1.gaaaa.com/;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
subs_filter_types text/html text/css text/xml;
subs_filter node1.gaaaa.com www.gaaaa.com;
}
}
备注:
subs_filter_types text/html text/css text/xml;
如果需要反代的文件拓展名为.php,则需要添加为:
subs_filter_types text/html text/css text/xml text/php;