Nginx转发代理配置

  作者:zhanhailiang 日期:2013-01-29

需求:

由于系统集成需求,需要将www.hwtrip.com集成到itravel.smartcom.cc中。

想法:

浏览器访问itravel.smartcom.cc/trip/****,通过Nginx将请求转发到www.hwtrip.com/$1?$query_string&from=itravel。

做法:

添加Nginx配置:匹配location中/trip/关键字,将该请求转发到www.hwtrip.com/$1?from=itravel
# 注意这里是匹配请求URI的路径是否含关键字/trip/,所以必须加上~, 若不加~,则相当于查找根目录下是否含有trip目录,详见【location】【一个简单PHP站点配置】【location
location ~ /trip/ {
    proxy_pass                             http://www.hwtrip.com:80;
    proxy_redirect                         default;
    proxy_set_header    Host               www.hwtrip.com;
    proxy_set_header    X-Real-IP          $remote_addr;
    proxy_set_header    X-Forwarded-For    $proxy_add_x_forwarded_for;
    # 转发前的请求参数会默认添加到新参数后面 ,详见【rewrite
    rewrite             ^/trip/(.*)$       /$1?from=itravel  break; 
    break;
}

最后测试用例如下:

==> 请求头 <==
GET /trip/t.html HTTP/1.1
Host: itravel.smartcom.cc
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.56 Safari/537.17
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Cookie: ******

==> 响应头 <==
HTTP/1.1 200 OK
Server: nginx
Date: Tue, 29 Jan 2013 07:38:11 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: close
Content-Encoding: gzip
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值