# Disable access by IP and unknown hosts

**URL:** <https://www.ruby-forum.com/t/disable-access-by-ip-and-unknown-hosts/188393>\
**Category:** NGINX\
**Created:** [May 11, 2010, 8:45am UTC](https://www.ruby-forum.com/t/disable-access-by-ip-and-unknown-hosts/188393 "2010-05-11T08:45:06Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![leonardocg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e274bd/32.png) [@leonardocg](https://www.ruby-forum.com/u/leonardocg)\
**Post date:** [May 11, 2010, 8:45am UTC](https://www.ruby-forum.com/t/disable-access-by-ip-and-unknown-hosts/188393/1 "2010-05-11T08:45:06Z")

</div>

Hi.

How can I disable access by IP to my server and by hosts not defined  
in server\_name?

I only have [www.domain.com](http://www.domain.com) on server\_name but people can still access  
the site via [mail.domain.com](http://mail.domain.com) or by the IP address.

Thanks

Leonardo.

---

<div class="post-metadata">

**Author:** ![leonardocg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e274bd/32.png) [@leonardocg](https://www.ruby-forum.com/u/leonardocg)\
**Post date:** [May 11, 2010, 8:56am UTC](https://www.ruby-forum.com/t/disable-access-by-ip-and-unknown-hosts/188393/2 "2010-05-11T08:56:13Z")

</div>

On Tue, May 11, 2010 at 04:44:33PM +1000, Leonardo C. wrote:

> Hi.
> 
> How can I disable access by IP to my server and by hosts not defined  
> in server\_name?
> 
> I only have [www.domain.com](http://www.domain.com) on server\_name but people can still access  
> the site via [mail.domain.com](http://mail.domain.com) or by the IP address.

> **[How nginx processes a request](http://nginx.org/en/docs/http/request_processing.html#how_to_prevent_undefined_server_names)**
>
> nginx first decides which server should process the request. Let’s start with a simple configuration where all three virtual servers listen on port \*:80:

–  
Igor S.

> **[Igor Sysoev](http://sysoev.ru/en/)**

---

<div class="post-metadata">

**Author:** ![leonardocg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e274bd/32.png) [@leonardocg](https://www.ruby-forum.com/u/leonardocg)\
**Post date:** [May 11, 2010, 9:03am UTC](https://www.ruby-forum.com/t/disable-access-by-ip-and-unknown-hosts/188393/3 "2010-05-11T09:03:14Z")

</div>

Cheers!

"Note that you should set a name for this server, otherwise nginx will  
use the hostname. "

How do I “set a name” for a server in nginx?

Thanks

---

<div class="post-metadata">

**Author:** ![leonardocg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e274bd/32.png) [@leonardocg](https://www.ruby-forum.com/u/leonardocg)\
**Post date:** [May 11, 2010, 9:51am UTC](https://www.ruby-forum.com/t/disable-access-by-ip-and-unknown-hosts/188393/4 "2010-05-11T09:51:50Z")

</div>

Cheers Igor!

---

<div class="post-metadata">

**Author:** ![leonardocg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e274bd/32.png) [@leonardocg](https://www.ruby-forum.com/u/leonardocg)\
**Post date:** [May 11, 2010, 9:16am UTC](https://www.ruby-forum.com/t/disable-access-by-ip-and-unknown-hosts/188393/5 "2010-05-11T09:16:42Z")

</div>

On Tue, May 11, 2010 at 05:02:42PM +1000, Leonardo C. wrote:

> Cheers!
> 
> "Note that you should set a name for this server, otherwise nginx will  
> use the hostname. "
> 
> How do I “set a name” for a server in nginx?

> **[Server names](http://nginx.org/en/docs/http/server_names.html#miscellaneous_names)**
>
> Server names are defined using the server\_name directive and determine which server block is used for a given request. See also “How nginx processes a request”. They may be defined using exact names, wildcard names, or regular expressions:

> > > 
> > 
> > * * *
> > 
> > nginx mailing list  
> > [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> > [mailman.nginx.org archive](http://nginx.org/mailman/listinfo/nginx)
> 
> * * *
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://nginx.org/mailman/listinfo/nginx)

–  
Igor S.

> **[Igor Sysoev](http://sysoev.ru/en/)**

---

<div class="post-metadata">

**Author:** ![leonardocg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e274bd/32.png) [@leonardocg](https://www.ruby-forum.com/u/leonardocg)\
**Post date:** [October 29, 2011, 8:07pm UTC](https://www.ruby-forum.com/t/disable-access-by-ip-and-unknown-hosts/188393/6 "2011-10-29T20:07:08Z")

</div>

Hallo… before i create new thread i want to ask…  
i was try with Igor S. tutorials… but didn`t work.

i want to disable access from browser,  
ex = [http://127.0.0.1/](http://127.0.0.1/)

i have to IP address for my server… how to disable ALL IP access from  
browser.?

Posted at Nginx Forum:

> **[Re: Disable access by IP and unknown hosts](https://forum.nginx.org/read.php?2,85212,217520#msg-217520)**
>
> Hi. How can I disable access by IP to my server and by hosts not defined in server\_name? I only have www.domain.com on server\_name but people can still access the site via mail.domain.com or by the IP address. Thanks Leonardo.

---

<div class="post-metadata">

**Author:** ![leonardocg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e274bd/32.png) [@leonardocg](https://www.ruby-forum.com/u/leonardocg)\
**Post date:** [October 29, 2011, 9:53pm UTC](https://www.ruby-forum.com/t/disable-access-by-ip-and-unknown-hosts/188393/7 "2011-10-29T21:53:56Z")

</div>

On 29 October 2011 21:06, MyName [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> i want to disable access from browser,  
> ex = [http://127.0.0.1/](http://127.0.0.1/)

To disable access by IP address, try setting your default server,  
usually in nginx.conf, as follows:

```
# Default server
server {
    listen 80;
    listen 443;

```

return 403;  
}

---

<div class="post-metadata">

**Author:** ![leonardocg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e274bd/32.png) [@leonardocg](https://www.ruby-forum.com/u/leonardocg)\
**Post date:** [October 30, 2011, 5:58pm UTC](https://www.ruby-forum.com/t/disable-access-by-ip-and-unknown-hosts/188393/8 "2011-10-30T17:58:22Z")

</div>

Hallo Nginx U…

i was try with your config, its work, but when i open with my domain is  
riderect to 403 page… i cant access my domain,  
this is for config server

user nginx;  
worker\_processes 10;

error\_log /var/log/nginx/error.log warn;  
pid /var/run/nginx.pid;

events {  
worker\_connections 1024;  
}

http {  
include /etc/nginx/mime.types;  
default\_type application/octet-stream;

```
log_format main '$remote_addr - $remote_user [$time_local]

```

“$request” ’  
'$status $body\_bytes\_sent “$http\_referer” ’  
‘“$http\_user\_agent” “$http\_x\_forwarded\_for”’;

```
access_log /var/log/nginx/access.log main;

sendfile on;
tcp_nopush on;
tcp_nodelay on;

keepalive_timeout 65;

```

}

server {  
listen 80;  
listen 443;  
return 403;  
server\_name domaingue.crots  
www.domaingue.crots  
;

```
location / {
    root path/to/html;
    index index.php index.html index.htm;
}

```

Posted at Nginx Forum:

> **[Re: Disable access by IP and unknown hosts](https://forum.nginx.org/read.php?2,85212,217545#msg-217545)**
>
> Hi. How can I disable access by IP to my server and by hosts not defined in server\_name? I only have www.domain.com on server\_name but people can still access the site via mail.domain.com or by the IP address. Thanks Leonardo.

---

<div class="post-metadata">

**Author:** ![leonardocg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e274bd/32.png) [@leonardocg](https://www.ruby-forum.com/u/leonardocg)\
**Post date:** [October 30, 2011, 6:23pm UTC](https://www.ruby-forum.com/t/disable-access-by-ip-and-unknown-hosts/188393/9 "2011-10-30T18:23:09Z")

</div>

On 30 October 2011 19:57, MyName [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> index index.php index.html index.htm;  
> }

You need a separate server block for your domain. Example below. You  
need to change to match your specific needs (E.G. location for php)

# Default server

server {  
listen 80;  
listen 443;  
return 403;  
}

# domaingue.crots server

server {  
server\_name domaingue.crots www.domaingue.crots;

# Put “root” and “index” directives at the server level

root path/to/html;  
index index.php index.html index.htm;

# As many locations as needed

location / {  
try\_files $uri $uri/ =404;  
}

# As many locations as needed

location ~ .+.php$ {  
# Linebelow is important for security  
location ~ .._/._.php$ { return 400; }

```
#proxy_pass/fastcgi_pass etc

```

}  
}
