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)<mailto:thomas.hoffm...@speed4trade.com.INVALID>
> 发送时间: 2022年1月21日 20:41
> 收件人: Tomcat Users List<mailto:users@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&amp;data=04%7C01%7C%7C901f707823d142ffcd4608d9dedf229c%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637785872232579567%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&amp;sdata=9oPRI12HrPcpjDEA1wisZ4j%2FlkxFX7jEllDpQZjFBlA%3D&amp;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&amp;data=04%7C01%7C%7C901f707823d142ffcd4608d9dedf229c%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637785872232579567%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&amp;sdata=NVcaPf9u2wnRkd%2FthcHY%2FwaBHP8ekeg4aatYguyLkYw%3D&amp;reserved=0
>
>
> Greetings,
> Thomas
>
> -----Ursprüngliche Nachricht-----
> Von: 王 静凯 <ly...@outlook.com>
> Gesendet: Freitag, 21. Januar 2022 07:26
> An: Tomcat Users List <users@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.


Reply via email to