Nginx(发音为"engine x")是一款高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器。它以高并发、低内存占用和模块化架构著称,广泛用于负载均衡、Web服务和反向代理等场景。
sudo apt update
sudo apt install nginx
sudo systemctl start nginx
sudo systemctl enable nginx
sudo yum install epel-release
sudo yum install nginx
sudo systemctl start nginx
sudo systemctl enable nginx
nginx -v # 查看版本
curl -I 127.0.0.1 # 测试服务是否运行
/etc/nginx/nginx.conf
(Linux)或conf/nginx.conf
(Windows)/etc/nginx/conf.d/
或/etc/nginx/sites-available/
目录user www-data;
worker_processes auto;
pid /run/nginx.pid;
events {
worker_connections 1024;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
gzip on;
gzip_disable "msie6";
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
access_log /var/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log;
}
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d example.com -d www.example.com
server {
listen 443 ssl;
server_name example.com www.example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
root /var/www/example.com;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
}
server {
listen 80;
server_name app.example.com;
location / {
proxy_pass http://localhost: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;
}
}
upstream backend {
server backend1.example.com weight=5;
server backend2.example.com;
server backend3.example.com;
# 负载均衡策略
# least_conn; # 最少连接
# ip_hash; # IP哈希
}
server {
listen 80;
server_name loadbalancer.example.com;
location / {
proxy_pass http://backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
server {
# ...其他配置...
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 30d;
add_header Cache-Control "public, no-transform";
}
}
worker_processes auto; # 自动设置为CPU核心数
worker_rlimit_nofile 100000; # 每个worker能打开的文件描述符数量
events {
worker_connections 4096; # 每个worker的最大连接数
multi_accept on; # 一次接受多个新连接
use epoll; # Linux系统使用epoll事件模型
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 30;
keepalive_requests 100000;
reset_timedout_connection on;
client_body_timeout 10;
send_timeout 2;
}
server {
# 禁用server tokens
server_tokens off;
# 防止点击劫持
add_header X-Frame-Options "SAMEORIGIN";
# XSS保护
add_header X-XSS-Protection "1; mode=block";
# 内容安全策略
add_header Content-Security-Policy "default-src 'self'";
# 禁止不安全的HTTP方法
if ($request_method !~ ^(GET|HEAD|POST)$ ) {
return 405;
}
}
# 限制连接频率
limit_req_zone $binary_remote_addr zone=one:10m rate=10r/s;
server {
location /login {
limit_req zone=one burst=20;
}
}
# 限制并发连接数
limit_conn_zone $binary_remote_addr zone=addr:10m;
server {
location /download {
limit_conn addr 10;
}
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
}
创建/etc/logrotate.d/nginx
文件:
/var/log/nginx/*.log {
daily
missingok
rotate 14
compress
delaycompress
notifempty
create 0640 www-data adm
sharedscripts
postrotate
[ -f /var/run/nginx.pid ] && kill -USR1 `cat /var/run/nginx.pid`
endscript
}
# 启动Nginx
sudo systemctl start nginx
# 停止Nginx
sudo systemctl stop nginx
# 重启Nginx
sudo systemctl restart nginx
# 重新加载配置(不中断服务)
sudo systemctl reload nginx
# 查看状态
sudo systemctl status nginx
sudo nginx -t # 测试配置文件语法
端口冲突:
sudo netstat -tulnp | grep :80
权限问题:
502 Bad Gateway:
403 Forbidden:
# 查看实时错误日志
sudo tail -f /var/log/nginx/error.log
# 查找常见错误
grep -E "50[0-9]|40[0-9]" /var/log/nginx/error.log
Nginx是一个功能强大且灵活的Web服务器,通过合理的配置可以满足各种高性能Web服务的需求。建议在生产环境部署前充分测试配置,并定期检查服务器日志和性能指标以确保稳定运行。