0

I have two upstreams for two different microservices which nginx does the routing for

server {
    listen 80 default_server;

    root /opt;
    charset utf-8;

    location /micorservice1 {
      try_files /dev/null @micro_one;
    }

    location /microservice2 {
        try_files $uri @micro_two;
    }

    location / {
        return 404 "No such endpoint\n";
    }

    location @micro_one {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $http_x_forwarded_proto;
        proxy_set_header Host $host;
        proxy_redirect off;
        proxy_pass http://micro_one;
    }

    location @micro_two {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $http_x_forwarded_proto;
        proxy_set_header Host $host;
        proxy_redirect off;
        proxy_pass http://micro_two;
    }
}

upstream micro_one {
    server 127.0.0.1:8080 fail_timeout=0;
}

upstream micro_two {
    server 127.0.0.1:8081 fail_timeout=0;
}

Named location location @micro_one here is to set extra headers and keep everything within one block in case I want extra routes to be handled by the same microservice (so I don't need to put proxy_set_header directives on each location declarations)

So how to properly redirect different urls (multiple locations) to be handled by the same microservice?

location /this/path/too {
    try_files /dev/null @micro_one;
}

Currently I'm using try_files but it seems to me like a workaround as I do not want to serve any files here and /dev/null needs to be provided as a parameter.

Your Answer

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

Browse other questions tagged or ask your own question.