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 am having trouble doing something so simple...

I have a service on my server that I configured to use a particular hostname which I have mapped with an A record. However I want to return a 403 error when this subdomain is accessed with a browser.

But it does not seem to work. I just followed the same steps I used to disallow web traffic from going to the ip address directly.

server {
    listen  80;
    server_name     155.155.155.155;
    return  403;
}

This worked fine but when I do this

server {
    listen  80;
    server_name     service.mydomain.com;
    return  403;
}

It doesn't 403. It just goes to the Nginx test page.

What would be the elegant solution for doing something like this?

share|improve this question

1 Answer 1

If the request for service.mydomain.com goes to a nginx site test there must be an other vhost that matches the server name first, e.g., *.mydomain.com or a vhost acting as default_server. Check your config and remove that one. Change your default vhost to

server {
    listen  80   default_server;
    return  403;
}

That way not only direct access to your IP but to any vhost that is not explicitly set will be rejected, e.g. requests to *.mydomain.com or to otherdomain.com.

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.