大网数据广告活动
服务器运维问答

Correct way to use reuseport in nginx virtual host

· 52CE 我爱测

I use nginx as a reverse proxy with a gunicorn application server (Django app). In my nginx virtual host file, there are two server blocks. The former redirects www traffic to the latter (which handles non-www , https traffic). S…

问题原文

原帖链接 · Hassan Baig · CC BY-SA 4.0

I use nginx as a reverse proxy with a gunicorn application server (Django app).

In my nginx virtual host file, there are two server blocks. The former redirects www traffic to the latter (which handles non-www, https traffic).

Specifically, the former location block is:

server {

    server_name www.example.com;

    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    return 301 https://example.com$request_uri;
}

The latter location block is:

server {

    server_name example.com;
    listen 443 ssl http2 reuseport;
    listen [::]:443 ssl http2 reuseport;

    # other directives here
}

Notice the use of reuseport in the latter block.

If I put reuseportin both blocks, I get an error: nginx: [emerg] duplicate listen options for 0.0.0.0:443 in /etc/nginx/sites-enabled/vhost:62.

If I insert it in the former block solely, it works. I'm torn about where to correctly place it, the former block or the latter one. Can someone clarify?


I understand that the reuseport parameter instructs nginx to create an individual listening socket for each worker process, allowing the kernel to distribute incoming connections between worker processes (to handle multiple packets being sent between client and server).

But that doesn't help me understand why I can't use reuseport in both of my aforementioned blocks, or which one I should use it in (given I can only use it in one).

已采纳解答

原帖链接 · Danila Vershinin · CC BY-SA 4.0

In NGINX, you specify the network socket's listen options only once in configuration and they "apply" to all other configured servers which listen on the same socket (port). Quoting docs:

The listen directive can have several additional parameters specific to socket-related system calls. These parameters can be specified in any listen directive, but only once for a given address:port pair.

So, what you have to decipher from the error message, is that you should only specify reuseport once for every unique listen address+port.

In which server you do this depends on your preference. But as a rule and for clarity, I suggest designating one of the servers with default_server directive, which:

if present, will cause the server to become the default server for the specified address:port pair. If none of the directives have the default_server parameter then the first server with the address:port pair will be the default server for this pair.

Then place your listen options in that server block (where you have specified the default_server).

E.g.

server {

    server_name www.example.com;

    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    return 301 https://example.com$request_uri;
}

server {

    server_name example.com;
    listen 443 ssl http2 default_server reuseport;
    listen [::]:443 ssl http2 default_server reuseport;

    # other directives here
}

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2020-01-25)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。