Nginx 是全球发展势头最猛的开源轻量级高性能 web 服务器系统. Nginx 可在 Linux, Windows,Mac OS 和 Solaris 等操作系统上运行. Nginx 继续人气激增, 意味着越来越多的 Nginx 部署环境需要加以保护.
[51CTO.com 快译] Nginx 是全球发展势头最猛的开源轻量级高性能 Web 服务器系统. Nginx 可在 Linux, Windows,Mac OS 和 Solaris 等操作系统上运行. Nginx 继续人气激增, 意味着越来越多的 Nginx 部署环境需要加以保护.
我们在本教程中将介绍几个常见的 Nginx 服务器安全要点.
要求
运行 Ubuntu 18.04 或 Debian 9 的服务器
你的服务器上已设置了 root 密码
1. 安装 Nginx
首先要把 Nginx 安装到系统上. 运行下列命令来安装它:
apt-get install nginx -y
一旦 Nginx 安装完毕, 可以用下列命令检查 Nginx 的状态:
systemctl status nginx
你应该会看到下列输出:
- ?nginx.service - A high performance Web server and a reverse proxy server
- Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
- Active: active (running) since Sun 2019-03-10 02:43:14 UTC; 4min 40s ago
- Docs: man:nginx(8)
- Process: 2271 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited, status=0/SUCCESS)
- Process: 2281 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
- Process: 2274 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
- Main PID: 2285 (nginx)
- Tasks: 2 (limit: 1111)
- CGroup: /system.slice/nginx.service
- ??2285 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
- ??2290 nginx: worker process
- Mar 10 02:43:14 ubuntu1804 systemd[1]: Starting A high performance Web server and a reverse proxy server...
- Mar 10 02:43:14 ubuntu1804 systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
- Mar 10 02:43:14 ubuntu1804 systemd[1]: Started A high performance Web server and a reverse proxy server.
2. 更新 Nginx
你需要更新 Nginx Web 服务器系统, 因为许多性能上的改进, 新的功能和安全补丁在不断添加. 大多数现代 Linux 发行版不会在默认软件包列表中随带最新版的 Nginx. 所以你需要通过软件包管理器来更新最新版的 Nginx. 可以用下列命令来更新 Nginx Web 服务器系统:
- apt-get update -y
- apt-get install nginx --reinstall -y
3. 防止信息透露
首先需要防止 Nginx 透露其版本信息.
默认情况下, Nginx 在 HTTP 报头中显示其名称和版本.
你可以用下列命令来检查:
curl -I http://localhost
应该会看到下列输出:
- HTTP/1.1 200 OK
- Server: nginx/1.14.0 (Ubuntu)
- Date: Sat, 09 Mar 2019 15:28:01 GMT
- Content-Type: text/html
- Content-Length: 10918
- Last-Modified: Fri, 01 Feb 2019 16:05:17 GMT
- Connection: keep-alive
- ETag: "5c546e3d-2aa6"
- Accept-Ranges: bytes
在上述输出中, 应该会看到 Nginx 和操作系统的版本.
可以编辑 / etc/nginx/nginx.conf 文件, 隐藏这部分信息:
nano /etc/nginx/nginx.conf
在 http 配置部分里面添加 server_tokens off 这一行:
- http {
- ##
- # Basic Settings
- ##
- server_tokens off;
完成之后保存并关闭文件. 然后重启 Nginx Web 服务器系统使变更生效:
systemctl restart nginx
现在再次运行 curl 命令:
curl -I http://localhost
应该会看到下列输出:
- HTTP/1.1 200 OK
- Server: nginx
- Date: Sat, 09 Mar 2019 15:33:31 GMT
- Content-Type: text/HTML
- Content-Length: 10918
- Last-Modified: Fri, 01 Feb 2019 16:05:17 GMT
- Connection: keep-alive
- ETag: "5c546e3d-2aa6"
- Accept-Ranges: bytes
4. 限制 IP 被访问
Nginx 随带一个名为 ngx_http_access_module 的简单模块, 允许或拒绝某个特定的 IP 地址.
如果你想允许从 172.16.0.0/16 访问 Nginx, 但拒绝来自其他子网的访问, 那么打开 / etc/nginx/sites-enabled/default 文件:
nano /etc/nginx/sites-enabled/default
在 server block 里面进行下列更改:
- server {
- listen 80 default_server;
- listen [::]:80 default_server;
- allow 172.16.0.0/16;
- deny all;
完成后保存并关闭文件. 然后重启 Nginx 使这些变更生效:
systemctl restart nginx
现在, 试着从 192.168.0.102 之类的其他 IP 地址范围来访问 Nginx 服务器.
下一步, 用下列命令检查 Nginx 日志:
tail -f /var/log/nginx/error.log
你应该会在下列输出中看到访问被禁止:
2019/03/09 16:13:01 [error] 11589#11589: *1 access forbidden by rule, client: 192.168.0.102, server: _, request: "GET /test/ HTTP/1.1", host: "172.16.0.122"
5. 用 TLS 保护 Nginx
传输层安全 (TLS) 是安全套接层 (SSL) 的后续技术. 它提供更强大更高效的 HTTPS, 含有更多的改进, 比如 Forward Secrecy, 与现代的 OpenSSL 密码组以及 HSTS. 本教程介绍如何在 Nginx 中启用自签名的 SSL 证书. 如果你想改而使用 let's Encrypt 证书, 不妨参阅: https://www.howtoforge.com/tutorial/nginx-with-letsencrypt-ciphersuite/.
先用下列命令为 SSL 创建目录:
mkdir /etc/nginx/ssl/
接下来, 用下列命令创建密钥和证书:
cd /etc/nginx/ssl/
先用下列命令创建密钥:
openssl genrsa -aes256 -out nginx.key 1024
你会看到下列输出:
- Generating RSA private key, 1024 bit long modulus
- ...++++++
- .............................++++++
- e is 65537 (0x010001)
- Enter pass phrase for nginx.key:
- Verifying - Enter pass phrase for nginx.key:
接下来用下列命令创建 csr:
openssl req -new -key nginx.key -out nginx.csr
提供所有信息, 如下所示:
- Generating RSA private key, 1024 bit long modulus
- ...++++++
- .............................++++++
- e is 65537 (0x010001)
- Enter pass phrase for nginx.key:
- Verifying - Enter pass phrase for nginx.key:
- root@ubuntu1804:~# openssl req -new -key nginx.key -out nginx.csr
- Enter pass phrase for nginx.key:
- You are about to be asked to enter information that will be incorporated
- into your certificate request.
- What you are about to enter is what is called a Distinguished Name or a DN.
- There are quite a few fields but you can leave some blank
- For some fields there will be a default value,
- If you enter '.', the field will be left blank.
- -----
- Country Name (2 letter code) [AU]:IN
- State or Province Name (full name) [Some-State]:Gujarat
- Locality Name (eg, city) []:Junagadh
- Organization Name (eg, company) [Internet Widgits Pty Ltd]:IT
- Organizational Unit Name (eg, section) []:IT
- Common Name (e.g. server FQDN or YOUR name) []:HITESH
- Email Address []:admin@example.com
- Please enter the following 'extra' attributes
- to be sent with your certificate request
- A challenge password []:admin
- An optional company name []:IT
接下来用下列命令签名证书:
openssl x509 -req -days 365 -in nginx.csr -signkey nginx.key -out nginx.crt
你会看到下列输出:
- Signature ok
- subject=C = IN, ST = Gujarat, L = Junagadh, O = IT, OU = IT, CN = HITESH, emailAddress = admin@example.com
- Getting Private key
- Enter pass phrase for nginx.key:
接下来打开 Nginx 默认虚拟主机文件, 定义证书:
nano /etc/nginx/sites-enabled/default
进行下列更改:
- server {
- listen 192.168.0.100:443 ssl;
- root /var/www/HTML;
- index index.HTML index.htm index.nginx-debian.HTML;
- server_name _;
- ssl_certificate /etc/nginx/ssl/nginx.crt;
- ssl_certificate_key /etc/nginx/ssl/nginx.key;
- ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
完成后保存并关闭文件. 然后重启 Nginx 服务器使这些变更生效:
systemctl restart nginx
6. 用密码保护目录
创建 Nginx Web 服务器时, 还可以用密码来保护特定的目录. 可以使用. htpasswd 文件来做到这一点.
为此用下列命令创建 passwd 文件, 并为它添加用户:
- mkdir /etc/nginx/.htpasswd
- htpasswd -c /etc/nginx/.htpasswd/passwd admin
你会看到下列输出:
- New password:
- Re-type new password:
- Adding password for user admin
接下来用下列命令在 Nginx Web root 里面创建测试目录.
mkdir /var/www/HTML/test
接下来用下列命令为 www-data user 赋予所有权:
chown -R www-data:www-data /var/www/HTML/test
接下来用下列命令打开 Nginx 默认虚拟主机文件:
nano /etc/nginx/sites-enabled/default
接下来保护测试目录, 如下所示:
- location /test {
- auth_basic "Restricted";
- auth_basic_user_file /etc/nginx/.htpasswd/passwd;
完成后保存并关闭文件. 然后重启 Nginx 服务使这些变更生效:
systemctl restart nginx
接下来打开 Web 浏览器, 输入 URL http://your-server-ip/test. 系统会提示你输入用户名和密码, 访问测试目录, 如下所示:
恭喜你! 你已成功保护了 Ubuntu 18.04 服务器上的 Nginx 服务器系统. 但愿本文可保护你放在 Nginx Web 服务器上的应用程序. 想了解更多信息, 可以查阅 Nginx 安全文档(https://docs.nginx.com/nginx/admin-guide/security-controls/).
原文标题: Tips and Tricks to Secure Your Nginx Web Server, 作者: Hitesh Jethva
来源: http://server.51cto.com/sOS-593746.htm