Tomcat and proxy settings

72,033

Solution 1

No, Tomcat won't automatically use the system proxy settings.

I suggest you look into the facilities provided by java.net.Proxy. This allows you to dynamically specifiy a proxy at runtime. The system properties work but they are only read once, and if Tomcat has already used an HttpURLConnection for its own purposes prior to you setting them that's the end of that: the setting has no effect.

Solution 2

I do not agree with the usage of java.net.Proxy.

What happens if you need to change it ? New build, new release. The setting of the proxy should be easy. It works well with both system properties or tomcat JAVA_OPTS. I used it in both ways. Just pay attention and be sure you know what JAVA_OPTS are loaded, what java is used and so on, because there are tomcats that have their own java version. Regardint the previous post, there is no way java can be used before was loaded :). So Tomcat cannot use it before the system properties are used...only if tomcat uses another JRE that does not read system properties.

I just test this setup :

set "JAVA_OPTS=%JAVA_OPTS% -Dhttp.proxyHost=proxy.com -Dhttp.proxyPort=8080 "

in catalina.bat and works well.

Solution 3

While specifying proxy settings, you have to define the proxy server name like below:

"-Dhttp.proxyHost=proxy.example.com"

Solution 4

You can implement HTTP proxy, HTTPS proxy and HTTP/HTTPS non-proxy hosts also in Tomcat. You need to update two files i.e, bin/Catalina.sh and conf/catalina.properties.

Solution 5

Create a /bin/setenv.sh (for WINDOWS \bin\setenv.bat):

JAVA_OPTS="-Dhttp.proxySet=true -Dhttp.proxyHost=<proxy_hostname> -Dhttp.proxyPort=<port_number> -Dhttp.nonProxyHosts=<domain_one>|<domain two> $JAVA_OPTS"

NOTE: if you already have setenv.sh/setenv.bat, you can add a line of above command. Tomcat startup script automatically runs setenv script before starting a tomcat instance.

Share:
72,033
icedek
Author by

icedek

Masters student CS #SOreadytohelp

Updated on July 09, 2022

Comments

  • icedek
    icedek almost 2 years

    There is a servlet running on tomcat7 and it makes a webservice call to a third party website. The call works fine from the windows machine but when run from tomcat it fails. Wont Tomcat automatically use the Windows' proxy settings? I added

    set JAVA_OPTS=%JAVA_OPTS% "-Dhttp.proxySet=true"
    set JAVA_OPTS=%JAVA_OPTS% "-Dhttp.proxyHost=IP"
    set JAVA_OPTS=%JAVA_OPTS% "-Dhttp.proxyPort=8080"
    

    to CATALINA.BAT and

    http.proxyHost=IP
    http.proxyPort=8080
    

    to catalina.properties But still there is no change. How do we set Tomcat to use the proxy settings of windows and is there a way to check if tomcat is picking up the proxy settings specified?

    • user207421
      user207421 almost 12 years
      http.proxySet does nothing, it is an urban myth.
    • icedek
      icedek almost 12 years
      I see.How do you suggest I tackle the problem.
    • Jaime Hablutzel
      Jaime Hablutzel over 6 years
      http.proxyHost and http.proxyPort should work. Are you sure your JAVA_OPTS variable is taking into effect?.
  • icedek
    icedek almost 12 years
    So in other words,it is best to do it in java. Also HttpURLConnection if already used in Tomcat, Is there no way of overriding it?Would java.net.Proxy override them?
  • user207421
    user207421 almost 12 years
    @icedek When you look up the Javadoc, as suggested, your questions will be answered.
  • spiritoo
    spiritoo over 10 years
    As said by Nicolae Petridan, doing this in the code can be ugly to maintain. Either use java.net.Proxy or the system properties, which do work properly: it depends on your use case (permanent proxy because your server is behing a company proxy, or specific proxy calls which vary at every HTTP request). Icedek's problem was likely caused by another JVM being used and settings ignored. See also : stackoverflow.com/questions/1432961/…
  • user207421
    user207421 over 10 years
    @spirito And as said above, using the system properties can be infeasible. There is no simple answer here.
  • Diego Magdaleno
    Diego Magdaleno about 9 years
    Best solution in my opinion.
  • giannisapi
    giannisapi about 8 years
    best sollution and according to the documentation.
  • user207421
    user207421 about 5 years
    What happens if you need to change it is that you put the configuration elements (host, port) into a configuration file, which is all you have to change. No new build required, and no release either necessarily.
  • user207421
    user207421 about 5 years
    http.proxySet does nothing. Try setting it false and see for yourself. It is an urban myth.
  • jinzo78
    jinzo78 about 4 years
    Sorry for the necropost, I just wanted to tell that this solution is still working (CentOS 7, Tomcat 8.0.53, installed from .tar.gz) with the right modifications. Thanks to who posted the solution, you restored my mental sanity! :D
  • Stomf
    Stomf about 4 years
    It's now likely you'll need to set the https proxy instead -Dhttps.proxyHost=proxy.com -Dhttps.proxyPort=8080