server_tokens off;
if ($request_method !~ ^(GET|HEAD|POST)$ ) {
return 405;
}
more_clear_headers 'Server';
more_clear_headers 'X-Powered-By';
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305';
add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload";
ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 5s;
location /admin {
allow 192.168.1.0/24;
deny all;
}
limit_req_zone $binary_remote_addr zone=one:10m rate=10r/s;
server {
limit_req zone=one burst=20 nodelay;
}
limit_conn_zone $binary_remote_addr zone=addr:10m;
server {
limit_conn addr 10;
}
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
gzip_min_length 1000;
gzip_proxied any;
gzip_comp_level 6;
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m inactive=60m use_temp_path=off;
server {
location / {
proxy_cache my_cache;
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m;
}
}
worker_rlimit_nofile 65535;
events {
worker_connections 8192;
multi_accept on;
use epoll;
}
log_format security '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" '
'$request_time $upstream_response_time';
access_log /var/log/nginx/access.log security;
error_log /var/log/nginx/error.log warn;
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
load_module modules/ngx_http_modsecurity_module.so;
http {
modsecurity on;
modsecurity_rules_file /etc/nginx/modsec/main.conf;
}
load_module modules/ngx_http_naxsi_module.so;
http {
include /etc/nginx/naxsi_core.rules;
location / {
SecRulesEnabled;
DeniedUrl "/RequestDenied";
}
location /RequestDenied {
internal;
return 403;
}
}
# /etc/sysctl.conf
net.core.somaxconn = 65535
net.ipv4.tcp_max_syn_backlog = 65535
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 30
# /etc/security/limits.conf
* soft nofile 65535
* hard nofile 65535
nginx soft nofile 65535
nginx hard nofile 65535
通过实施以上优化措施,可以显著提升Nginx服务的安全性和性能,同时保持服务的稳定性和可靠性。