How to preserve the Content-Type header of a Tomca

2019-02-09 15:09发布

I'm having a problem with an incorrect HTTP Response Content-Type header while accessing an Axis2 web service hosted in Tomcat behind Apache through an AJP/1.3 connector.

I can access the web service without problems in the browser through its RESTful interface and I can see the results but somehow Apache is changing the response Content-Type header sent by Tomcat from text/xml to text/plain and it prevents me from consuming the web service through SOAP in NetBeans, because of an Unsupported Content-Type: text/plain Supported ones are: [text/xml] exception.

Here's the relevant section of my Apache vhosts configuration:

<VirtualHost *:80>
    ServerAdmin me@myserver.example
    ServerName  myserver.example
    ServerAlias other.myserver.example

    ProxyPreserveHost On 
    SetEnv force-proxy-request-1.0 1
    SetEnv proxy-nokeepalive 1

    <Location /axis2/services>
        ProxyPass ajp://localhost:8009/axis2/services
        ProxyPassReverse ajp://localhost:8009/axis2/services
    </Location>
</VirtualHost>

And the relevant section of my Tomcat server.xml:

<Connector port="8009" protocol="AJP/1.3" redirectPort="9443" />

<Connector port="9443" protocol="HTTP/1.1" SSLEnabled="true" maxHttpHeaderSize="8192"
    maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
    enableLookups="false" disableUploadTimeout="true"
    acceptCount="100" scheme="https" secure="true"
    clientAuth="false" sslProtocol="TLS"
    SSLCertificateFile="path/to/bundle" 
    SSLCertificateKeyFile="path/to/key"
    SSLPassword="S3cr3t"
    proxyName="myserver.example" proxyPort="443" />

If I access the WS directly in Tomcat using the default connector on port 8080 I get the correct content-type but if I access it through Apache then I get text/plain, so it's definitely a problem with the proxy.

How can I solve this problem?

EDIT: I got it to work by using the Tomcat HTTP connector for the proxying, instead of the AJP one, but I would prefer to use mod_ajp if I find a working solution.

I just changed the

ProxyPass ajp://localhost:8009/axis2/services
ProxyPassReverse ajp://localhost:8009/axis2/services

lines to

ProxyPass http://localhost:8080/axis2/services
ProxyPassReverse http://localhost:8080/axis2/services

3条回答
老娘就宠你
2楼-- · 2019-02-09 15:43
# DefaultType: the default MIME type the server will use for a document
# if it cannot otherwise determine one, such as from filename extensions.
# If your server contains mostly text or HTML documents, "text/plain" is
# a good value.  If most of your content is binary, such as applications
# or images, you may want to use "application/octet-stream" instead to
# keep browsers from trying to display binary files as though they are
# text.
#
DefaultType None

This is the solution. Look at this part in the httpd.conf, It's important that DefalutType is None. If you see plain/text this is the problem. Sorry, this solution is not mine but i dont find out in which blog i've found it :-)

查看更多
虎瘦雄心在
3楼-- · 2019-02-09 15:43

I've spent the past two days at work tracking down something similar to this.

There have been a few bugs producing problems similar to this in the past, in both Apache HTTPD and Tomcat, but most seem to have been resolved at least 2 years ago. I think this one is what anyone using current software is likely to be hitting - it is certainly what I am currently experiencing:

https://issues.apache.org/bugzilla/show_bug.cgi?id=49929

There may be a patch in Tomcat 7.x, but nobody has tested it yet. I plan to do so when I have time in about a week as well as produce a reliable test case so this can get fixed in all relevant versions of tomcat.

This only occurs when using APR, so one intermediate solution is to avoid that (but that could have performance consequences).

查看更多
淡お忘
4楼-- · 2019-02-09 15:50

This solved a head ache for me. I had Apache 2.2 as frontend with mod_proxy_ajp and jboss as backend. Several transactions failed with

Message: Client found response content type of 'text/plain; charset=UTF-8', but expected 'text/xml'.

My Apache did have 'text/plain' as DefaultType. I did not however, change this at the global level (httpd.conf). I went about and added a new line to my vhost config in the <proxy> config section setting it to text/xml.

DefaultType text/xml

I did try using None did not work for me, then I just got:

'' in the error msg instead of 'text/plain'.
查看更多
登录 后发表回答