Nginx配置反向代理

反向代理实现效果有两种:
1、打开浏览器,在浏览器地址栏输入地址http://www.01h.net/,服务器请求到http://01h.net.idcadmin.com/地址。
2、打开浏览器,在浏览器地址栏输入地址http://www.01h.net/blog/,服务器请求到http://ken.01h.net/地址,即根据访问的路径(二级目录)请求相应的地址。

第一种情况配置示例:

server {
        listen       80;
        root /www/web/01h_net/public_html;
        server_name www.01h.net;
        index  index.html index.php index.htm;
        location / {
                proxy_pass http://01h.net.idcadmin.com;
        }
}

第二种情况配置示例:

server {
         listen       80;
         root /www/web/01h_net/public_html;
         server_name www.01h.net;
         index  index.html index.php index.htm;
         location /blog/ {
                 proxy_pass http://ken.01h.net/;
         }
 }

或者,还有一种写法:

server {
         listen       80;
         root /www/web/01h_net/public_html;
         server_name www.01h.net;
         index  index.html index.php index.htm;
         location ~* /blog {
                 rewrite /blog/(.*) /$1 break;
                 proxy_pass http://ken.01h.net;
         }
 }

location指令说明:
1、= :用于不含正则表达式的 uri 前,要求请求字符串与 uri 严格匹配,如果匹配成功,就停止继续向下搜索并立即处理该请求。
2、~ :用于表示 uri 包含正则表达式,并且区分大小写。
3、~* :用于表示 uri 包含正则表达式,并且不区分大小写。
4、^~ :用于不含正则表达式的 uri 前,要求 Nginx 服务器找到标识 uri 和请求字符串匹配度最高的 location 后,立即使用此 location 处理请求,而不再使用 location 块中的正则 uri 和请求字符串做匹配。
注意:如果 uri 包含正则表达式,则必须要有 ~ 或者 ~* 标识。