# Binding nginx to a single interface

**URL:** <https://www.ruby-forum.com/t/binding-nginx-to-a-single-interface/216630>\
**Category:** NGINX\
**Created:** [February 9, 2012, 6:43pm UTC](https://www.ruby-forum.com/t/binding-nginx-to-a-single-interface/216630 "2012-02-09T18:43:20Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [February 9, 2012, 6:43pm UTC](https://www.ruby-forum.com/t/binding-nginx-to-a-single-interface/216630/1 "2012-02-09T18:43:20Z")

</div>

I have two nics on my server. I have nginx set to listen on one of them  
using “listen 10.1.2.3 80” However, it keeps listening on the other ip  
as  
well. So i’m unable to start anything else on port 80 on that  
interface.

Any ideas what could be wrong? Everything I’m finding says that the  
listen  
command is all I needed to do.

---

<div class="post-metadata">

**Author:** ![Bai\_Shen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/6a8cbe/32.png) [@Bai\_Shen](https://www.ruby-forum.com/u/Bai_Shen)\
**Post date:** [February 9, 2012, 6:45pm UTC](https://www.ruby-forum.com/t/binding-nginx-to-a-single-interface/216630/2 "2012-02-09T18:45:49Z")

</div>

listen ip:port; or listen ip port;?

actually in that case it would be listening on default for the ip AND  
for the port as an address (weird scenario).

version?

-a.

---

<div class="post-metadata">

**Author:** ![Bai\_Shen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/6a8cbe/32.png) [@Bai\_Shen](https://www.ruby-forum.com/u/Bai_Shen)\
**Post date:** [February 9, 2012, 6:50pm UTC](https://www.ruby-forum.com/t/binding-nginx-to-a-single-interface/216630/3 "2012-02-09T18:50:49Z")

</div>

listen ip:port;

I realized after I sent the email that I’d typoed that. It’s the newest  
version, 1.0.11

---

<div class="post-metadata">

**Author:** ![Bai\_Shen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/6a8cbe/32.png) [@Bai\_Shen](https://www.ruby-forum.com/u/Bai_Shen)\
**Post date:** [February 9, 2012, 6:52pm UTC](https://www.ruby-forum.com/t/binding-nginx-to-a-single-interface/216630/4 "2012-02-09T18:52:04Z")

</div>

then grep your whole config and included files/config folders for any  
listen statements, there must be something in there. i’m currently using  
the same version with multiple interfaces and different servers on each  
one and it’s working fine.

-a.

---

<div class="post-metadata">

**Author:** ![Bai\_Shen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/6a8cbe/32.png) [@Bai\_Shen](https://www.ruby-forum.com/u/Bai_Shen)\
**Post date:** [February 9, 2012, 7:03pm UTC](https://www.ruby-forum.com/t/binding-nginx-to-a-single-interface/216630/5 "2012-02-09T19:03:54Z")

</div>

I’m using the default nginx.conf file with no changes. I’ve added my  
own  
config file in the conf.d directory.

Inside that I have two server {} blocks. They both have “listen  
10.1.2.3:80”  
and different server\_names.

Yet when I telnet to 10.1.2.4 on port 80, I get a response from nginx.

---

<div class="post-metadata">

**Author:** ![Bai\_Shen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/6a8cbe/32.png) [@Bai\_Shen](https://www.ruby-forum.com/u/Bai_Shen)\
**Post date:** [February 9, 2012, 6:55pm UTC](https://www.ruby-forum.com/t/binding-nginx-to-a-single-interface/216630/6 "2012-02-09T18:55:10Z")

</div>

Hello!

On Thu, Feb 09, 2012 at 12:42:47PM -0500, Bai Shen wrote:

> I have two nics on my server. I have nginx set to listen on one of them  
> using “listen 10.1.2.3 80” However, it keeps listening on the other ip as  
> well. So i’m unable to start anything else on port 80 on that interface.
> 
> Any ideas what could be wrong? Everything I’m finding says that the listen  
> command is all I needed to do.

Make sure all server{} blocks in your config have the above listen  
explicitly specified. If server{} block have no listen directives  
at all, nginx will use “listen 80” by default, and this may be a  
culprit.

Maxim D.

---

<div class="post-metadata">

**Author:** ![Bai\_Shen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/6a8cbe/32.png) [@Bai\_Shen](https://www.ruby-forum.com/u/Bai_Shen)\
**Post date:** [February 9, 2012, 7:07pm UTC](https://www.ruby-forum.com/t/binding-nginx-to-a-single-interface/216630/7 "2012-02-09T19:07:01Z")

</div>

They do.

However, I do have some weird behaviour. I have the server\_name set to  
[www.example.com](http://www.example.com) and that correctly connects me to my web server. But if  
I  
type in 10.1.2.3, that connects me to my web server as well, even though  
I  
don’t have a default rule setup.

When I go to 10.1.2.4 I get a “Welcome to nginx!” page.

---

<div class="post-metadata">

**Author:** ![Bai\_Shen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/6a8cbe/32.png) [@Bai\_Shen](https://www.ruby-forum.com/u/Bai_Shen)\
**Post date:** [February 9, 2012, 7:13pm UTC](https://www.ruby-forum.com/t/binding-nginx-to-a-single-interface/216630/8 "2012-02-09T19:13:12Z")

</div>

Found it. There was a default.conf file in there that had “listen 80”  
Thanks.

---

<div class="post-metadata">

**Author:** ![Bai\_Shen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/6a8cbe/32.png) [@Bai\_Shen](https://www.ruby-forum.com/u/Bai_Shen)\
**Post date:** [February 13, 2012, 5:09pm UTC](https://www.ruby-forum.com/t/binding-nginx-to-a-single-interface/216630/9 "2012-02-13T17:09:01Z")

</div>

But I’m not defining an ip server\_name. Isn’t nginx listening for  
server\_names?

Right now I have [example.com](http://example.com) rewriting to [www.example.com](http://www.example.com) They both  
listen  
on 10.1.2.3. Previously, I could connect to 10.1.2.3 and it would  
redirect  
me to the web server. Now when I connect to 10.1.2.3 it rewrites the  
url  
to [www.example.com](http://www.example.com) and because I’m internal, that never resolves. How  
do I  
set nginx to do the redirect without the rewrite?

---

<div class="post-metadata">

**Author:** ![Bai\_Shen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/6a8cbe/32.png) [@Bai\_Shen](https://www.ruby-forum.com/u/Bai_Shen)\
**Post date:** [February 10, 2012, 12:34am UTC](https://www.ruby-forum.com/t/binding-nginx-to-a-single-interface/216630/10 "2012-02-10T00:34:42Z")

</div>

Hello!

On Thu, Feb 09, 2012 at 01:06:14PM -0500, Bai Shen wrote:

> They do.
> 
> However, I do have some weird behaviour. I have the server\_name set to  
> [www.example.com](http://www.example.com) and that correctly connects me to my web server. But if I  
> type in 10.1.2.3, that connects me to my web server as well, even though I  
> don’t have a default rule setup.
> 
> When I go to 10.1.2.4 I get a “Welcome to nginx!” page.

When selecting server{} based on server\_name nginx will look only  
through server{} blocks which have the listen socket defined.

That is, if you have

```
server {
    listen 80;
    server_name default;
}

server {
    listen 10.1.2.3:80;
    server_name example.com;
}

```

nginx will never consider “default” server if connection comes to  
10.1.2.3:80. All requests to 10.1.2.3:80 will end up in  
“[example.com](http://example.com)” server as it’s the only server defined for the  
listen socket in question.

More details may be found here:

> **[How nginx processes a request](http://nginx.org/en/docs/http/request_processing.html#mixed_name_ip_based_servers)**
>
> 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:

and in docs.

Maxim D.

---

<div class="post-metadata">

**Author:** ![Bai\_Shen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/6a8cbe/32.png) [@Bai\_Shen](https://www.ruby-forum.com/u/Bai_Shen)\
**Post date:** [February 13, 2012, 9:50pm UTC](https://www.ruby-forum.com/t/binding-nginx-to-a-single-interface/216630/11 "2012-02-13T21:50:09Z")

</div>

Hello!

On Mon, Feb 13, 2012 at 11:08:25AM -0500, Bai Shen wrote:

> But I’m not defining an ip server\_name. Isn’t nginx listening for  
> server\_names?
> 
> Right now I have [example.com](http://example.com) rewriting to [www.example.com](http://www.example.com) They both listen  
> on 10.1.2.3. Previously, I could connect to 10.1.2.3 and it would redirect  
> me to the web server. Now when I connect to 10.1.2.3 it rewrites the url  
> to [www.example.com](http://www.example.com) and because I’m internal, that never resolves. How do I  
> set nginx to do the redirect without the rewrite?

Sorry, I wasn’t able to understand your question. Though overal  
you probably need to set server\_name\_in\_redirect to off, see here:

> **[Module ngx\_http\_core\_module](http://nginx.org/en/docs/http/ngx_http_core_module.html#server_name_in_redirect)**
>
> This directive appeared in version 1.11.8.

And you probably want to re-read the article here:

> **[How nginx processes a request](http://nginx.org/en/docs/http/request_processing.html)**

Maxim D.

> > > [www.example.com](http://www.example.com) and that correctly connects me to my web server. But  
> > > That is, if you have
> > 
> > and in docs.
> > 
> > Maxim D.
> > 
> > * * *
> > 
> > nginx mailing list  
> > [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> > [mailman.nginx.org archive](http://mailman.nginx.org/mailman/listinfo/nginx)
