备忘录-web服务(Nginx/Apache/Tomcat)
Nginx 生产级实践
1. 基础安装与配置
# Ubuntu/Debian
sudo apt-get install nginx
# CentOS/RHEL
sudo yum install epel-release
sudo yum install nginx
# 启动服务
sudo systemctl start nginx
sudo systemctl enable nginx
2. 最小化安全配置
# /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
events {
worker_connections 1024;
use epoll;
}
http {
server_tokens off;# 隐藏版本号
add_header X-Content-Type-Options "nosniff";
add_header X-Frame-Options "SAMEORIGIN";
}
3. 静态站点托管
# /etc/nginx/conf.d/static_site.conf
server {
listen 80;
server_name example.com;
root /var/www/html;
index index.html;
location / {
try_files $uri $uri/ =404;
}
# 禁止访问隐藏文件
location ~ /\. {
deny all;
}
}
4. SSL/TLS 配置(Let's Encrypt)
# 安装 Certbot
sudo apt-get install certbot python3-certbot-nginx
# 获取证书
sudo certbot --nginx -d example.com
# 自动续期验证
sudo certbot renew --dry-run
Nginx SSL 优化配置:
server {
listen 443 ssl http2;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
}
5. 反向代理 Tomcat
# /etc/nginx/conf.d/tomcat_proxy.conf
upstream tomcat_cluster {
server 127.0.0.1:8080 weight=3;# 主节点
server 192.168.1.101:8080; # 备用节点
keepalive 32;
}
server {
listen 80;
server_name api.example.com;
location / {
proxy_pass http://tomcat_cluster;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# 长连接优化
proxy_http_version 1.1;
proxy_set_header Connection "";
}
}
6. 负载均衡与缓存
# 动态内容负载均衡
upstream backend {
least_conn;
server 192.168.1.100:8000;
server 192.168.1.101:8000;
}
# 静态资源缓存
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=static_cache:10m inactive=60m;
server {
location /static/ {
proxy_cache static_cache;
proxy_pass http://backend;
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m;
}
}
三、Apache 辅助配置
1. 与 Nginx 协作(动静分离)
# Nginx 处理静态文件
location ~* \.(jpg|css|js)$ {
root /var/www/static;
expires 30d;
}
# 动态请求转发到 Apache
location / {
proxy_pass http://127.0.0.1:8080;
include proxy_params;
}
Apache 配置:
# /etc/apache2/ports.conf
Listen 127.0.0.1:8080
# 虚拟主机配置
<VirtualHost 127.0.0.1:8080>
DocumentRoot /var/www/dynamic
<Directory /var/www/dynamic>
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
四、Tomcat 生产级调优
1. 连接器优化
<!-- conf/server.xml -->
<Connector port="8080" protocol="HTTP/1.1"
maxThreads="200"# 最大工作线程
minSpareThreads="25"# 最小空闲线程
acceptCount="100" # 等待队列长度
compression="on"# 启用压缩
compressionMinSize="2048"
compressableMimeType="text/html,text/xml,text/css"/>
2. JVM 内存优化
# bin/setenv.sh
export JAVA_OPTS="-Xms512m -Xmx2048m -XX:+UseG1GC -XX:MaxGCPauseMillis=200"
3. Nginx + Tomcat 会话保持
# 基于 Cookie 的会话黏滞
upstream tomcat {
ip_hash;# 或使用 sticky 模块
server 192.168.1.100:8080;
server 192.168.1.101:8080;
}
五、安全加固指南
1. 防止 DDoS 攻击
# 限制连接频率
limit_req_zone $binary_remote_addr zone=one:10m rate=10r/s;
location / {
limit_req zone=one burst=20;
}
2. Web 应用防火墙(ModSecurity)
# 编译时添加 ModSecurity 模块
load_module modules/ngx_http_modsecurity_module.so;
location / {
ModSecurityEnabled on;
ModSecurityConfig modsecurity.conf;
}
3. 文件权限控制
# Nginx 用户隔离
sudo chown -R nginx:nginx /var/www
sudo find /var/www -type d -exec chmod 755 {} \;
sudo find /var/www -type f -exec chmod 644 {} \;
六、性能监控与调优
1. 实时状态监控
# 启用 Stub Status 模块
location /nginx_status {
stub_status;
allow 127.0.0.1;
deny all;
}
输出示例:
Active connections: 3
server accepts handled requests
12 12 24
Reading: 0 Writing: 1 Waiting: 2
2. Prometheus 监控体系
# nginx-exporter 配置
scrape_configs:
- job_name: 'nginx'
static_configs:
- targets: ['nginx-server:9113']
七、高可用架构
1. Keepalived 双机热备
# Keepalived 配置
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
virtual_ipaddress {
192.168.1.200
}
}
2. 云原生部署(Docker)
FROM nginx:1.21-alpine
COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY ssl /etc/nginx/ssl
EXPOSE 80 443
八、故障排查工具箱
1. 日志分析
# 实时错误日志
tail -f /var/log/nginx/error.log
# 请求追踪
tcpdump -i eth0 port 80 -w nginx_capture.pcap
2. 性能瓶颈定位
# 查看工作进程状态
top -p $(pgrep -d',' nginx)
# 连接数统计
ss -ant | grep :80 | awk '{print $6}' | sort | uniq -c
总结
通过以上配置示例,可实现以下生产场景:
- Nginx 核心服务:静态资源托管、SSL 卸载、负载均衡
- Apache 辅助角色:动态内容处理(如 PHP)
- Tomcat 集成:Java 应用反向代理与集群管理
关键配置要点:
- 安全:隐藏版本号、SSL 强化、访问控制
- 性能:连接池优化、缓存策略、动静分离
- 可观测性:状态监控、日志分析、Prometheus 集成
百万并发场景调优指南
一、核心配置
1. 内存与线程配置
确保服务器有足够的内存和线程来处理高并发请求。
# 配置文件
server {
listen 80;
server_name example.com;
# 增加内存
mem_max_size 1G;
# 增加线程
server_threads 200;
serverI 200;
}
2. HTML 合并与文件合并
使用 html merger 和 sendfile 优化资源加载。
# 配置文件
server {
listen 80;
server_name example.com;
html merger on;
sendfile on;
}
3. 启用 html merger 和 sendfile
配置 html merger 和 sendfile 插件以加速资源加载。
# 配置文件
html merger on;
sendfile on;
html merger: {
directory /var/www/html;
include merge-angle=1;
include merge-file=on;
}
sendfile: {
directory /var/www/html;
protocol=HTTP/1.1;
}
4. 启用 fastcgi 插件
使用 fastcgi 提供更快的HTTP处理。
# 配置文件
fastcgi on;
fastcgi version=2;
fastcgi protocol=HTTP/1.1;
fastcgi host 0.0.0.0:80;
fastcgi offline on;
fastcgi permanently off;
fastcgi merge on;
二、安全配置
1. 启用 SSL
为服务器配置SSL,确保数据传输安全。
# 配置文件
ssl on;
ssl_certificate /etc/letsencrypt/live/example.com/ssl cert.pem;
ssl_key /etc/letsencrypt/live/example.com/keys/strawberry.key;
# 配置文件
ssl_root /var/www/html;
2. 配置SSR(Site-to-Site Routing)
启用SSR,限制请求源IP和端口。
# 配置文件
ssr on;
ssr protocol=HTTP/1.1;
ssr host-only on;
ssr www-only on;
ssr referrer-only on;
ssr allow-ssb on;
ssr rewrite-strategy "lib/direct/1,1,1,1,1,1,1";
3. 启用防DDoS
配置modsec过滤恶意请求。
# 配置文件
modsec on;
modsec rule file /etc/logrotate/shadow.log;
modsec deny all;
modsec allow 80;
modsec allow 443;
modsec require www.*;
modsec require *.com;
modsec require *.com/atpanel;
modsec require *.com/irc;
modsec require *.com/yahoo;
modsec require *.com/dropbox;
modsec require *.com/structr
三、性能优化
1. 启用 vhost
为每个虚拟服务器启用 vhost,以提高资源利用率。
# 配置文件
vhost example.com:
server_name example.com;
document_root /var/www/html;
allow 200-299;
include /etc/nginx.conf;
vhost example.com:8080:
server_name example.com:8080;
document_root /var/www/html;
allow 200-299;
include /etc/nginx.conf;
2. 配置 html merger 和 html cache
优化HTML资源的加载和缓存。
html merger on;
html merger: {
directory /var/www/html;
include merge-angle=1;
include merge-file=on;
}
html cache on;
html cache: {
directory /var/www/html;
cache_time 300;
cache_max_size 1M;
}
3. 启用 html cache
配置HTML缓存,减少资源加载时间。
html cache on;
html cache: {
directory /var/www/html;
cache_time 300;
cache_max_size 1M;
}
四、监控与排查
1. 使用 Prometheus 和 ELK
部署 Prometheus 和 ELK 系统进行性能监控。
# 安装和配置 Prometheus
sudo apt-get install -y -t e2c4b2f-8b38-1e2e-2b1b-91020b17-1e8
sudo systemctl enable prometheus
# 配置 Prometheus
prometheus hosts 127.0.0.1:8080 prometheus
prometheus enable-ssr
prometheus enable-sssi
prometheus enable-sssi-sssi
prometheus enable-sssi-sssi-sssi
prometheus enable-sssi-sssi-sssi-sssi
prometheus enable-sssi-sssi-sssi-sssi-sssi
2. 配置 ELK 系统
部署 ELK(Elasticsearch + Logrotate + Kibana)进行日志分析。
# 安装和配置 ELK
sudo apt-get install -y elasticsearch elasticsearch-contrib-dev elogrotate kibana
sudo systemctl enable elasticsearch elasticsearch-contrib-dev elogrotate kibana
# 配置 Elasticsearch
elasticsearch hosts 127.0.0.1:5120 -s -m
# 配置 Logrotate
logrotate hosts 127.0.0.1:8080 logrotate logrotate
logrotate logrotate logrotate logrotate logrotate logrotate logrotate logrotate
3. 日志排查工具
使用 tail -f /var/log/nginx/error.log 和 tail -f /var/log/nginx/access.log 进行日志分析。
五、最佳实践
-
定期备份配置文件
cp /etc/nginx/conf.d/nginx.conf /etc/nginx/conf.d/ mv /etc/nginx/conf.d/nginx.conf /etc/nginx/conf -
监控资源使用情况
df -h free -h iostat -h -
使用性能监控工具
ntpmon -c /var/log/nginx/access.log -
定期清理缓存
# 配置文件 cache clear cache clear 1M cache clear 1M 1M -
监控连接池压力
# 配置文件 serverI 200; serverI 200; serverI 200; -
使用多线程处理
# 配置文件 serverI 200; serverI 200; serverI 200;

浙公网安备 33010602011771号