Re: 回复: getServerPort always return 80

2022-01-06 Thread Hua Zhang
Hi Wang,

I hope that this link will help you to solve the problem:
https://stackoverflow.com/questions/19751313/forward-request-headers-from-nginx-proxy-server

You may check the tomcat access log to see what kind of traffic it received
from nginx to confirm that your issue is related to nginx (probably some
configuration change will help you to solve the problem)

Anyway, good that you did a test to verify that this issue is not related
to Tomcat. If the link does not help you to solve the problem,
you should put your question better in a nginx forum than in a tomcat forum.



Good luck.

Best regards,

Hua


On Thu, Jan 6, 2022 at 10:45 AM 王 静凯  wrote:

> Hi Hua,
>
>You are right, when tomcat listening port 80, then getServerPort
> return 10001.
>So how to resolve this problem when I use nginx as a reverse-proxy?
>I write in nginx config file:
>   “proxy_set_header   Host$http_host;”  or  “proxy_set_header
>  Host$host:10001;”
>Then I write in jsp:
>   var ProxyPort = "<%= request.getServerPort() %>"
>   var header1 = "<%= request.getHeader("host") %>"
>   When I visit the jsp it print:
>
> var ProxyPort = "80"
>
> var header1 = "xx.xxx.xx.xxx:10001"
>
>
>
>   The definition of getServerPort() is to return the value after ‘:’
> in host header,
>
>so maybe some code in getServerPort method could not process the
> host header correctly and getHeader method can get the correct header value
> at the same time?
>
>
> >Hi Wang, A second application (nginx) is involved here.
> >
> >The problem can be related to nginx, nginx received a http traffic on
> 10001
> >in access log does not mean that if forwards exactly the same original
> http
> >request. Probably some configuration needs to be changed there. You should
> >take a look in the nginx documentation about port forwarding.
> >
> >You may also skip using nginx and mapping port 10001 directly to tomcat
> >port 8080. In your browser typing your http://domain:10001, to check the
> >getServerPort() value.
> >
> >This will help you to investigate if it is a Tomcat issue or not.
> >
> >
> >Best regards,
> >
> >Hua
>
>
> On Thu, Jan 6, 2022 at 6:39 AM 王 静凯  wrote:
>
> > Sorry for my poor English.
> > I re-explain the problem:
> >
> > I mean the nginx listening on port 80 and tomcat listening on 8080.
> > So the visitors in the LAN can use 80 to visit my website.
> > Then the port 80 make a port mapping to port 10001.
> > So the visitors use 10001 to visit my website via internet.
> > At this time the browser send the Host header with value ‘domain:10001’.
> > The nginx print the correct value ‘domain:10001’ in nginx access log.
> > The jsp print the correct value ‘domain:10001’ when I use
> > httpServletRequest.getHeader(“Host”).
> > According to the definition of ‘getServerPort()’, it should return 10001
> > but now it return 80.
> > What’s more, the port 80 not relate to nginx, just bcz I am using the
> http
> > protocol.
> >
> > In another server I found one more situation.
> > The server has an internet IP and Tomcat listening 8080 and nginx
> > listening 81.
> > So there are no port mapping.
> > The visitors use ‘http://domain:81’ to visit the website.
> > At this time getServerPort() still return 80.
> >
> > -
> > To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
> > For additional commands, e-mail: users-h...@tomcat.apache.org
> >
> >
>
>


Re: 回复: getServerPort always return 80

2022-01-06 Thread Hua Zhang
Hi Wang, A second application (nginx) is involved here.

The problem can be related to nginx, nginx received a http traffic on 10001
in access log does not mean that if forwards exactly the same original http
request. Probably some configuration needs to be changed there. You should
take a look in the nginx documentation about port forwarding.

You may also skip using nginx and mapping port 10001 directly to tomcat
port 8080. In your browser typing your http://domain:10001, to check the
getServerPort() value.

This will help you to investigate if it is a Tomcat issue or not.


Best regards,

Hua


On Thu, Jan 6, 2022 at 6:39 AM 王 静凯  wrote:

> Sorry for my poor English.
> I re-explain the problem:
>
> I mean the nginx listening on port 80 and tomcat listening on 8080.
> So the visitors in the LAN can use 80 to visit my website.
> Then the port 80 make a port mapping to port 10001.
> So the visitors use 10001 to visit my website via internet.
> At this time the browser send the Host header with value ‘domain:10001’.
> The nginx print the correct value ‘domain:10001’ in nginx access log.
> The jsp print the correct value ‘domain:10001’ when I use
> httpServletRequest.getHeader(“Host”).
> According to the definition of ‘getServerPort()’, it should return 10001
> but now it return 80.
> What’s more, the port 80 not relate to nginx, just bcz I am using the http
> protocol.
>
> In another server I found one more situation.
> The server has an internet IP and Tomcat listening 8080 and nginx
> listening 81.
> So there are no port mapping.
> The visitors use ‘http://domain:81’ to visit the website.
> At this time getServerPort() still return 80.
>
> -
> To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
> For additional commands, e-mail: users-h...@tomcat.apache.org
>
>


Re: 回复: getServerPort always return 80

2022-01-05 Thread Christopher Schultz

王 静凯,

On 1/5/22 01:55, 王 静凯 wrote:

Hi chris,


I use httpServletRequest.getServerPort() to get the port in my jsp.
When the tomcat listen 8080 and nginx listen 80.
Nginx has set ‘proxy_set_header Host $http_host’.



What is the value of $http_host?


  The $http_host is ‘externalIP:10001’ (I print it in access_log of nginx 
to make sure that it is a correct valve.)
  I use httpServletRequest.getHeader(“Host”) in java and it can print the 
right value too. But as I know the getServerPort should print the number after 
‘:’ of ‘Host’ Header.
  So I don’t know what’s the wrong.

  And today I found another similar situation.
  The server has a Internet IP and Tomcat listening 8080 and nginx 
listening 81. So there are no port mapping.
  At this time getServerPort() still return 80.


Then the port  80 mapping to Internet via 10001.
Use httpServletRequest.getHeader(“Host”) I can get the correct value of Host 
header – “InternetIP:10001”.
But  httpServletRequest.getServerPort() did not return 10001 and return 80.
How could I solve it?
Btw, to set proxyPort in connector can not take effect. But proxyName work.



Have you read about the RemoteIPValve?

The RemoteIPValve seems to be used for get the real client IP, but I just 
need to get the correct port which client use to visit my website.

Forgive my poor English and I hope you can understand what I say. (^-^)


Something does not make sense:

1. You have nginx listening on port 80, proxying to port 11

2. You have a Tomcat  on port 8080:
   redirectPort="8443" maxHttpHeaderSize="8192" 
maxThreads="4000" minSpareThreads="1000" maxSpareThreads="2000" 
enableLookups="false" acceptCount="2000" URIEncoding="UTF-8" 
maxPostSize="4194304" />


If Tomcat is listening on port 8080, how is nginx reaching it on port 10001?

-chris

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