Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm trying to use nginx as a reverse proxy for sonarqube, and I'm running into trouble with the static files requests not mapping properly.

My proxy is proxy.com. I want to access sonarqube.internal.com:9000 on proxy.com/sonar, and have it be transparent to the client; everything should work.

I'm halfway there. The sonarqube homepage loads appropropriately. However, all the links on the sonarqube page aren't built quite correctly. They're missing the /sonar location. I.e., I want them to be proxy.com/sonar/dashboard/index/blah, but they're coming up as proxy.com/dashboard/index/blah. So:

my server configuration file:

server {
  listen   80 default;
  server_name  _;

  access_log  /var/log/nginx/localhost.access.log;
  error_log /var/log/nginx/localhost.error.log notice;
  rewrite_log on;

  location / {
    include proxy.conf;
    if ($http_referer ~* /sonar ) {
      rewrite ^(.*) /sonar$1 last;
    }
    proxy_pass http://build.internal.com:8080;
  }

  location /sonar {
    include proxy.conf;
    proxy_pass http://build.internal.com:9000/;
    access_log /var/log/nginx/sonar.access.log;
    error_log /var/log/nginx/sonar.error.log notice;
  }

  location /nexus {
    include proxy.conf;
    proxy_pass http://nexus.internal.com:8081;
    acess_log /var/log/nginx/nexus.access.log;
  }
}

And my proxy.conf:

proxy_redirect          off;
proxy_set_header        Host $host;
proxy_set_header        X-Real-IP $remote_addr;
proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size    10m;
client_body_buffer_size 128k;
proxy_connect_timeout   90s;
proxy_send_timeout      90s;
proxy_read_timeout      90s;
proxy_buffers           32 4k;

The rewrite that I'm doing based on referer works for loading the initial page (which was my first challenge), but then fails to get the links set up correctly. Is there a way I can get all requests destined for that application to have the /sonar location on them?

share|improve this question
    
There is a setting in the sonarqube application that allows me to set a "context", but that means that the original links to the application break, which is also bad. –  qkslvrwolf Sep 19 '13 at 0:34

1 Answer 1

I got it working by doing this:

    location / {
    include proxy.conf;
if ($http_referer ~* /sonar ) {
  rewrite ^(.*) /sonar$1 last;
}
    proxy_pass build.internal.com:8080;
}

  location /sonar {
    include proxy.conf;
    proxy_pass build.internal.com/sonar;
    access_log /var/log/nginx/sonar.access.log;
    error_log /var/log/nginx/sonar.error.log notice;
  }

And then there was a property in the sonarqube configuration to set the context to /sonar.

I would still like to find a way to do this without needing to modify the upstream server at all.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.