I would like to get nginx to redirect certain sub domains to specific server blocks to access services on various ports

server {

    listen 80;
    server_name example.com www.example.com;

    location /crm {
            return 301 http://example.com:8935;

    location /api {
            return 301 http://example.com:4667;

    location /scanner {
            return 301 http://example.com:4466;

    return 301 https://www.example.com/$request_uri?$query_string;


So if someone access example.com/crm then it needs to redirect to a server {} block listening on port 8935.

If its not one of those then it needs to redirect to the regular https web content.

This is not working, it seems to always use the last redirect to the regular site. So when I try /cms I get a 404 not found in the website.

Any way to prevent this from falling through?


Try putting the last option inside a location / block


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.