Re: ServletRequest.getRemoteHost() not working when Tomcat is behind Nginx (Nginx as a reverse proxy)

2015-09-09 Thread Christopher Schultz
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA256

Brian,

On 9/8/15 5:16 PM, Brian wrote:
> mm.. ... Well, so far I have always assumed that Tomcat
> itself has always made this effort (assuming that it is enabled to
> do so in the connector), so that when I execute the method I'm just
> retrieving the value. I'm I wrong?
> 
> In this case when using Nginx+Tomcat, I assume that Nginx already 
> made the effort to get the remoteHost value as well, so Tomcat
> just receives it and I just need to invoke the method to get it.
> Maybe I'm wrong here.

You have to enable the RemoteIPValve, which was specifically written
for this purpose:

http://tomcat.apache.org/tomcat-8.0-doc/config/valve.html#Proxies_Suppor
t

- -chris
-BEGIN PGP SIGNATURE-
Comment: GPGTools - http://gpgtools.org

iQIcBAEBCAAGBQJV8FU6AAoJEBzwKT+lPKRYhC0QALJWX08e7xiC85YFsUgm5Kn9
YLGtsJCwlRJLZKsJuI/5/a6d0LiQdCfhmNmZO+uk9uyzo2MSJchVcPOJzJ1EsDVx
Yu0U3TRV3FFkecJyEaIjkTJCAufR2tcV0KdUK1tCJ7KfLrLebwneVaIzfw1T/DM4
xW8fkAvCgueEyTteEVc9fUPc7ck/5+jEXV8DrFXE51gISJea79BWfTd5V/FTjK/D
kgteoRbPoLxlRnhUTaeAyT7qxbEiNX5hjZ498NsBOgZ4/qk+EkdVFXK9QdJ0drsP
FhvahJE19VtVuxFnI9EOfg/yiH+ZwE1xygfgZfYQ9SyyaUgIAWYXzLOUrlHUdVES
jprwk3UFcxDrv+fAkhFefHQDk2LdgPDm7/ZeYvU+cKbd17gPHkLfcl9cX75v5pk5
yDi+wnSjZY1sMHlglbwhJGfCtttRTkbV+isTKkL5DeU2+IJPZU2/K3/1d0iVygXv
bPf58cWjdas/1C+9IcWl8zRZjMv3iunzN92McdPwVkt3OR1izFiGAaB/qByzHGLA
IvkqEdcSdpgED34jDHRWtNkgnW17p628IbUL7PBsG/e+BPKSsRfM4cxkBZw8CUPj
jLdDEH8ENYcEBtids+baHdkWsdsRld5h8VwifaA935RB2NnGb2vyk8S1YqQa3gh5
g3QRGpgjesJBn+H7+DHd
=2AhX
-END PGP SIGNATURE-

-
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org



Re: ServletRequest.getRemoteHost() not working when Tomcat is behind Nginx (Nginx as a reverse proxy)

2015-09-08 Thread Jose María Zaragoza
2015-09-08 21:22 GMT+02:00 Brian :
> Hi,
>
>
>
> First of all, I'm using:
>
> - Tomcat 7.0.50
>
> - Nginx 1.4.7
>
>
>
> When I use Tomcat alone, ServletRequest.getRemoteHost()
> (http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getRe
> moteHost()
>  moteHost())>  )  works fine. But when Tomcat is behind Nginx (Nginx acting
> as a reverse proxy), it does not.
>
> Just to make myself clear, this is the architecture I'm talking about:
>
>
>
> Client -> Nginx (as a reverse proxy) -> Tomcat.
>
>
>
> The problem is that ServletRequest.getRemoteHost() gives me the hostname of
> the proxy itself (meaning Nginx) and not that of the client.
>
>
>
> I was able to get the IP address of the visitor (and not that of the host
> where Nginx is running) doing this on Nginx:
>
>
>
> server {
>
> listen 80;
>
> server_name www.acme.com acme.com;
>
> location / {
>
> proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
> <--- This line did the trick
>
> proxy_set_header Host $http_host;
>
> proxy_pass http://152.53.163.220:80/;
>
> }
>
> }
>
>
>
> And then inspecting the content of the "X-Forwarded-For" header in my java
> programming. But what do I do to obtain the remote hostname? I guess it is
> something similar, but I haven't found a solution. What I want to know is:
>
> - Exactly what configuration do I need in Nginx
>
> - Exactly what do I do from Java to obtain the value.

