I have setup a server block with a proxy in nginx which works when I hit the service with an a directory.

For example I can access gs.geolytix.net/geoserver with this conf.

server {
  listen 80;
  server_name gs.geolytix.net;
  location /geoserver {
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-Server $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

So far so good. What I would like to achieve though is that I get to the same address but only address gs.geolytix.net in the browser. Without a directory. If I change the location to / alone I go to gs.geolytix.net the response is gs.geolytix.net//

Thanks, Dennis


If you want the root URI / to quietly map to /geoserver, then a simple method is with an internal rewrite:

location = / {
    rewrite ^ /geoserver last;

EDIT: If you want an external rewrite, use the same location block with a return statement:

location = / {
    return 302 /geoserver;

The location directive is documented here. The rewrite and return directives are documented here.

  • Thanks for the tip. What I looking for is a simple re-write. When I enter gs.geolytix.net I want to get gs.geolytix.net/geoserver in the browser. Currently I get the default nginx page when I type the domain without a directory address. Sep 6 '16 at 17:14
  • I have updated my answer Sep 6 '16 at 17:16
  • This doesn't seem to work in combination with the proxy_pass. If I add the return inside the location location block I am no longer able to restart the nginx service. Sep 6 '16 at 17:42
  • Sorry, it needs to be the long form if the short URI is used. Either 301 or 302 will work as a response code. Sep 6 '16 at 17:55
  • that works. perfect... Sep 6 '16 at 18:04

You can do this by having a "URI" in the proxy_pass, which acts a sort of implicit rewrite:

location / {
    # Note the "/" at the end of the proxy_pass, this will cause it to 
    # replace the location block (/ in this case) with "/geoserver/"
    # when talking to origin.
    # So gs.geolytix.net/test becomes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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