nginx: redirects by pattern and default rule

lasoweq asked:

Domain is no longer in use, and I need to make redirects to correct website, but only for specific urls.

server {
    listen 80;

    server_name example.org;

    location ~ ^/(page|show) {
        return 301 http://new-domain.org/$request_uri;
    }

    location = / {
        return 301 http://new-domain.org/;
    }

    return 410;
}

Only main page and urls matching the pattern will be redirected, everything else should throw 410 HTTP code

but config above is not working, currently everything is throwing 410…

My answer:


Try putting it in a location. Anything in the server block will always match.

location / {
    return 410;
}

View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.