Why not do you perform a reverse DNS lookup by code ? Something like :

InetAddress addr = InetAddress.getByName("xx.xx.xx.xx");
String host = addr.getCanonicalHostName();
System.out.println(host);

You only need to extract 'X-Forwarded-For' header from request  and
execute that piece of code


Regards

>
>
>
> Thanks in advance,
>
>
>
> Brian
>

-
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org



RE: ServletRequest.getRemoteHost() not working when Tomcat is behind Nginx (Nginx as a reverse proxy)

2015-09-08 Thread Brian
Hello José,

That’s a nice idea indeed (A VERY NICE ONE!), but an extra work because of the 
networking effort. I'm talking about a site that can get hundreds of requests 
per second.

Since Nginx has access to this information, I bet there must be a way to pass 
it to Tomcat the same way the IP address can be passed! But for some reason I 
can't find it and I have spent quite some time looking for it.

Thanks a lot!


> -Original Message-
> From: Jose María Zaragoza [mailto:demablo...@gmail.com]
> Sent: martes, 08 de septiembre de 2015 02:58 p.m.
> To: Tomcat Users List 
> Subject: Re: ServletRequest.getRemoteHost() not working when Tomcat is
> behind Nginx (Nginx as a reverse proxy)
> 
> 2015-09-08 21:22 GMT+02:00 Brian :
> > Hi,
> >
> >
> >
> > First of all, I'm using:
> >
> > - Tomcat 7.0.50
> >
> > - Nginx 1.4.7
> >
> >
> >
> > When I use Tomcat alone, ServletRequest.getRemoteHost()
> >
> (http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getRe
> > moteHost()
> >
>  > moteHost())>  )  works fine. But when Tomcat is behind Nginx (Nginx acting
> > as a reverse proxy), it does not.
> >
> > Just to make myself clear, this is the architecture I'm talking about:
> >
> >
> >
> > Client -> Nginx (as a reverse proxy) -> Tomcat.
> >
> >
> >
> > The problem is that ServletRequest.getRemoteHost() gives me the hostname of
> > the proxy itself (meaning Nginx) and not that of the client.
> >
> >
> >
> > I was able to get the IP address of the visitor (and not that of the host
> > where Nginx is running) doing this on Nginx:
> >
> >
> >
> > server {
> >
> > listen 80;
> >
> > server_name www.acme.com acme.com;
> >
> > location / {
> >
> > proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
> > <--- This line did the trick
> >
> > proxy_set_header Host $http_host;
> >
> > proxy_pass http://152.53.163.220:80/;
> >
> > }
> >
> > }
> >
> >
> >
> > And then inspecting the content of the "X-Forwarded-For" header in my java
> > programming. But what do I do to obtain the remote hostname? I guess it is
> > something similar, but I haven't found a solution. What I want to know is:
> >
> > - Exactly what configuration do I need in Nginx
> >
> > - Exactly what do I do from Java to obtain the value.
> 
> Why not do you perform a reverse DNS lookup by code ? Something like :
> 
> InetAddress addr = InetAddress.getByName("xx.xx.xx.xx");
> String host = addr.getCanonicalHostName();
> System.out.println(host);
> 
> You only need to extract 'X-Forwarded-For' header from request  and
> execute that piece of code
> 
> 
> Regards
> 
> >
> >
> >
> > Thanks in advance,
> >
> >
> >
> > Brian
> >
> 
> -
> To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
> For additional commands, e-mail: users-h...@tomcat.apache.org


-
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org



Re: ServletRequest.getRemoteHost() not working when Tomcat is behind Nginx (Nginx as a reverse proxy)

2015-09-08 Thread Jose María Zaragoza
2015-09-08 22:10 GMT+02:00 Brian :
> Hello José,
>
> That’s a nice idea indeed (A VERY NICE ONE!), but an extra work because of 
> the networking effort. I'm talking about a site that can get hundreds of 
> requests per second.

