You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by 王 静凯 <ly...@outlook.com> on 2022/01/05 06:55:55 UTC
回复: getServerPort always return 80
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 �C ��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. (�ޣ���)
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
AW: getServerPort always return 80
Posted by "Thomas Hoffmann (Speed4Trade GmbH)" <Th...@speed4trade.com.INVALID>.
Hello,
Maybe you can use isSecure() instead of getScheme()
Issecure can be modified via server.xml
Greetings, Thomas
________________________________
Von: 王 静凯 <ly...@outlook.com>
Gesendet: Montag, 24. Januar 2022 10:35:52
An: Tomcat Users List
Betreff: 回复: getServerPort always return 80
Hi,
I have found what cause this problem after compile a tomcat with some logs.
In RemoteIpValve.java’s method ‘invoke’, it check if protocolHeader is null. My nginx send a header ‘X-Forwarded-Proto http’ to tomcat. So it call setPorts method and use the default port (80) of http protocol to replace the value of serverPort.
It seems RemoteIpValve has high priority so it change serverPort’s value when the serverPort has been set to a correct value before.
So when I set the nginx not to send X-Forwarded-Proto header, the problem be resolved.
But if nginx not to send it, when the website set to visit via https protocol, the tomcat cannot recognize when use request.getScheme().
If there has no solution to let tomcat get correct port when nginx send the header, I will set nginx not to send the header.
> 发件人: Thomas Hoffmann (Speed4Trade GmbH)<ma...@speed4trade.com.INVALID>
> 发送时间: 2022年1月21日 20:41
> 收件人: Tomcat Users List<ma...@tomcat.apache.org>
> 主题: AW: getServerPort always return 80
>
> Hello,
> according to https://apac01.safelinks.protection.outlook.com/?url=https%3A%2F%2Ftomcat.apache.org%2Ftomcat-8.0-doc%2Fservletapi%2Fjavax%2Fservlet%2FServletRequest.html%23getServerPort&data=04%7C01%7C%7C901f707823d142ffcd4608d9dedf229c%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637785872232579567%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=9oPRI12HrPcpjDEA1wisZ4j%2FlkxFX7jEllDpQZjFBlA%3D&reserved=0()
> it is the Port to which the request was sent to.
> Probably your Tomcat listens on Port 80 and nginx was sending the request to that port.
>
> For Proxy, check out the documentation at https://apac01.safelinks.protection.outlook.com/?url=https%3A%2F%2Ftomcat.apache.org%2Ftomcat-9.0-doc%2Fconfig%2Fajp.html%23Proxy_Support&data=04%7C01%7C%7C901f707823d142ffcd4608d9dedf229c%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637785872232579567%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=NVcaPf9u2wnRkd%2FthcHY%2FwaBHP8ekeg4aatYguyLkYw%3D&reserved=0
>
>
> Greetings,
> Thomas
>
> -----Ursprüngliche Nachricht-----
> Von: 王 静凯 <ly...@outlook.com>
> Gesendet: Freitag, 21. Januar 2022 07:26
> An: Tomcat Users List <us...@tomcat.apache.org>
> Betreff: re: getServerPort always return 80>
>
> Hi, any other know this problem? I meet it again today.
> A server has an Internet IP and when nginx listen to 81,the request.getServerPort() return 80.
回复: getServerPort always return 80
Posted by 王 静凯 <ly...@outlook.com>.
Hi,
I have found what cause this problem after compile a tomcat with some logs.
In RemoteIpValve.java’s method ‘invoke’, it check if protocolHeader is null. My nginx send a header ‘X-Forwarded-Proto http’ to tomcat. So it call setPorts method and use the default port (80) of http protocol to replace the value of serverPort.
It seems RemoteIpValve has high priority so it change serverPort’s value when the serverPort has been set to a correct value before.
So when I set the nginx not to send X-Forwarded-Proto header, the problem be resolved.
But if nginx not to send it, when the website set to visit via https protocol, the tomcat cannot recognize when use request.getScheme().
If there has no solution to let tomcat get correct port when nginx send the header, I will set nginx not to send the header.
> 发件人: Thomas Hoffmann (Speed4Trade GmbH)<ma...@speed4trade.com.INVALID>
> 发送时间: 2022年1月21日 20:41
> 收件人: Tomcat Users List<ma...@tomcat.apache.org>
> 主题: AW: getServerPort always return 80
>
> Hello,
> according to https://apac01.safelinks.protection.outlook.com/?url=https%3A%2F%2Ftomcat.apache.org%2Ftomcat-8.0-doc%2Fservletapi%2Fjavax%2Fservlet%2FServletRequest.html%23getServerPort&data=04%7C01%7C%7C901f707823d142ffcd4608d9dedf229c%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637785872232579567%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=9oPRI12HrPcpjDEA1wisZ4j%2FlkxFX7jEllDpQZjFBlA%3D&reserved=0()
> it is the Port to which the request was sent to.
> Probably your Tomcat listens on Port 80 and nginx was sending the request to that port.
>
> For Proxy, check out the documentation at https://apac01.safelinks.protection.outlook.com/?url=https%3A%2F%2Ftomcat.apache.org%2Ftomcat-9.0-doc%2Fconfig%2Fajp.html%23Proxy_Support&data=04%7C01%7C%7C901f707823d142ffcd4608d9dedf229c%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637785872232579567%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=NVcaPf9u2wnRkd%2FthcHY%2FwaBHP8ekeg4aatYguyLkYw%3D&reserved=0
>
>
> Greetings,
> Thomas
>
> -----Ursprüngliche Nachricht-----
> Von: 王 静凯 <ly...@outlook.com>
> Gesendet: Freitag, 21. Januar 2022 07:26
> An: Tomcat Users List <us...@tomcat.apache.org>
> Betreff: re: getServerPort always return 80>
>
> Hi, any other know this problem? I meet it again today.
> A server has an Internet IP and when nginx listen to 81,the request.getServerPort() return 80.
re: getServerPort always return 80
Posted by 王 静凯 <ly...@outlook.com>.
Hi,
I have try proxyName and proxyPort, but the proxyPort can change nothing when proxyName can take effect.
>>> Probably your Tomcat listens on Port 80 and nginx was sending the request to that port.
My tomcat listen 8080 and nginx listen 81, nginx sent the request to 8080 with a host header �C ��ip:81��
According to the definition of getServerPort : ��It is the value of the part after ":" in the Host header value, if any��
�� Windows ���ʼ�<https://go.microsoft.com/fwlink/?LinkId=550986>����
> ������: Thomas Hoffmann (Speed4Trade GmbH)<ma...@speed4trade.com.INVALID>
> ����ʱ��: 2022��1��21�� 20:41
> �ռ���: Tomcat Users List<ma...@tomcat.apache.org>
> ����: AW: getServerPort always return 80
>
> Hello,
> according to https://apac01.safelinks.protection.outlook.com/?url=https%3A%2F%2Ftomcat.apache.org%2Ftomcat-8.0-doc%2Fservletapi%2Fjavax%2Fservlet%2FServletRequest.html%23getServerPort&data=04%7C01%7C%7C61784521d165411600c508d9dcdb5e00%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637783657020615549%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=CCUiWi%2Bm4ouiAHFFhtx9ohvVTYBVKbxQnFn8os0RPRE%3D&reserved=0()
> it is the Port to which the request was sent to.
> Probably your Tomcat listens on Port 80 and nginx was sending the request to that port.
>
> For Proxy, check out the documentation at https://apac01.safelinks.protection.outlook.com/?url=https%3A%2F%2Ftomcat.apache.org%2Ftomcat-9.0-doc%2Fconfig%2Fajp.html%23Proxy_Support&data=04%7C01%7C%7C61784521d165411600c508d9dcdb5e00%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637783657020615549%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=aoegRGk9NEUdq6K2ks%2FBTeQKyCYiGvm65js%2BsbidLNU%3D&reserved=0
>
>
> Greetings,
> Thomas
>
> -----Urspr��ngliche Nachricht-----
> Von: �� ���� <ly...@outlook.com>
> Gesendet: Freitag, 21. Januar 2022 07:26
> An: Tomcat Users List <us...@tomcat.apache.org>
> Betreff: re: getServerPort always return 80>
>
> Hi, any other know this problem? I meet it again today.
> A server has an Internet IP and when nginx listen to 81��the request.getServerPort() return 80.
AW: getServerPort always return 80
Posted by "Thomas Hoffmann (Speed4Trade GmbH)" <Th...@speed4trade.com.INVALID>.
Hello,
according to https://tomcat.apache.org/tomcat-8.0-doc/servletapi/javax/servlet/ServletRequest.html#getServerPort()
it is the Port to which the request was sent to.
Probably your Tomcat listens on Port 80 and nginx was sending the request to that port.
For Proxy, check out the documentation at https://tomcat.apache.org/tomcat-9.0-doc/config/ajp.html#Proxy_Support
Greetings,
Thomas
-----Ursprüngliche Nachricht-----
Von: 王 静凯 <ly...@outlook.com>
Gesendet: Freitag, 21. Januar 2022 07:26
An: Tomcat Users List <us...@tomcat.apache.org>
Betreff: re: getServerPort always return 80
Hi, any other know this problem? I meet it again today.
A server has an Internet IP and when nginx listen to 81,the request.getServerPort() return 80.
re: getServerPort always return 80
Posted by 王 静凯 <ly...@outlook.com>.
Hi, any other know this problem? I meet it again today.
A server has an Internet IP and when nginx listen to 81,the request.getServerPort() return 80.
re: re: getServerPort always return 80
Posted by 王 静凯 <ly...@outlook.com>.
Hi Hua,
I have try to modify the access log format of tomcat.
Add '%{Host}i' in server.xml like this:
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%{Host}i %h %l %u %t "%r" %s %b" />
Then it print 'domain:10001' in log, so I think nginx has passed the correct host header to tomcat.
--------------------
By the way, I found the valve component 'RemoteIpValve' supports an attribute 'portHeader' but I think it just can resolve my second situation. Because nginx can't get the port which visitor using when there is a port mapping. The variable $server_port just reprecent the port which nginx is listening.
--------------------
Maybe 'httpServerPort' in RemoteIpValve can solve my problem. But I think it is not an elegant way. Because it is not a common option (I mean I need to change the valve for different server). This problem not reproduce in every server.
We use tomcat-embed-core-8.5.53 to make a single-step debugging to find how tomcat work, and it work fine on our PC. The 'parseHost()' method work and get the correct port an then set to the 'this.serverPort'.
The source code we track:
Visit ->
Some Process ->
coyote.http11.Http11Processor.service() ->
coyote.http11.Http11InputBuffer.parseHeaders() ->
coyote.http11.Http11InputBuffer.parseHeader() (remark: the request header info save in a MimeHeaders array name headers) ->
return to coyote.http11.Http11Processor.prepareRequest() ->
call 'hostValueMB = headers.getUniqueValue("host");' (remark: get the value of host header save in array named headers) ->
call 'parseHost(hostValueMB);' ->
call 'request.setServerPort(port)' in parseHost() method after finishing to process value of host;
And the getHeader(String name) code :
public String getHeader(String name) {
return headers.getHeader(name);
}
In conclusion, I use getHeader('Host') it return the correct value when the value get from array 'headers', and the parameter transform to parseHost() is get from array 'headers' too. So I think maybe there is some problem in the parseHost() method?
>Hi Wang,
>
>I hope that this link will help you to solve the problem:
>https://apac01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstackoverflow.com%2Fquestions%2F19751313%2Fforward-request-headers-from-nginx-proxy-server&data=04%7C01%7C%7C8baac3e1c61340c4479708d9d0faf542%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637770598551826479%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=LAB05OEZ4isqGbboQcJbzKZigAPNqV%2FiO4zn2I6WiEo%3D&reserved=0
>
>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 王 静凯 <ly...@outlook.com> 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 王 静凯 <ly...@outlook.com> 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-unsubscribe@tomcat.apache.org
> > For additional commands, e-mail: users-help@tomcat.apache.org
> >
> >
>
>
Re: 回复: getServerPort always return 80
Posted by Hua Zhang <hz...@gmail.com>.
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 王 静凯 <ly...@outlook.com> 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 王 静凯 <ly...@outlook.com> 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-unsubscribe@tomcat.apache.org
> > For additional commands, e-mail: users-help@tomcat.apache.org
> >
> >
>
>
回复: 回复: getServerPort always return 80
Posted by 王 静凯 <ly...@outlook.com>.
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 王 静凯 <ly...@outlook.com> 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-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>
Re: 回复: getServerPort always return 80
Posted by Hua Zhang <hz...@gmail.com>.
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 王 静凯 <ly...@outlook.com> 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-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>
回复: 回复: getServerPort always return 80
Posted by 王 静凯 <ly...@outlook.com>.
>> 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 �C ��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 100001
>
>2. You have a Tomcat <Connector> on port 8080:
> <Connector port="8080" protocol="HTTP/1.1"
> connectionTimeout="20000"
> 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?
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-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: 回复: getServerPort always return 80
Posted by Christopher Schultz <ch...@christopherschultz.net>.
王 静凯,
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 100001
2. You have a Tomcat <Connector> on port 8080:
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
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-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org