Nginx正向代理請求https報400

如題,Nginx配置正向代理,請求https時報400,請求http時正常。
Nginx正向代理配置如下:

#正向代理
server {
	resolver 8.8.8.8;#指定dns服務器
	listen 30000;
	
	access_log D:/mysoft/nginx-1.17.3/logs/proxy_access.log;
	error_log D:/mysoft/nginx-1.17.3/logs/proxy_error.log;
	
	location / {
		proxy_pass $scheme://$http_host$request_uri;
		proxy_set_header Host $http_host;
		proxy_set_header X-Real-IP $host;
		proxy_set_header X-Forwarded-For $host;
		
		proxy_buffering on;
		proxy_buffer_size 32k;
		proxy_busy_buffers_size 256k;
		proxy_buffers 256 4k;
		proxy_max_temp_file_size 0;
		proxy_connect_timeout 30;
		proxy_cache_valid 200 302 10m;
		proxy_cache_valid 301 1h;
		proxy_cache_valid any 1m;
	}
}

使用JDK原生 HttpURLConnection 發送http請求

public static String sendProxyPost(String url, String param, String method, boolean proxy) {
    OutputStreamWriter out = null;
    BufferedReader in = null;
    StringBuffer result = new StringBuffer();
    try {
        URL realUrl = new URL(url);
        HttpURLConnection conn = null;
        if (proxy) {
            Proxy proxy1 = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("172.203.144.6", 30000));
            conn = (HttpURLConnection) realUrl.openConnection(proxy1);
        } else {
            conn = (HttpURLConnection) realUrl.openConnection();
        }
        if(url.indexOf("https")==0){
            trustAllCertificates(conn);//信任所有證書
        }
        if (method.equalsIgnoreCase("POST")) {
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            conn.setRequestMethod("POST");// POST方法
            // 設置通用的請求屬性
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        } else if (method.equalsIgnoreCase("GET")) {
            conn.setDoOutput(false);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            conn.setRequestMethod("GET");
        }
        conn.connect();
        if (method.equalsIgnoreCase("POST")){
            out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
            out.write(param);
            out.flush();
        }
        in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line;
        while ((line = in.readLine()) != null) {
            result.append(line);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (null != out) {
            try {
                out.close();
            } catch (IOException e) {
                System.out.println("e.printStackTrace():{}" + e);
            }
        }
        if (null != in) {
            try {
                in.close();
            } catch (IOException e) {
                System.out.println("e.printStackTrace():{}" + e);
            }
        }
    }
    return result.toString();
}

java通過代理請求百度首頁

public static void main(String[] args) {
    String url;
    //url = "http://www.formysql.com/mysql/";
    url = "https://www.baidu.com";
    String s = ErolyTests.sendProxyPost(url,"", "GET",true);
    System.out.println(s);
    
}

運行結果
在這裏插入圖片描述
沒有得到解決

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章