帮助中心

DNS污染处理

常见问题

香港高防IP

常见问题

新加坡高防服务器

产品简介 操作指南 常见问题

新加坡云服务器

产品简介 操作指南 常见问题

香港服务器租用

产品简介 操作指南 常见问题

香港高防服务器

产品简介 操作指南 常见问题

域名服务

域名服务

Ubuntu 16.04 中配置和使用 NGINX Web 服务器

时间 : 2022-07-22 16:18:31
编辑 : 华纳云

  ginx专为性能优化而开发,其最知名的优点是它的稳定性和低系统资源消耗,以及对并发连接的高处理能力(单台物理服务器可支持30000~50000个并发连接), 是一个高性能的 HTTP 和反向代理服务器,也是一个IMAP/POP3/SMTP 代理服,本篇文章为大家讲解一下Ubuntu 16.04 中配置和使用 NGINX Web 服务器

  安装 Certbot

  第一步是安装 certbot,该软件客户端可以几乎自动化所有的过程。 Certbot 开发人员维护自己的 Ubuntu 仓库,其中包含比 Ubuntu 仓库中存在的软件更新的软件。

  添加 Certbot 仓库:

  # add-apt-repository ppa:certbot/certbot

  接下来,更新 APT 源列表:

  # apt-get update

  此时,可以使用以下 apt 命令安装 certbot:

  # apt-get install certbot

  Certbot 现已安装并可使用。

  获得证书

  有各种 Certbot 插件可用于获取 SSL 证书。这些插件有助于获取证书,而证书的安装和 Web 服务器配置都留给管理员。

  我们使用一个名为 Webroot 的插件来获取 SSL 证书。

  在有能力修改正在提供的内容的情况下,建议使用此插件。在证书颁发过程中不需要停止 Web 服务器。

  配置 NGINX

  Webroot 会在 Web 根目录下的 .well-known 目录中为每个域创建一个临时文件。在我们的例子中,Web 根目录是 /var/www/html。确保该目录在 Let’s Encrypt 验证时可访问。为此,请编辑 NGINX 配置。使用文本编辑器打开 /etc/nginx/sites-available/default:

  # $EDITOR /etc/nginx/sites-available/default

  在该文件中,在 server 块内,输入以下内容:

  location ~ /.well-known {

  allow all;

  }

  保存,退出并检查 NGINX 配置:

  # nginx -t

  没有错误的话应该会显示如下:

  nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

  nginx: configuration file /etc/nginx/nginx.conf test is successful

  重启 NGINX:

  # systemctl restart nginx

  使用 Certbot 获取证书

  下一步是使用 Certbot 的 Webroot 插件获取新证书。在本教程中,我们将保护示例域 www.example.com。需要指定应由证书保护的每个域。执行以下命令:

  # certbot certonly --webroot --webroot-path=/var/www/html -d www.example.com

  在此过程中,Cerbot 将询问有效的电子邮件地址,用于进行通知。还会要求与 EFF 分享,但这不是必需的。在同意服务条款之后,它将获得一个新的证书。

  最后,目录 /etc/letsencrypt/archive 将包含以下文件:

  chain.pem:Let’s Encrypt 加密链证书。

  cert.pem:域名证书。

  fullchain.pem:cert.pem和 chain.pem 的组合。

  privkey.pem:证书的私钥。

  Certbot 还将创建符号链接到 /etc/letsencrypt/live/domain_name/ 中的最新证书文件。这是我们将在服务器配置中使用的路径。

  在 NGINX 上配置 SSL/TLS

  下一步是服务器配置。在 /etc/nginx/snippets/ 中创建一个新的代码段。 snippet 是指一段配置,可以包含在虚拟主机配置文件中。如下创建一个新的文件:

  # $EDITOR /etc/nginx/snippets/secure-example.conf

  该文件的内容将指定证书和密钥位置。粘贴以下内容:

  ssl_certificate /etc/letsencrypt/live/domain_name/fullchain.pem;

  ssl_certificate_key /etc/letsencrypt/live/domain_name/privkey.pem;

  在我们的例子中,domain_name 是 example.com。

  编辑 NGINX 配置

  编辑默认虚拟主机文件:

  # $EDITOR /etc/nginx/sites-available/default

  如下:

  server {

  listen 80 default_server;

  listen [::]:80 default_server;

  server_name www.example.com

  return 301 https://$server_name$request_uri;

  # SSL configuration#

  listen 443 ssl default_server;

  listen [::]:443 ssl default_server;

  include snippets/secure-example.conf

  ## Note: You should disable gzip for SSL traffic.# See: https://bugs.debian.org/773332# ...

  }

  这将启用 NGINX 加密功能。

  保存、退出并检查 NGINX 配置文件:

  # nginx -t

  nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

  nginx: configuration file /etc/nginx/nginx.conf test is successful

  重启 NGINX:

  # systemctl restart nginx


客服咨询
7*24小时技术支持
Telegram
hncloudnoc

技术支持

渠道支持