But you would want to execute ServletRequest.getRemoteHost() in every
request , right ? That was your question.
I don't know how is the Tomcat 6's ServletRequest.getRemoteHost()
implementation , but I guess it's not very different to my code

Regards




>
> Since Nginx has access to this information, I bet there must be a way to pass 
> it to Tomcat the same way the IP address can be passed! But for some reason I 
> can't find it and I have spent quite some time looking for it.
>
> Thanks a lot!
>
>
>> -Original Message-
>> From: Jose María Zaragoza [mailto:demablo...@gmail.com]
>> Sent: martes, 08 de septiembre de 2015 02:58 p.m.
>> To: Tomcat Users List 
>> Subject: Re: ServletRequest.getRemoteHost() not working when Tomcat is
>> behind Nginx (Nginx as a reverse proxy)
>>
>> 2015-09-08 21:22 GMT+02:00 Brian :
>> > Hi,
>> >
>> >
>> >
>> > First of all, I'm using:
>> >
>> > - Tomcat 7.0.50
>> >
>> > - Nginx 1.4.7
>> >
>> >
>> >
>> > When I use Tomcat alone, ServletRequest.getRemoteHost()
>> >
>> (http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getRe
>> > moteHost()
>> >
>> > > moteHost())>  )  works fine. But when Tomcat is behind Nginx (Nginx acting
>> > as a reverse proxy), it does not.
>> >
>> > Just to make myself clear, this is the architecture I'm talking about:
>> >
>> >
>> >
>> > Client -> Nginx (as a reverse proxy) -> Tomcat.
>> >
>> >
>> >
>> > The problem is that ServletRequest.getRemoteHost() gives me the hostname of
>> > the proxy itself (meaning Nginx) and not that of the client.
>> >
>> >
>> >
>> > I was able to get the IP address of the visitor (and not that of the host
>> > where Nginx is running) doing this on Nginx:
>> >
>> >
>> >
>> > server {
>> >
>> > listen 80;
>> >
>> > server_name www.acme.com acme.com;
>> >
>> > location / {
>> >
>> > proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
>> > <--- This line did the trick
>> >
>> > proxy_set_header Host $http_host;
>> >
>> > proxy_pass http://152.53.163.220:80/;
>> >
>> > }
>> >
>> > }
>> >
>> >
>> >
>> > And then inspecting the content of the "X-Forwarded-For" header in my java
>> > programming. But what do I do to obtain the remote hostname? I guess it is
>> > something similar, but I haven't found a solution. What I want to know is:
>> >
>> > - Exactly what configuration do I need in Nginx
>> >
>> > - Exactly what do I do from Java to obtain the value.
>>
>> Why not do you perform a reverse DNS lookup by code ? Something like :
>>
>> InetAddress addr = InetAddress.getByName("xx.xx.xx.xx");
>> String host = addr.getCanonicalHostName();
>> System.out.println(host);
>>
>> You only need to extract 'X-Forwarded-For' header from request  and
>> execute that piece of code
>>
>>
>> Regards
>>
>> >
>> >
>> >
>> > Thanks in advance,
>> >
>> >
>> >
>> > Brian
>> >
>>
>> -
>> To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
>> For additional commands, e-mail: users-h...@tomcat.apache.org
>
>
> -
> To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
> For additional commands, e-mail: users-h...@tomcat.apache.org
>

-
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org



RE: ServletRequest.getRemoteHost() not working when Tomcat is behind Nginx (Nginx as a reverse proxy)

2015-09-08 Thread Brian
mm..
... Well, so far I have always assumed that Tomcat itself has always made this 
effort (assuming that it is enabled to do so in the connector), so that when I 
execute the method I'm just retrieving the value. I'm I wrong?

In this case when using Nginx+Tomcat, I assume that Nginx already made the 
effort to get the remoteHost value as well, so Tomcat just receives it and I 
just need to invoke the method to get it. Maybe I'm wrong here.

I really appreciate your help!


