10

I am trying to set up TeamCity behind nginx. I'd like https://public.address.com/teamcity/... to redirect to http://127.0.0.1:8111/..., but even though nginx does this successfully, the login page comes back with references that look like this:

<script type="text/javascript" src="/res/-8762791360234593415.js?v=1305815890782"></script>

Obviously, this won't do, and fiddling with the rootURL setting (Server URL: in Server Configuration) doesn't make any difference.

How do I run TeamCity behind a proxy under a non-root URL?


FWIW, here's the relevant portion of my nginx config:

location /teamcity/ {
    proxy_pass       http://127.0.0.1:8111/;
    proxy_redirect   http://127.0.0.1:8111/ https://$host/teamcity/;
}
Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
  • just in case, here is the [latest official documentation](https://www.jetbrains.com/help/teamcity/how-to.html#Set+Up+TeamCity+behind+a+Proxy+Server) for setting up reverse proxy for the TeamCity server. – KIR Apr 08 '21 at 11:58

2 Answers2

18

I did this using the standard Teamcity Windows installer, and presumably it would work on any platform.

Change Teamcity Location

As per a comment by a JetBrains employee:

To change TeamCity address from http://server/ to http://server/teamcity/, rename the <TeamCity home>\webapps\ROOT directory to <TeamCity home>\webapps\teamcity.

Note also that you'll need to rename this directory every time you upgrade Teamcity.

Proxy configuration

The nginx config then looks something like:

    location /teamcity/ {
            proxy_pass http://teamcity-server.domain.com/teamcity/;
    }

Or you can use Apache (I switched to Apache due to authentication requirements I had):

    <Location /teamcity>
            ProxyPass http://teamcity-server.domain.com/teamcity
            ProxyPassReverse http://teamcity-server.domain.com/teamcity
    </Location>

Redirect old URL

I also created a new <Teamcity home>\webapps\ROOT, and put a index.jsp file into it, which redirects to the new URL so old links continue to work (eg, if someone goes to http://teamcity-server.domain.com it redirects to http://teamcity-server.domain.com/teamcity):

<!DOCTYPE html>
<html>
<head>
  <title>TeamCity</title>
  <meta http-equiv="refresh" content="0;url=/teamcity/overview.html"/>
</head>
<body>
  <!-- no content -->
</body>
</html>

You could also do the redirect in nginx/apache, but doing on the Teamcity server means if someone goes to the old URL directly on the teamcity web server (instead of via your proxy) they'll still get correctly redirected (instead of a 404).

gregmac
  • 24,276
  • 10
  • 87
  • 118
  • Just wanted to clarify: The ROOT folder should be renamed to your target name, not moved into a folder of your target name – Frederik Oct 02 '14 at 17:16
  • @Frederik Right. The *nix in me coming out.. (move == rename). I'll update my answer. – gregmac Oct 02 '14 at 18:52
  • Also another thing to add: While this will make the base webapp function, the build agent will have a status of "disconnected". You need to update buildAgent/conf/buildAgent.properties with your new URL. – Frederik Oct 07 '14 at 10:15
  • Thanks! I've been searching for a solution for days, and this worked perfectly for me! I have several web apps running on the same windows box (serving on different, non-standard ports). I'm using nginx to map requests for http://my.server.local/teamcity -> http://my.server.local:9001. I had the nginx config part correct already, but teamcity wasn't loading static image files correctly. Re-naming \webapps\ROOT to \webapps\teamcity was the key for my setup. – Matt Merrifield Dec 12 '14 at 00:38
4

(I eventually tracked down a solution myself...)

Install tomcat, then install the WAR version of TeamCity, which is in the download area above the Java EE Container tab. This exposes TeamCity under a base URL that you can choose at the time you install the WAR.

The simplest approach is to copy the .war file into Tomcat's webapps directory, giving it a name that matches the desired base URL. For instance, installing teamcity.war into $TOMCAT_HOME/webapps will load TeamCity under the url http://localhost:8080/teamcity (assuming the default Tomcat install). Proxying from https://public.address.com/teamcity to this internal address should be fairly straighforward in nginx.

I had trouble getting it to run immediately after I installed the .war file, but after restarting Tomcat, it all came good.

Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
  • 4
    You may also need to add scheme="https" into /conf/server.xml Connector element (or add one more Connector specific for proxy) to to make TeamCity know all urls must be https:// – Eugene Petrenko Jun 19 '12 at 11:11
  • Excellent suggestion to use the same relative URL. I did the same to redirect an https connection to the :8080 internal and it works like a charm, where it was failing with another relative URL. – BxlSofty Jun 25 '14 at 09:18