nginx正向代理-内网服务器通过代理服务器访问外部网络

# 1.安装编译工具

yum install gcc gcc-c++  make -y
yum install rpm-build rpmdevtools -y

2.安装依赖

yum install pcre-devel pcre -y
yum install zlib-devel zlib -y
yum install openssl-devel openssl -y
yum install redhat-lsb-core -y
yum install git -y

3.进入/usr/local目录下载nginx和ngx_http_proxy_connect_module

cd /usr/local

从git拉取模块

git clone https://github.com/chobits/ngx_http_proxy_connect_module.git

下载nginx包

wget http://nginx.org/download/nginx-1.9.9.tar.gz

解压nginx压缩包

tar -zxvf nginx-1.9.9.tar.gz

4.进入nginx解压目录添加https模块

cd  /usr/local/nginx-1.9.9

添加模块到nginx

patch  -p1 < /usr/local/ngx_http_proxy_connect_module/patch/proxy_connect.patch
./configure --add-module=/usr/local/ngx_http_proxy_connect_module

5.安装nginx

make &&  make install

6.配置nginx.conf

vim /usr/local/nginx/conf/nginx.conf
worker_processes 1;
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid;events {worker_connections 1024;
}http {include mime.types;default_type application/octet-stream;#log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '#                  '$status $body_bytes_sent "$http_referer" '#                  '"$http_user_agent" "$http_x_forwarded_for"';#access_log  logs/access.log  main;sendfile on;#tcp_nopush     on;#keepalive_timeout  0;keepalive_timeout 65;#gzip  on;server {#代理后端口listen 9999;charset utf-8;# dns resolver used by forward proxyingresolver 114.114.114.114;# forward proxy for CONNECT requestproxy_connect;#设置为all,允许转发所有的端口proxy_connect_allow all; proxy_connect_connect_timeout 10s;proxy_connect_read_timeout 10s;proxy_connect_send_timeout 10s;# forward proxy for non-CONNECT requestlocation / {#试过使用 $scheme://$host$request_uri;好像不起作用if ($scheme = 'http') {proxy_pass http://$host$request_uri;}if ($scheme = 'https') {proxy_pass https://$host$request_uri;}proxy_set_header Host $host;proxy_buffers 256 4k;proxy_max_temp_file_size 0k;}}
}

7.启动nginx

cd /usr/local/nginx/sbin./nginx 

8.修改nginx.conf文件后,重新加载配置

cd /usr/local/nginx/sbin./nginx -s reload

9.去要访问外网的内网服务器环境变量里面配置正向代理服务器的代理地址

vim  /etc/profile
#export http_proxy=正向代理服务器http的IP:端口
export http_proxy=192.168.3.17:9999#export https_proxy=正向代理服务器https的IP:端口
export https_proxy=192.168.3.17:9999
#保存文件后重新加载环境
source /etc/profile

10.内网服务器测试访问

curl  -i  www.baidu.com#如果未加环境变量代理设置,则可以通过临时代理访问curl -i  --proxy 192.168.3.17:9999      www.baidu.com

11.java通过代理访问外部接口

package test.util;import java.io.*;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.net.Proxy.Type;public class HttpAndHttpsProxy {/*** url:外网测试地址 param:请求报文 proxy:代理地址(内网IP地址:10.0.100.00) port :端口号(22)* **/public static String HttpProxy(String url, String param, String proxy, int port) {HttpURLConnection httpConn = null;OutputStreamWriter out1 = null;BufferedReader in = null;String result = "";BufferedReader reader = null;try {URL urlClient = new URL(url);System.out.println("请求的URL========:" + urlClient);// 创建代理Proxy proxy1 = new Proxy(Type.HTTP, new InetSocketAddress(proxy, port));// 设置代理httpConn = (HttpURLConnection) urlClient.openConnection(proxy1);// 设置通用的请求属性httpConn.setRequestProperty("accept", "*/*");httpConn.setRequestProperty("connection", "Keep-Alive");httpConn.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");httpConn.setRequestProperty("Charset", "utf-8");// 发送POST请求必须设置如下两行httpConn.setDoOutput(true);httpConn.setDoInput(true);// 获取URLConnection对象对应的输出流//使请求报文不中文乱码out1 = new OutputStreamWriter(httpConn.getOutputStream());out1.write(param);out1.flush();//使返回的报文不中文乱码in = new BufferedReader(new InputStreamReader(httpConn.getInputStream(),"gbk"));String line;while ((line = in.readLine()) != null) {result += line;}httpConn.disconnect();System.out.println("====result====" + result);System.out.println("返回结果:" + httpConn.getResponseMessage());} catch (Exception e) {e.printStackTrace();} finally {try { if (reader != null) {  reader.close(); } } catch (IOException e) { e.printStackTrace(); }try { if (in != null) { in.close(); } } catch (IOException e) { e.printStackTrace(); }try { if (out1 != null) { out1.close(); } } catch (IOException e) { e.printStackTrace(); }}return result;}private static void getResponseTextV2(InputStream netIps, StringBuilder builder) throws Exception {byte[] buffer = new byte[1024];int len;while((len = netIps.read(buffer)) != -1) {builder.append(new String(buffer, 0, len, "UTF-8"));}}public static void main(String[] args) {// 示例String aa=HttpProxy("http://whois.pconline.com.cn/ipJson.jsp?ip=183.129.203.232&json=true", "", "192.168.3.17", 9999);System.out.println(aa);}
}


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部