I'm right understand that it's wrong to use "reuseport" for same IP:PORT pair on different virtual hosts:
http {
server {
listen 192.168.0.1:80 reuseport;
server_name server1;
…
}
server {
listen 192.168.0.1:80 reuseport;
server_name server2;
…
}
}
This config gives me:
nginx: [emerg] duplicate listen options for 192.168.0.1:80 in /etc/nginx/vhosts/server1.local.conf:66
or
nginx: [emerg] listen() to 0.0.0.0:80, backlog 511 failed (98: Address already in use)
So I've to use unique IP:PORT pairs for every virtual host?
In same time server-wide "listen 80 reuseport;" works just fine, but is it doing same as per unique IP:PORT ?
Answer to your last question - in nginx, the listen
directive is only allowed in the server
context (that means per virtual host).
According to manual:
The
listen
directive can have several additional parameters specific to socket-related system calls. These parameters can be specified in anylisten
directive, but only once for a given address:port pair.
So if you have more than 1 virtual host (server
definition in nginx config), then you can use the reuseport
option in any 1 of them. Non-socket related options (like ssl
or spdy
) can still be set for more than 1 listen
directive.
SIDE NOTE: What the reuseport
directive really does:
Nginx from version 1.9.1 supports setting the SO_REUSEPORT
TCP socket parameter. In modern OS (Linux kernel since 3.9), this enables the kernel to have more socket listeners for each socket (ip:port).
Without it, when new connection arrives, kernel notified all nginx workers about it and all of them try to accept
it.
With this option enabled, each worker has its own listening socket and on each new connection, kernel chooses one of them which will receive it - so there is no contention.
More info about benefits, drawbacks and benchmarks of reuseport
option can be read on this Nginx blog post
Only one listen directive per port/ip
pair should have the reuseport
option.
So just remove the reuseport
from server2
vhost.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With