Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am still newbie to nginx.

I am trying to block every request that came from somewhere but not server_name.

According to documentation it should work but I didn't succede.

I assume there are should be another method, say, block in if condition by checking $hostname.

What do you suggest to do in order to block request?


share|improve this question
Do you mean that you're trying to check the source of the connection? Or that you're trying to make the server answer only requests with the correct HTTP Host header? – Shane Madden May 22 '12 at 15:52
yes, I want to check the correct HTTP host header – com May 22 '12 at 15:53
A seperate server section should have worked just fine, then, and is better than using an if statement. What configuration did you try that didn't work? – Shane Madden May 22 '12 at 15:55

Make a default server section to catch requests for invalid virtual hosts.

server {
    listen 80 default_server;
    return 404;
server { 
    listen 80;
share|improve this answer
Improved your answer a bit, if you don't mind. – Dmitry Verkhoturov May 23 '12 at 11:16
Thanks for the answer, but something went wrong, I blocked also the Can I print the hostname from nginx configuration let's say to log, just in order to compare it with my server_name? – com May 23 '12 at 12:48
Made a correction - Added listening port to the second server config - this should fix the trouble you've had. – Sašo May 23 '12 at 16:39

Your Answer


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.