Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

http

nginx

http2

I have several virtual hosts on nginx.

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

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

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

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

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.

Is the http2 directive at server level?

like image 209
kino lucky Avatar asked Dec 06 '16 03:12

kino lucky


1 Answers

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.

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

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.

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

  • 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)

Example config for multiple IPs:

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;
  ...
}
like image 199
alindt Avatar answered Sep 19 '22 14:09

alindt