引言
隱藏版本號資訊
-
開啟Nginx配置檔案:通常位於/etc/nginx/nginx.conf或/usr/local/nginx/conf/nginx.conf,具體位置取決於你的安裝方式。
-
編輯配置檔案:找到http塊,並在其中新增server_tokens off;指令。如果你有多個server塊,也可以在每個server塊中單獨設定這個選項,以確保它在整個伺服器範圍內生效。
-
儲存並退出編輯器:完成修改後儲存更改。
-
檢查配置語法:使用命令nginx -t來測試配置檔案是否有語法錯誤。
-
重新載入Nginx:如果配置檔案沒有問題,使用命令nginx -s reload來應用新的配置。
# nginx.conf 或者某個特定的 server 配置檔案
http {
# 其他配置...
# 關閉版本資訊顯示
server_tokens off;
# 更多其他配置...
server {
listen
80
;
server_name example.com;
# 在 server 塊內也可以設定 server_tokens
# server_tokens off;
location / {
root /
var
/www/html;
index index.html index.htm;
}
}
}
限制訪問敏感目錄
server {
listen
80
;
server_name example.com;
# 根目錄設定
root /
var
/www/html;
# 禁止訪問.git目錄
location ~ /\.git {
deny all;
}
# 禁止訪問.htaccess檔案
location ~ /\.ht {
deny all;
}
# 其他location配置...
}
server {
listen
80
;
server_name example.com;
# 根目錄設定
root /
var
/www/html;
# 定義自定義
403
錯誤頁面
error_page
403
/custom_403.html;
# 禁止訪問.git目錄
location ~ /\.git {
deny all;
}
# 禁止訪問.htaccess檔案
location ~ /\.ht {
deny all;
}
# 自定義
403
錯誤頁面的位置
location = /custom_403.html {
allow all;
root /usr/share/nginx/html;
}
}
配置錯誤頁面
server {
listen
80
;
server_name example.com;
# 根目錄設定
root /
var
/www/html;
index index.html index.htm;
# 隱藏
Nginx
版本資訊
server_tokens off;
# 定義自定義錯誤頁面
error_page
404
/custom_404.html;
error_page
500502503504
/custom_50x.html;
# 自定義
404
錯誤頁面的位置
location = /custom_404.html {
root /usr/share/nginx/html;
internal;
}
# 自定義50x錯誤頁面的位置
location = /custom_50x.html {
root /usr/share/nginx/html;
internal;
}
# 其他location配置...
}
<!
DOCTYPE
html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Page Not Found</title>
</head>
<body>
<h1>Oops! Page not found.</h1>
<p>We're sorry, but the page you were looking for doesn't exist.</p>
<form action="/search" method="get">
<input type="text" name="query" placeholder="Search...">
<button type="submit">Search</button>
</form>
<a href="/">Go back to homepage</a>
</body>
</html>
<!
DOCTYPE
html>
<
htmllang
=
"en"
>
<
head
>
<
metacharset
=
"UTF-8"
>
<
title
>Server Error</
title
>
</
head
>
<
body
>
<
h1
>Something went wrong!</
h1
>
<
p
>Our team has been notified and is working on fixing the issue as quickly as possible.</
p
>
<
ahref
=
"/"
>Return to homepage</
a
>
<
p
>Contact us if you need further assistance.</
p
>
</
body
>
</
html
>
啟用HTTPS
openssl req -
new
-newkey
rsa
:
2048
-nodes -out yourdomain.csr -keyout yourdomain.key
server {
listen
80
;
server_name yourdomain.com www.yourdomain.com;
# 將
HTTP
請求重定向到
HTTPS
return301https
:
//$host$request_uri;
}
server {
listen
443
ssl;
server_name yourdomain.com www.yourdomain.com;
ssl_certificate /path/to/yourdomain.crt;
ssl_certificate_key /path/to/yourdomain.key;
ssl_trusted_certificate /path/to/intermediate.crt;
ssl_protocols
TLSv1.2TLSv1.3
;
ssl_prefer_server_ciphers on;
ssl_ciphers
'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH'
;
location / {
# 你的網站根目錄和其他配置
root /
var
/www/html;
index index.html index.htm;
}
}
應用內容安全策略(CSP)
server {
listen
80
;
server_name example.com;
# 其他配置...
add_header
Content
-
Security
-
Policy"default-src 'self'; img-src *; script-src 'self' 'unsafe-inline' 'unsafe-eval'"
;
# 其他location配置...
}
-
default-src 'self':預設情況下,只允許從當前域名載入資源。
-
img-src *:允許圖片從任何源載入。
-
script-src 'self' 'unsafe-inline' 'unsafe-eval':允許指令碼從當前域名載入,並允許內聯指令碼和eval()函式。然而,在實際生產環境中應儘量避免使用unsafe-inline和unsafe-eval,因為它們會增加XSS攻擊的風險。
add_header
Content
-
Security
-
Policy"default-src 'self'; script-src 'self' https://trustedscripts.example.com; style-src 'self' https://trustedstyles.example.com; img-src 'self' data: https://trustedimages.example.com; connect-src 'self' https://api.example.com; object-src 'none'; frame-src 'none';"
;
-
只允許從當前域載入所有型別的內容(default-src 'self')。
-
指令碼只能從當前域和一個可信的第三方指令碼提供者載入(script-src 'self' https://trustedscripts.example.com )。
-
樣式表也受到類似的限制(style-src 'self' https://trustedstyles.example.com)。
-
圖片可以從當前域、data URI以及一個可信的圖片伺服器載入(img-src 'self' data: https://trustedimages.example.com)。
-
API請求只能傳送到當前域或一個可信的API端點(connect-src 'self' https://api.example.com)。
-
禁止載入外掛內容(object-src 'none')以減少潛在的安全風險。
-
不允許頁面被嵌入到任何iframe中,防止點選劫持攻擊(frame-src 'none')。
add_header
Content
-
Security
-
Policy"default-src 'self'; report-uri /csp-report-endpoint"
;
設定正確的檔案許可權
-
檔案許可權:通常推薦將檔案許可權設為644,這意味著所有者可以讀寫(rw-),而組使用者和其他人只能讀取(r–)。
-
目錄許可權:目錄許可權通常設為755,允許所有者讀寫執行(rwx),組使用者和其他人只能讀取和執行(r-x)。
-
所有者和組:所有者應設定為執行Nginx服務的非root使用者,例如nginx或www-data,具體取決於你的系統配置。
/
var
/www/html/ # 網站根目錄
/etc/nginx/nginx.conf #
Nginx
主配置檔案
/
var
/log/nginx/access.log # 訪問日誌檔案
# 設定檔案許可權為
644
sudo chmod
644
/
var
/www/html/index.html
sudo chmod
644
/etc/nginx/nginx.conf
sudo chmod
644
/
var
/log/nginx/access.log
# 設定目錄許可權為
755
sudo chmod
755
/
var
/www/html/
# 假設nginx執行使用者為nginx,組也為nginx
sudo chown
nginx
:nginx /
var
/www/html/index.html
sudo chown
root
:nginx /etc/nginx/nginx.conf # 主配置檔案可能由root擁有,但屬於nginx組
sudo chown
nginx
:nginx /
var
/log/nginx/access.log
grep
'user'
/etc/nginx/nginx.conf
# 設定
WordPress
安裝目錄下的檔案和目錄許可權
find /
var
/www/html -type f -exec chmod
644
{
} \;
find /
var
/www/html -type d -exec chmod
755
{
} \;
# 更改所有權
sudo chown -R
nginx
:nginx /
var
/www/html
# 設定
Nginx
日誌檔案許可權
sudo chmod
640
/
var
/log/nginx
/*.log
sudo chown nginx:nginx /var/log/nginx/*.log
配置安全Headers
# 防止點選劫持
add_header X-
Frame
-
OptionsSAMEORIGIN
;
add_header
Content
-
Security
-
Policy"frame-ancestors 'self';"
;
# 強化
XSS
防護
# 注意:根據實際情況決定是否保留X-
XSS
-
Protection
,因為現代瀏覽器支援程度不同
add_header X-
XSS
-
Protection"1; mode=block"
;
# 防止
MIME
型別混淆攻擊
add_header X-
Content
-
Type
-
Options
nosniff;
# 增強隱私保護
add_header
Referrer
-
Policy"strict-origin-when-cross-origin"
;
# 強化的
CSP
策略
add_header
Content
-
Security
-
Policy"default-src 'self'; script-src 'self'; object-src 'none'; frame-ancestors 'self'; upgrade-insecure-requests;"
;
限制連線數
http {
# 定義一個名為addr的共享記憶體區域,大小為10MB,鍵值為$binary_remote_addr
limit_conn_zone $binary_remote_addr zone=
addr
:10m;
server {
location / {
# 每個
IP
地址最多允許
10
個併發連線
limit_conn addr
10
;
}
}
}
http {
# 定義一個名為req_zone的共享記憶體區域,大小為10MB,鍵值為$binary_remote_addr,速率限制為每秒
20
次請求
limit_req_zone $binary_remote_addr zone=
req_zone
:10m rate=20r/s;
server {
location / {
# 應用名為req_zone的限制規則,允許突發請求量為
5
limit_req zone=req_zone burst=
5
nodelay;
}
}
}
配置白名單
server {
listen
80
;
server_name yourdomain.com;
location /admin/ {
allow
192.168.1.1
; # 允許的
IP
地址
deny all; # 拒絕所有其他
IP
地址
proxy_pass
http
:
//backend;
}
}
最佳化SSL配置
server {
listen
443
ssl http2;
ssl_protocols
TLSv1.2TLSv1.3
;
# 其他配置...
}
server {
listen
443
ssl http2;
ssl_protocols
TLSv1.2TLSv1.3
;
ssl_ciphers
'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256'
;
ssl_prefer_server_ciphers on;
# 其他配置...
}
server {
listen
443
ssl http2;
ssl_stapling on;
ssl_stapling_verify on;
resolver
8.8.8.88.8.4.4
valid=300s;
resolver_timeout 10s;
# 其他配置...
}
server {
listen
443
ssl http2;
add_header
Strict
-
Transport
-
Security"max-age=31536000; includeSubDomains"
always;
# 其他配置...
}
server {
listen
443
ssl http2;
ssl_session_tickets on;
# 其他配置...
}
檔案上傳安全
http {
# 其他配置...
client_max_body_size 10m; # 設定最大上傳檔案大小為10MB
server {
listen
80
;
server_name example.com;
location /upload {
# 將請求轉發給後端處理檔案上傳的應用伺服器
proxy_pass
http
:
//backend_server;
# 確保這裡也設定了client_max_body_size以覆蓋預設值
client_max_body_size 10m;
}
# 其他配置...
}
}
server {
listen
80
;
server_name example.com;
location /uploads {
alias /path/to/uploads; # 指定上傳目錄的實際路徑
# 防止任何
PHP
或類似指令碼被執行
location ~* \.(php|pl|py|jsp|asp|sh|cgi)$ {
deny all; # 對匹配這些副檔名的檔案返回
403Forbidden
}
}
}
防止常見攻擊
防止DDoS攻擊
http {
# 設定請求速率限制
limit_req_zone $binary_remote_addr zone=
one
:10m rate=30r/m;
server {
location /login.html {
# 應用請求速率限制
limit_req zone=one;
}
# 設定併發連線數限制
limit_conn_zone $binary_remote_addr zone=
addr
:10m;
location /shopping/ {
limit_conn addr
10
; # 每個
IP
最多允許
10
個併發連線
}
# 關閉慢連線
client_body_timeout 5s;
client_header_timeout 5s;
}
}
防止SQL注入
location / {
# 檢查
URL
中是否包含特殊字元
# 如果包含分號、單引號、尖括號等字元,返回
444
狀態碼
#
444
是
Nginx
特殊狀態碼,表示關閉連線而不傳送響應頭
if
($request_uri ~* [;
'<>] ) {
return 444;
}
# 檢查查詢字串中的特殊字元
if ($args ~* [;'
<>] ) {
return444
; }
# 保護敏感
URI
location ~*
/(admin|backup|config|db|src)/
{
deny all;
}}
防止跨站指令碼攻擊(XSS)
add_header X-
XSS
-
Protection"1; mode=block"
;
點選劫持(Clickjacking)防禦
add_header X-
Frame
-
OptionsSAMEORIGIN
;
防止目錄遍歷
server {
listen
80
;
server_name example.com;
location / {
autoindex off; # 確保目錄瀏覽被關閉
root /
var
/www/html;
}
}
location ~
/\.\./
{
deny all; # 拒絕所有包含“../”的請求
}
location /
static
/ {
# 注意這裡的斜槓
alias /
var
/www/static_files/; # 確保這裡也有斜槓
}
location /admin/ {
allow
192.168.1.100
; # 允許特定
IP
訪問
deny all; # 拒絕其他所有
IP
訪問
}
if
($uri ~*
"\.\."
) {
return403
; # 如果
URI
包含“..”,返回
403Forbidden
}
location /
static
/ {
alias /
var
/www/static_files/;
autoindex off; # 確保目錄瀏覽被關閉
}
location /admin/ {
allow
192.168.1.100
; # 只允許特定
IP
訪問
deny all; # 拒絕其他所有
IP
訪問
}
server {
listen
80
;
server_name yoursite.com;
# 拒絕不含斜槓的父級目錄嘗試
location ~
/\.\./
{
deny all;
}
# 正常的站點配置...
}
日誌安全
http {
# 定義一個名為
'main'
的日誌格式,包含多種有用的資訊
log_format main
'$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" '
'"$http_x_forwarded_for" "$msec" '
'"$connection" "$connection_requests" '
'"$upstream_addr" "$upstream_response_time" '
'"$request_time" "$gzip_ratio"'
;
# 指定使用
'main'
格式記錄訪問日誌到特定位置
access_log /
var
/log/nginx/access.log main buffer=32k flush=1m;
server {
listen
80
;
server_name example.com;
location / {
root /
var
/www/html;
index index.html;
}
}
}
http {
# 設定全域性錯誤日誌級別為warn
error_log /
var
/log/nginx/error.log warn;
server {
listen
80
;
server_name example.com;
location / {
root /
var
/www/html;
index index.html;
}
# 可以為特定虛擬主機設定不同的錯誤日誌級別
error_log /
var
/log/nginx/example.error.log error;
}
}
其他安全措施
禁止執行指令碼
server {
listen
80
;
server_name example.com;
location /uploads/ {
# 禁止訪問任何php指令碼
location ~* \.php$ {
deny all;
}
# 或者使用以下方式直接返回
403
錯誤給所有嘗試執行php指令碼的請求
# location ~* \.php$ {
#
return403
;
# }
# 其他靜態資源處理規則...
}
# 其他server配置...
}
location ~
/(uploads)/
.*\.(php|php5)$ {
deny all; # 返回
403Forbidden
}
配置超時時間
-
client_body_timeout: 設定客戶端與伺服器建立連線後傳送request body的超時時間。
-
client_header_timeout: 設定客戶端向伺服器傳送一個完整的request header的超時時間。
-
send_timeout: 設定服務端向客戶端傳輸資料的超時時間。
-
keepalive_timeout: 設定每個TCP連線最多可以保持多長時間。
-
client_max_body_size: 限制客戶端能夠上傳的最大檔案大小。
-
limit_rate: 限制連線速率,防止客戶端過快地使用頻寬。
http {
# 設定客戶端主體讀取超時時間為
10
秒
client_body_timeout 10s;
# 設定客戶端頭部讀取超時時間為
10
秒
client_header_timeout 10s;
# 設定傳送給客戶端的資料超時時間為
10
秒
send_timeout 10s;
# 設定
Keep
-
Alive
連線的超時時間為
60
秒
keepalive_timeout 60s;
# 設定允許客戶端上傳的最大檔案大小為1M
client_max_body_size 1M;
# 限制每秒傳輸的資料量為100KB
limit_rate 100k;
server {
listen
80
;
server_name example.com;
location / {
# 其他配置...
}
}
}