关于 nginx
nginx 是用于配置和运维 Nginx Web 服务器的技能,面向管理网站交付与代理服务的人员。内容涵盖服务控制、反向代理、负载均衡、性能设置及故障排查,并附有配置示例。
主要功能
- 01管理并测试 Nginx 服务
- 02配置静态站点和 HTTPS
- 03设置反向代理和 WebSocket 代理
- 04配置负载均衡策略
- 05调整工作进程与 gzip 压缩
- 06排查错误和性能问题
适用场景
- 01通过 HTTPS 托管静态网站
- 02将 API 或 WebSocket 流量代理到后端
- 03在多个后端服务器间分配请求
- 04限制 API 请求速率或管理区访问
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ chaterm/ terminal-skills --skill nginx安装后在 Claude Code 中输入/nginx确认已加载。
详细介绍
Nginx 配置与优化
概述
Nginx Web 服务器配置、反向代理、负载均衡、性能优化等技能。
基本管理
服务控制
# Start/Stop services
systemctl start nginx
systemctl stop nginx
systemctl restart nginx
systemctl reload nginx # Graceful reload config
# Configuration test
nginx -t
nginx -T # Test and print config
配置文件
# Main configuration file
/etc/nginx/nginx.conf
# Site configuration
/etc/nginx/conf.d/*.conf
/etc/nginx/sites-available/
/etc/nginx/sites-enabled/
# Log files
/var/log/nginx/access.log
/var/log/nginx/error.log
基本配置
静态网站
server {
listen 80;
server_name example.com www.example.com;
root /var/www/html;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
# Static resource caching
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 30d;
add_header Cache-Control "public, immutable";
}
}
HTTPS 配置
server {
listen 443 ssl http2;
server_name example.com;
ssl_certificate /etc/nginx/ssl/cert.pem;
ssl_certificate_key /etc/nginx/ssl/key.pem;
# SSL optimization
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256;
ssl_prefer_server_ciphers off;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 1d;
# HSTS
add_header Strict-Transport-Security "max-age=31536000" always;
}
# HTTP redirect to HTTPS
server {
listen 80;
server_name example.com;
return 301 https://$server_name$request_uri;
}
反向代理
基本代理
server {
listen 80;
server_name api.example.com;
location / {
proxy_pass http://127.0.0.1:3000;
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_set_header X-Forwarded-Proto $scheme;
}
}
WebSocket 代理
location /ws {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_read_timeout 86400;
}
负载均衡
基本配置
upstream backend {
server 192.168.1.10:8080 weight=3;
server 192.168.1.11:8080 weight=2;
server 192.168.1.12:8080 backup;
}
server {
listen 80;
location / {
proxy_pass http://backend;
proxy_next_upstream error timeout http_500;
}
}
负载均衡策略
# Round Robin (default)
upstream backend {
server 192.168.1.10:8080;
server 192.168.1.11:8080;
}
# IP Hash (session persistence)
upstream backend {
ip_hash;
server 192.168.1.10:8080;
server 192.168.1.11:8080;
}
# Least Connections
upstream backend {
least_conn;
server 192.168.1.10:8080;
server 192.168.1.11:8080;
}
# Health Check
upstream backend {
server 192.168.1.10:8080 max_fails=3 fail_timeout=30s;
server 192.168.1.11:8080 max_fails=3 fail_timeout=30s;
}
性能优化
基本优化
# nginx.conf
worker_processes auto;
worker_rlimit_nofile 65535;
events {
worker_connections 65535;
use epoll;
multi_accept on;
}
http {
# File transfer optimization
sendfile on;
tcp_nopush on;
tcp_nodelay on;
# Timeout settings
keepalive_timeout 65;
client_body_timeout 12;
client_header_timeout 12;
send_timeout 10;
# Buffer settings
client_body_buffer_size 10K;
client_header_buffer_size 1k;
client_max_body_size 8m;
large_client_header_buffers 4 32k;
}
Gzip 压缩
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_min_length 1000;
gzip_types text/plain text/css text/xml application/json application/javascript application/xml;
常见场景
场景 1:PHP-FPM 配置
server {
listen 80;
server_name example.com;
root /var/www/html;
index index.php index.html;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
场景 2:速率限制
# Define rate limit zone
limit_req_zone $binary_remote_addr zone=api:10m rate=10r/s;
server {
location /api/ {
limit_req zone=api burst=20 nodelay;
proxy_pass http://backend;
}
}
场景 3:访问控制
location /admin {
allow 192.168.1.0/24;
deny all;
auth_basic "Admin Area";
auth_basic_user_file /etc/nginx/.htpasswd;
}
故障排查
| 问题 | 解决方案 |
|---|---|
| 配置错误 | 使用 nginx -t 测试配置 |
| 502 Bad Gateway | 检查后端服务和 upstream 配置 |
| 504 Gateway Timeout | 增加 proxy_read_timeout |
| 权限问题 | 检查文件权限和 SELinux |
| 性能问题 | 检查 worker_connections,并分析日志 |
评论
支持 Markdown