> -Original Message-
> From: Jose María Zaragoza [mailto:demablo...@gmail.com]
> Sent: martes, 08 de septiembre de 2015 03:59 p.m.
> To: Tomcat Users List 
> Subject: Re: ServletRequest.getRemoteHost() not working when Tomcat is
> behind Nginx (Nginx as a reverse proxy)
> 
> 2015-09-08 22:10 GMT+02:00 Brian :
> > Hello Jos�,
> >
> > That�s a nice idea indeed (A VERY NICE ONE!), but an extra work because of
> the networking effort. I'm talking about a site that can get hundreds of 
> requests
> per second.
> 
> But you would want to execute ServletRequest.getRemoteHost() in every
> request , right ? That was your question.
> I don't know how is the Tomcat 6's ServletRequest.getRemoteHost()
> implementation , but I guess it's not very different to my code
> 
> Regards
> 
> 
> 
> 
> >
> > Since Nginx has access to this information, I bet there must be a way to 
> > pass it
> to Tomcat the same way the IP address can be passed! But for some reason I
> can't find it and I have spent quite some time looking for it.
> >
> > Thanks a lot!
> >
> >
> >> -Original Message-
> >> From: Jose Mar�a Zaragoza [mailto:demablo...@gmail.com]
> >> Sent: martes, 08 de septiembre de 2015 02:58 p.m.
> >> To: Tomcat Users List 
> >> Subject: Re: ServletRequest.getRemoteHost() not working when Tomcat is
> >> behind Nginx (Nginx as a reverse proxy)
> >>
> >> 2015-09-08 21:22 GMT+02:00 Brian :
> >> > Hi,
> >> >
> >> >
> >> >
> >> > First of all, I'm using:
> >> >
> >> > - Tomcat 7.0.50
> >> >
> >> > - Nginx 1.4.7
> >> >
> >> >
> >> >
> >> > When I use Tomcat alone, ServletRequest.getRemoteHost()
> >> >
> >>
> (http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getRe
> >> > moteHost()
> >> >
> >>
>  >> > moteHost())>  )  works fine. But when Tomcat is behind Nginx (Nginx 
> >> > acting
> >> > as a reverse proxy), it does not.
> >> >
> >> > Just to make myself clear, this is the architecture I'm talking about:
> >> >
> >> >
> >> >
> >> > Client -> Nginx (as a reverse proxy) -> Tomcat.
> >> >
> >> >
> >> >
> >> > The problem is that ServletRequest.getRemoteHost() gives me the
> hostname of
> >> > the proxy itself (meaning Nginx) and not that of the client.
> >> >
> >> >
> >> >
> >> > I was able to get the IP address of the visitor (and not that of the host
> >> > where Nginx is running) doing this on Nginx:
> >> >
> >> >
> >> >
> >> > server {
> >> >
> >> > listen 80;
> >> >
> >> > server_name www.acme.com acme.com;
> >> >
> >> > location / {
> >> >
> >> > proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
> >> > <--- This line did the trick
> >> >
> >> > proxy_set_header Host $http_host;
> >> >
> >> > proxy_pass http://152.53.163.220:80/;
> >> >
> >> > }
> >> >
> >> > }
> >> >
> >> >
> >> >
> >> > And then inspecting the content of the "X-Forwarded-For" header in my 
> >> > java
> >> > programming. But what do I do to obtain the remote hostname? I guess it 
> >> > is
> >> > something similar, but I haven't found a solution. What I want to know 
> >> > is:
> >> >
> >> > - Exactly what configuration do I need in Nginx
> >> >
> >> > - Exactly what do I do from Java to obtain the value.
> >>
> >> Why not do you perform a reverse DNS lookup by code ? Something like :
> >>
> >> InetAddress addr = InetAddress.getByName("xx.xx.xx.xx");
> >> String host = addr.getCanonicalHostName();
> >> System.out.println(host);
> >>
> >> You only need to extract 'X-Forwarded-For' header from request  and
> >> execute that piece of code
> >>
> >>
> >> Regards
> >>
> >> >
> >> >
> >> >
> >> > Thanks in advance,
> >> >
> >> >
> >> >
> >> > Brian
> >> >
> >>
> >> -
> >> To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
> >> For additional commands, e-mail: users-h...@tomcat.apache.org
> >
> >
> > -
> > To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
> > For additional commands, e-mail: users-h...@tomcat.apache.org
> >
> 
> -
> To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
> For additional commands, e-mail: users-h...@tomcat.apache.org