我可以在Nginx上仅对特定服务器块(虚拟主机)启用HTTP/2吗? [英] Can I enable HTTP/2 for specific server blocks (virtual hosts) only, on Nginx?

查看:98
本文介绍了我可以在Nginx上仅对特定服务器块(虚拟主机)启用HTTP/2吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在nginx上有几台虚拟主机.

I have several virtual hosts on nginx.

是否可以仅在nginx上的虚拟主机上启用HTTP/2 ?

Can I enable HTTP/2 for specific virtual hosts only on nginx?

当我为虚拟主机启用HTTP/2时,例如:

When I enable HTTP/2 for a virtual host, like:

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

我可以通过HTTP2.0访问a.b.com.

I can access a.b.com by HTTP2.0.

但是现在同一nginx上的所有其他虚拟主机也支持HTTP/2. 但是我只想通过HTTP/1.1访问它们.

But now every other virtual host on the same nginx supports HTTP/2 too. But I want to access them only by HTTP/1.1.

http2指令是否在服务器级别?

Is the http2 directive at server level?

推荐答案

简短的回答:在当前设置中是不可能的.

启动时,nginx首先为在同一 IP:port 组合上侦听的每组虚拟主机创建一个单独的进程,然后将该进程的功能设置为以下各项的总和该进程处理的该组中每个虚拟主机的所有功能.

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.

在您的情况下,只有一个进程可以处理绑定到*:443的所有虚拟主机,因此该进程包括http2功能.

In your case, there's only one process that handles all the virtual hosts bound to *:443, so the process includes the http2 capability.

为了实现所需的功能,您需要使nginx产生一个在单独的 IP:port 组合上不具有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.

对于要通过http2访问的虚拟主机,您必须:

For the virtual hosts you want to be accessed via http2, you must either:

  • 使用其他端口-琐碎,只需为它们使用另一个端口(例如listen 8443 ssl http2;),然后从所有其他端口中删除http2 (例如,听443 ssl;")
  • 使用其他IP -您需要将另一个IP添加到相同的 NIC ,它使用您当前的IP并相应地修改您的虚拟主机 (例如listen new_ip:443 ssl http2;listen current_ip:443 ssl; 分别)
  • use a different port - trivial, just use another port for them (e.g. listen 8443 ssl http2;) and remove http2 from all the others (e.g. `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 (e.g. listen new_ip:443 ssl http2; and listen current_ip:443 ssl; respectively)

用于多个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;
  ...
}

这篇关于我可以在Nginx上仅对特定服务器块(虚拟主机)启用HTTP/2吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