利用Nginx架设Http代理服务器

来源:互联网 发布:苹果显示器知乎 编辑:程序博客网 时间:2024/05/22 07:57
转自:http://blog.csdn.net/longmarch12/article/details/6563820
nginx是一款强大的反向代理服务器软件,除了作为http服务器和反向代理之外,还可以架设http代理服务器。

配置很简单:

[cpp] view plaincopy
  1. resolver YOUR_RESOLV_IP;  
  2. server {   
  3. listen proxy_ip:proxy_port;   
  4.   
  5. location / {  
  6. proxy_pass http://$http_host$request_uri;  
  7. access_log logs/http_proxy.access.log;  
  8. error_log logs/http_proxy.error.log;  
  9. }  
  10.   
  11. }  



=========================================================

wget通过http代理访问:
 
在服务器上wget之前,设置一下环境变量:

export http_proxy=proxy_ip:proxy_port 


wget http://www.qq.com

--16:25:38-- http://www.qq.com/
=> `index.html'
Connecting to proxy_ip:proxy_port... connected.
Proxy request sent, awaiting response... 200 OK
Length: unspecified [text/html]

[ <=> ] 55,822 121.90K/s 

16:25:42 (121.74 KB/s) - `index.html' saved [55822]

大功告成。

===============================================
 
PHP中如何使用代理发送http请求呢?

一、 使用CURL扩展
 
[php] view plaincopy
  1. $curl = curl_init($url); curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);curl_setopt($ch, CURLOPT_PROXY, 'proxy_ip:proxy_port');$res = curl_exec($curl);  
 
二、 使用pecl的http扩展 

官网地址:http://pecl.php.net/package/pecl_http
使用方式:
 
[php] view plaincopy
  1. $http_obj = new HttpRequest($url, HttpRequest::METH_GET);  $options = array(   'useragent' => 'Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1',   'connecttimeout' => 1,    'timeout' => 3,   'proxyhost' => 'proxy_ip:proxy_port ',); $http_obj->setOptions($options); $message_obj = $this->mHTTP->send();$res =   $message_obj->getBody();     
 
0 0
原创粉丝点击