apache ProxyPass: how to preserve original IP address

前端 未结 5 730
执念已碎
执念已碎 2020-12-02 12:22

We are using ProxyPass to redirect all \"/r\" requests to jboss on port 18080 as follows:

ProxyPreserveHost on
ProxyPass /r http://localhost:18080/redirectSe         


        
相关标签:
5条回答
  • 2020-12-02 13:00

    You can get the original host from X-Forwarded-For header field.

    0 讨论(0)
  • 2020-12-02 13:04

    If you are using Apache reverse proxy for serving an app running on a localhost port you must add a location to your vhost.

    <Location />            
       ProxyPass http://localhost:1339/ retry=0
       ProxyPassReverse http://localhost:1339/
       ProxyPreserveHost On
       ProxyErrorOverride Off
    </Location>
    

    To get the IP address have following options

    console.log(">>>", req.ip);// this works fine for me returned a valid ip address 
    console.log(">>>", req.headers['x-forwarded-for'] );// returned a valid IP address 
    console.log(">>>", req.headers['X-Real-IP'] ); // did not work returned undefined 
    console.log(">>>", req.connection.remoteAddress );// returned the loopback IP address 
    

    So either use req.ip or req.headers['x-forwarded-for']

    0 讨论(0)
  • 2020-12-02 13:08

    The answer of JasonW is fine. But since apache httpd 2.4.6 there is a alternative: mod_remoteip

    All what you must do is:

    1. May be you must install the mod_remoteip package
    2. Enable the module:

      LoadModule remoteip_module modules/mod_remoteip.so
      
    3. Add the following to your apache httpd config. Note that you must add this line not into the configuration of the proxy server. You must add this to the configuration of the proxy target httpd server (the server behind the proxy):

      RemoteIPHeader X-Forwarded-For
      

    See at http://httpd.apache.org/docs/trunk/mod/mod_remoteip.html for more informations and more options.

    0 讨论(0)
  • 2020-12-02 13:08

    This has a more elegant explanation and more than one possible solutions. http://kasunh.wordpress.com/2011/10/11/preserving-remote-iphost-while-proxying/

    The post describes how to use one popular and one lesser known Apache modules to preserve host/ip while in a setup involving proxying.

    Use mod_rpaf module, install and enable it in the backend server and add following directives in the module’s configuration. RPAFenable On
    RPAFsethostname On
    RPAFproxy_ips 127.0.0.1

    (2017 edit) Current location of mod_rpaf: https://github.com/gnif/mod_rpaf

    0 讨论(0)
  • 2020-12-02 13:09

    If you have the capability to do so, I would recommend using either mod-jk or mod-proxy-ajp to pass requests from Apache to JBoss. The AJP protocol is much more efficient compared to using HTTP proxy requests and as a benefit, JBoss will see the request as coming from the original client and not Apache.

    0 讨论(0)
提交回复
热议问题