简体   繁体   English

我可以在Nginx上为特定服务器块(虚拟主机)启用HTTP / 2吗?

[英]Can I enable HTTP/2 for specific server blocks (virtual hosts) only, on Nginx?

I have several virtual hosts on nginx . 我在nginx上有几个虚拟主机。

Can I enable HTTP/2 for specific virtual hosts only on nginx ? 我是否可以nginx上为特定虚拟主机启用HTTP/2

When I enable HTTP/2 for a virtual host, like: 当我为虚拟主机启用HTTP/2 ,例如:

server {
  listen        443 ssl http2;
  server_name   a.b.com;
  ...
}

I can access abcom by HTTP2.0. 我可以通过HTTP2.0访问abcom。

But now every other virtual host on the same nginx supports HTTP/2 too. 但是现在同一个nginx上的每个其他虚拟主机也支持HTTP / 2。 But I want to access them only by HTTP/1.1 . 但我想只通过HTTP/1.1访问它们。

Is the http2 directive at server level? http2指令是否在服务器级别?

Short answer: not possible on your current setup. 简短回答:目前的设置无法实现。

When starting, nginx first creates a separate process for every group of virtual hosts that listen on the same IP:port combination, and then sets the capabilities of that process to be the sum of all capabilities of every virtual host in that group handled by said process. 启动时, nginx首先为侦听同一IP:端口组合的每组虚拟主机创建一个单独的进程,然后将该进程的功能设置为该组处理的该组中每个虚拟主机的所有功能的总和。处理。

In your case, there's only one process that handles all the virtual hosts bound to *:443 , so the process includes the http2 capability. 在您的情况下,只有一个进程处理绑定到*:443所有虚拟主机,因此该进程包括http2功能。

In order to achieve what you want, you need to make nginx spawn a different process that doesn't have the http2 capability on a separate IP:port combination. 为了实现您想要的,您需要使nginx生成一个不同的进程,该http2在单独的IP:端口组合上没有http2功能。

For the virtual hosts you want to be accessed via http2 , you must either: 对于要通过http2访问的虚拟主机,您必须:

  • use a different port - trivial, just use another port for them (eg listen 8443 ssl http2; ) and remove http2 from all the others (eg `listen 443 ssl;) 使用一个不同的端口 - 琐碎,只需为他们使用另一个端口(例如, listen 8443 ssl http2; )并从所有其他端口中移除http2 (例如`listen 443 ssl;)
  • use a different IP - you need to add another IP to the same NIC that uses your current IP and modify your virtual hosts accordingly (eg listen new_ip:443 ssl http2; and listen current_ip:443 ssl; respectively) 使用不同的IP - 您需要将另一个IP添加到使用当前IP的同一NIC并相应地修改您的虚拟主机(例如, listen new_ip:443 ssl http2;并分别listen current_ip:443 ssl;

Example config for multiple IPs: 多个IP的配置示例:

server {
  listen        current_ip:443 ssl;
  server_name   http11-host.example.com;
  ...
}

server {
  listen        current_ip:443 ssl;
  server_name   another-http11-host.example.com;
  ...
}

...
...

server {
  listen        new_ip:443 ssl http2;
  server_name   http2-host.example.net;
  ...
}

server {
  listen        current_ip:443 ssl http2;
  server_name   another-http2-host.example.org;
  ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM