Nginx Web伺服器最佳安全實踐


NGINX 是一款免費的、開源的、高效能的 HTTP 伺服器和反向代理,也稱為 IMAP/POP3 代理伺服器。NGINX 以其高效能、穩定性、豐富的功能集、簡單的配置和低資源消耗而聞名。在本文中,我們將解釋“Nginx Web伺服器最佳安全實踐”。

sysctl.conf 是一個簡單的檔案,包含由 sysctl 讀取和設定的 sysctl 值。要開啟 sysctl.conf,請使用以下命令:

$ sudo vim /etc/sysctl.conf

示例輸出應如下所示:

## /etc/sysctl.conf - Configuration file for setting system variables
# See /etc/sysctl.d/ for additional system variables.
# See sysctl.conf (5) for information.
#

#kernel.domainname = example.com

# Uncomment the following to stop low-level messages on console
#kernel.printk = 3 4 1 3

##############################################################3
# Functions previously found in netbase
#

# Uncomment the next two lines to enable Spoof protection (reverse-path filter)
# Turn on Source Address Verification in all interfaces to
# prevent some spoofing attacks
#net.ipv4.conf.default.rp_filter=1
..........................................

為防止 Smurf 攻擊,請將以下行新增到 sysctl.conf 檔案中。

net.ipv4.icmp_echo_ignore_broadcasts = 1

要開啟對錯誤 ICMP 訊息的保護,請將以下行新增到 sysctl.conf 檔案中。

net.ipv4.icmp_ignore_bogus_error_responses = 1

要開啟 SYN cookies 以保護 SYN 泛洪攻擊,請將以下行新增到 sysctl.conf 檔案中。

net.ipv4.tcp_syncookies = 1

要開啟並記錄偽造的、源路由的和重定向的資料包,請將以下行新增到 sysctl.conf 檔案中。

net.ipv4.conf.all.log_martians = 1
net.ipv4.conf.default.log_martians = 1

要取消源路由資料包,請將以下行新增到 sysctl.conf 檔案中。

net.ipv4.conf.all.accept_source_route = 0
net.ipv4.conf.default.accept_source_route = 0

要開啟反向路徑過濾,請將以下行新增到 sysctl.conf 檔案中。

net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.default.rp_filter = 1

要識別更改路由表,請將以下行新增到 sysctl.conf 檔案中。

net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.default.accept_redirects = 0
net.ipv4.conf.all.secure_redirects = 0
net.ipv4.conf.default.secure_redirects = 0

要開啟 execshild,請將以下行新增到 sysctl.conf 檔案中。

kernel.exec-shield = 1
kernel.randomize_va_space = 1

要調整 IPv6,請將以下行新增到 sysctl.conf 檔案中。

net.ipv6.conf.default.router_solicitations = 0
net.ipv6.conf.default.accept_ra_rtr_pref = 0
net.ipv6.conf.default.accept_ra_pinfo = 0
net.ipv6.conf.default.accept_ra_defrtr = 0
net.ipv6.conf.default.autoconf = 0
net.ipv6.conf.default.dad_transmits = 0
net.ipv6.conf.default.max_addresses = 1

要最佳化埠,請使用 LBs 並將以下行新增到 sysctl.conf 檔案中。

fs.file-max = 65535

要允許更多 PID,請將以下行新增到 sysctl.conf 檔案中。

kernel.pid_max = 65536

要增加系統 IP 埠限制,請將以下行新增到 sysctl.conf 檔案中。

net.ipv4.ip_local_port_range = 2000 65000

要增加 TCP 最大緩衝區大小,請使用 setsockopt() 設定表,並將以下行新增到 sysctl.conf 檔案中。

net.ipv4.tcp_rmem = 4096 87380 8388608
net.ipv4.tcp_wmem = 4096 87380 8388608

要儲存並重新載入上述檔案,請使用以下命令:

# sysctl -p

要關閉顯示的 nginx 版本號,請將以下行新增到 /etc/nginx/conf.d/default.conf 檔案中。

server_tokens off

要控制緩衝區溢位攻擊,請將以下命令新增到 /etc/nginx/nginx.conf 檔案中。

## Start: Size Limits & Buffer Overflows ##
client_body_buffer_size 1K;
client_header_buffer_size 1k;
client_max_body_size 1k;
large_client_header_buffers 2 1k;
## END: Size Limits & Buffer Overflows ##
  • client_body_buffer_size 1k - 此指令指定客戶端請求正文緩衝區大小。

  • client_header_buffer_size 1k - 此指令設定來自客戶端的請求標頭的標頭緩衝區大小。

  • client_max_body_size 1k - 它由標頭請求中的 Content-Length 行指示。

  • large_client_header_buffers 2 1k - 此指令分配讀取客戶端請求的大標頭的最大緩衝區數量和大小。

Nginx 和 PHP 安全提示

要在 php 中新增安全提示,它需要一個名為 php.ini 的檔案。php.ini 檔案的示例應如下所示:

[PHP]
;;;;;;;;;;;;;;;;;;;
; About php.ini ;
;;;;;;;;;;;;;;;;;;;
; PHP's initialization file, generally called php.ini, is responsible for
; configuring many of the aspects of PHP's behavior.

; PHP attempts to find and load this configuration from a number of locations.
; The following is a summary of its search order:
; 1. SAPI module specific location.
; 2. The PHPRC environment variable. (As of PHP 5.2.0)
; 3. A number of predefined registry keys on Windows (As of PHP 5.2.0)
; 4. Current working directory (except CLI)
; 5. The web server's directory (for SAPI modules), or directory of PHP
; (otherwise in Windows)
; 6. The directory from the --with-config-file-path compile time option, or the
; Windows directory (C:\windows or C:\winnt)
; See the PHP docs for more specific information.

要禁止 PHP 中的危險函式,請將以下命令新增到 php.ini 檔案中。

disable_functions = phpinfo, system, mail, exec

要設定每個指令碼的最大執行時間,請將以下命令新增到 php.ini 檔案中。

max_execution_time = 30

要設定每個指令碼可以花費解析請求資料的時間上限,請將以下命令新增到 php.ini 檔案中。

max_input_time = 60

要設定指令碼可以消耗的最大記憶體量,請將以下命令新增到 php.ini 檔案中。

memory_limit = 8M

要設定 PHP 將接受的 POST 資料的最大大小,請將以下命令新增到 php.ini 檔案中。

post_max_size = 8M

要設定上傳檔案的最大允許大小,請將以下命令新增到 php.ini 檔案中。

upload_max_filesize = 2M

不要將 PHP 錯誤訊息暴露給外部使用者,請將以下命令新增到 php.ini 檔案中。

display_errors = Off

要開啟安全模式,請將以下命令新增到 php.ini 檔案中。

safe_mode = On

要限制對 PHP 環境的外部訪問,請將以下命令新增到 php.ini 檔案中。

safe_mode_allowed_env_vars = PHP_

要檢視所有日誌錯誤,請將以下命令新增到 php.ini 檔案中。

log_errors = On

要設定最小允許的 PHP post 大小,請將以下命令新增到 php.ini 檔案中。

post_max_size = 1K

要啟用 SQL 安全模式,請將以下命令新增到 php.ini 檔案中。

sql.safe_mode = On

要避免開啟遠端檔案,請將以下命令新增到 php.ini 檔案中。

allow_url_fopen = Off

要升級 Nginx,請使用以下命令:

$ sudo apt-get upgrade nginx

示例輸出應如下所示:

Reading package lists... Done
Building dependency tree
Reading state information... Done
Calculating upgrade... Done
The following packages were automatically installed and are no longer required:
libhdb9-heimdal libkdc2-heimdal libntdb1 python-ntdb
Use 'apt-get autoremove' to remove them.
The following NEW packages will be installed:
nginx nginx-common nginx-core
0 upgraded, 3 newly installed, 0 to remove and 0 not upgraded.
Need to get 349 kB of archives.
After this operation, 1,297 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
.....................................................................

閱讀本文後,您將能夠了解什麼是 Nginx Web 伺服器以及如何保護 Nginx Web 伺服器。在我們的下一篇文章中,我們將提供更多基於 Linux 的技巧和提示。繼續閱讀!

更新於:2020年1月17日

302 次瀏覽

開啟您的職業生涯

完成課程後獲得認證

開始
廣告
© . All rights reserved.