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 &quot;%r&quot; %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&amp;data=04%7C01%7C%7C8baac3e1c61340c4479708d9d0faf542%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637770598551826479%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&amp;sdata=LAB05OEZ4isqGbboQcJbzKZigAPNqV%2FiO4zn2I6WiEo%3D&amp;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-unsubscr...@tomcat.apache.org
> > For additional commands, e-mail: users-h...@tomcat.apache.org
> >
> >
>
>

Reply via email to