【问题标题】:how to pass url in request through upstream proxy using Nginx如何使用 Nginx 通过上游代理在请求中传递 url
【发布时间】:2022-07-13 16:59:17
【问题描述】:

如何通过授权的上游代理将我们的客户端请求传递到特定的网址?

我必须输入用户名/密码来连接上游代理,然后在我的浏览器中以 HTTP 和 HTTPS 协议(我可以在 Firefox 手动代理选项中设置)打开一些网页(例如 jj.com)。 所以我想为其他客户端打开 jj.com 提供访问权限,而无需知道并输入用户名/密码并通过 NginX 在她/他的浏览器中打开网页(如 jj.com)。

 +--------------------+                      +---------------------+
 |                    |                      | upstramProxy:9090   |       +----------+
 | Client browser     |                      | user:user-003       |+----->| jj.com   |
 |                    |                      | pass:123456         |<------+----------+
 +--------------------+                      +---------------------+
     |  ^ (myProxyService.com:8080/pr?url=jj.com)   |  ^
     |  |                                           |  |
     |  |                                           |  |
     |  |                                +---------------------+
     |  +--------------------------------| myProxyService.com  |
     |                                   | NginX in myServer   |
     +---------------------------------->| Listen to 8080      |
                                         +---------------------+
                          

有可能吗? 如果还有其他方法我想知道。

【问题讨论】:

    标签: nginx proxy


    【解决方案1】:

    witch java 中的简单 java web 应用程序通过名为 myProxyServer 的第三方代理发送所有请求

     public static void processRequest(HttpServletRequest req, HttpServletResponse resp) {
        try {
            PrintWriter out = resp.getWriter();
            System.out.println(req.getRequestURI());
            System.out.println(req.getRequestURL());
            jjTools.ShowAllParameter(req);
            final BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
            credsProvider.setCredentials(
                    new AuthScope("thirdpartyProxy.com", 8080),
                    new UsernamePasswordCredentials("username", "passss".toCharArray())
            );
            try (final CloseableHttpClient httpclient = HttpClients.custom()
                    .setDefaultCredentialsProvider(credsProvider).build()) {
                
                final HttpHost target = new HttpHost("https", "www.targetwebSite.com", 443);
                final HttpHost proxy = new HttpHost("http", "thirdpartyProxy.com", 8080);
                final RequestConfig config = RequestConfig.custom()
                        .setProxy(proxy)
                        .setCircularRedirectsAllowed(true)
                        .build();                                        
                final HttpGet httpget = new HttpGet(req.getRequestURI().replaceFirst("/My_Proxy/", "/"));                
                httpget.setConfig(config);                
    
                System.out.println("Executing request " + httpget.getMethod() + " " + httpget.getUri()
                        + " via " + proxy);
    
                try (final CloseableHttpResponse response = httpclient.execute(target, httpget)) {
                    System.out.println("----------------------------------------");
                    resp.setContentType(response.getHeader("Content-Type").toString());
                    System.out.println(response.getHeader("Content-Type").toString());
                    System.out.println(response.getCode() + " " + response.getReasonPhrase());
                    out.print(EntityUtils.toString(response.getEntity())+"<scipt>alert");
                }
            }
            out.close();
        } catch (Exception ex) {
            Logger.getLogger(Listener.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    

    然后你必须像下面的代码那样配置 nginX.conf:

    server {
        listen          80;
        server_name         targetwebSite.localhost;
        location / {
            #proxy_set_header X-Forwarded-Host $host;
            #proxy_set_header X-Forwarded-Server $host;
            #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://localhost:8080/My_Proxy/;
        }
    

    }

    对于结果,它将适用于所有包含其 CSS 和 JavaScript 文件的页面

    http://targetwebSite.localhost/

    地址

    注意:cookie 存在一些问题,并在浏览器中接受 cookie 策略

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-29
      • 1970-01-01
      • 2018-01-07
      • 1970-01-01
      • 2013-07-16
      • 1970-01-01
      • 2014-09-18
      • 2018-01-31
      相关资源
      最近更新 更多