如何在 Ubuntu 和 CentOS 上启用 Nginx 的 HTTP/2 协议支持
HTTP/2 nginx
![](/data/attachment/album/201702/28/132038abpn30ddtazcnw5o.jpg)


HTTP/2 是 HTTP 网络协议的主要修订版本，其专注于 HTTP 协议的性能改进。HTTP/2 协议的目标是减少延迟，并且允许在 Web 浏览器和服务器之间的一个连接上并行发起多个请求，因此 Web 应用程序会更快。在本篇教程中，我们将像你展示如何在安装有 Ubuntu 或 CentOS 作为操作系统的 Linux VPS 上使用开启 Nginx 的 HTTP/2 协议。如果你使用 Apache，你可以查看我们的另一篇教程：[如何在 Ubuntu 上开启 Apache 的 HTTP/2 协议](https://www.rosehosting.com/blog/how-to-set-up-apache-with-http2-support-on-ubuntu-16-04/)。


### 必备条件


为了能够按照本篇教程最终在服务器上启用 HTTP/2 协议，你需要先安装好 Nginx 。并且确保功能正常而且配置没有错误。你可以使用下面的命令来检查一下：



```
sudo nginx -t

```

此外，你需要有服务器的 root 访问权限，或者至少有一个具有 sudo 权限的非 root 系统用户，以便你在修改 Nginx 配置文件的时候不会出现权限问题。最后你需要有一个域名和一个颁发给这个域名的有效的 SSL 证书。


### 在 Ubuntu 上开启 Nginx 的 HTTP/2 协议


为了在 Ubuntu VPS 上开启 Nginx 的 HTTP/2 协议，你需要编辑默认的 Nginx 的服务（`server`）块，我们使用的是 `nano`，你可以使用你自己的文本编辑器。



```
sudo nano /etc/nginx/sites-available/default

```

增加下面的服务块：



```
server {  
        server_name domain.com www.domain.com;
        listen 443 ssl http2 default_server;
        root /var/www/html;
        index index.html;

        location / {
                try_files $uri $uri/ =404;
        }

        ssl_certificate /etc/nginx/ssl/domain.com.crt;
        ssl_certificate_key /etc/nginx/ssl/domain.com.key;
}

server {
       listen         80;
       server_name    domain.com www.domain.com;
       return         301 https://$server_name$request_uri;
}

```

确保 `domain.com` 替换成你真正的域名。 此外，应正确设置文档根（`root`）目录，还有 SSL 证书和密钥的路径。


当你编辑完成这个服务块之后，需要保存并关闭文件。使用以下命令检查 Nginx 配置是否有错误：



```
sudo nginx -t

```

为了刚刚的改变生效，需要重启 Nginx：



```
sudo systemctl restart nginx.service

```

如果你想为另一个域名开启 HTTP/2 协议，你可以查看我们的博客[如何在 Ubuntu 和 CentOS 上设置 Nginx 服务块](https://www.rosehosting.com/blog/how-to-set-up-nginx-server-blocks-on-ubuntu-and-centos/)。


### 在 CentOS 上开启 Nginx 的 HTTP/2 协议


为了在 CentOS VPS 开启 Nginx 的 HTTP/2 协议，你需要按照 Ubuntu 上完全相同的步骤做。唯一的不同点是 Nginx 块文件的位置。为了在 CentOS 上编辑默认的 Nginx 服务块，你需要进入 `/etc/nginx/conf.d` 这个文件夹。



```
# nano /etc/nginx/conf.d/default.conf

```

再次检查配置是否有错误，保存并关闭文件，然后使用以下命令重新启动 Nginx 服务：



```
# systemctl restart nginx.service

```

为了检测 Nginx 的 HTTP/2 协议是否开启成功，你可以使用一些[在线 HTTP/2 检测工具](https://www.rosehosting.com/network-tools/http2-support.html)。




---


via: <https://www.rosehosting.com/blog/how-to-enable-http2-in-nginx-on-ubuntu-and-centos/>


作者：[rosehosting.com](https://www.rosehosting.com/blog/how-to-enable-http2-in-nginx-on-ubuntu-and-centos/) 译者：[Flowsnow](https://github.com/Flowsnow) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
QWe’ll show you, how to enable HTTP/2 in Nginx on Ubuntu and CentOS. HTTP/2 is a major revision of the HTTP network protocol and it focuses on performance improvements. Its goal is to reduce the latency as well as to make the web applications faster by allowing multiple concurrent requests between the web browser and the server across a single TCP connection. In this tutorial, we are going to show you how to enable HTTP/2 in Nginx on a Linux VPS using Ubuntu or CentOS as an operating system. If you use Apache, you can check our tutorial on [how to enable HTTP/2 in Apache on Ubuntu](https://www.rosehosting.com/blog/how-to-set-up-apache-with-http2-support-on-ubuntu-16-04/).


Table of Contents

## Prerequisites

In order to be able to follow the instructions and enable HTTP/2 on your server, you need to have [Nginx](https://www.rosehosting.com/nginx-hosting.html) already preinstalled. Make sure that it is functional and there are no errors with its configuration. You can check this using the command below:

sudo nginx -t

Additionally, you need to have root access to the server or at least you need to have a non-root system user with sudo privileges so you can make changes in the Nginx configuration files without having permission problems. Finally, you need to have a [domain name](https://secure.rosehosting.com/clientarea/index.php?/checkdomain/domain-registration/) and valid [SSL certificate](https://www.rosehosting.com/ssl-certificates.html) issued for the domain name.

## Enable HTTP/2 in Nginx on Ubuntu

To enable HTTP/2 in Nginx on an [Ubuntu VPS](https://www.rosehosting.com/ubuntu-hosting.html) you should edit the default Nginx server block. We will use `nano`

but you can use a text editor of your choice.

sudo nano /etc/nginx/sites-available/default

Add the following server block:

server { server_name domain.com www.domain.com; listen 443 ssl http2 default_server; root /var/www/html; index index.html; location / { try_files $uri $uri/ =404; } ssl_certificate /etc/nginx/ssl/domain.com.crt; ssl_certificate_key /etc/nginx/ssl/domain.com.key; } server { listen 80; server_name domain.com www.domain.com; return 301 https://$server_name$request_uri; }

Make sure that `domain.com`

is replaced with your real domain name. Additionally, the document root and the path to the SSL certificate and key should be correctly set.

Once you finish with editing the server block, save and close the file. Check if there are errors in the Nginx configuration using the command:

sudo nginx -t

And then restart Nginx for the changes to take effect

sudo systemctl restart nginx.service

If you’d like to enable HTTP/2 for another domain name, you can check our blog post on [how to set up Nginx server blocks on Ubuntu and CentOS](https://www.rosehosting.com/blog/how-to-set-up-nginx-server-blocks-on-ubuntu-and-centos/).

## Enable HTTP/2 in Nginx on CentOS

To enable HTTP/2 on a [CentOS VPS](https://www.rosehosting.com/centos-hosting.html) you need to follow the exact same steps as on Ubuntu. The location of the Nginx block file is the only difference. To edit the default Nginx server block on CentOS you should look into the `/etc/nginx/conf.d`

directory.

# nano /etc/nginx/conf.d/default.conf

Once again, check if there are errors with the configuration, save and close the file, then restart the Nginx service using the command below:

# systemctl restart nginx.service


To check whether HTTP/2 is enabled in Nginx you can use some [online HTTP/2 checker tool](https://www.rosehosting.com/network-tools/http2-support.html).

Of course you don’t have to enable HTTP/2 in Nginx on Ubuntu and CentOS, if you use one of our [Nginx VPS hosting](https://www.rosehosting.com/nginx-hosting.html) services, in which case you can simply ask our expert Linux admins to to enable HTTP/2 in Nginx on Ubuntu and CentOS, on your server. They are available 24×7 and will take care of your request immediately.

PS. If you liked this post, on how to enable HTTP/2 in Nginx on Ubuntu and CentOS, please share it with your friends on the social networks using the buttons below or simply leave a comment. Thanks.

Hello,

Thanks for sharing such an amazing and helpful post. HTTP/2 is more essential for every site.

ReplyThanks for the tip.

ReplyHow can I enable on all sites at one?