Nginx 是一款高性能的 Web 服务器和反向代理服务器,广泛应用于网站托管、负载均衡、安全防护等场景。当然对于刚接触 Nginx 的运维人员来说,掌握一些实用技巧可以大大能提升工作效率。这里给大家分享几个非常实用的 Nginx 配置技巧,适合小白快速上手。
1. 快速搭建静态网站
Nginx 最基础的功能是托管静态资源(如 HTML、图片、CSS、JS 文件)。通过简单的配置,你就可以让 Nginx 成为一个轻量级的网站服务器。
配置示例:
server { listen 80; server_name yourdomain.com; # 替换为你的域名或IP location / { root /var/www/html; # 网站文件存放路径 index index.html; # 默认首页文件 } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; # 错误页面路径 }}
server {
listen 80;
server_name yourdomain.com; # 替换为你的域名或IP
location / {
root /var/www/html; # 网站文件存放路径
index index.html; # 默认首页文件
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html; # 错误页面路径
}
}
操作步骤:
图片
- 首先将网站文件(如 index.html)放在 /var/www/html 目录下。
- 然后修改 server_name 为你的域名或 IP 地址。
- 保存配置文件后,先执行 nginx -t 检查语法是否正确。
- 最后执行 systemctl reload nginx 重新加载nginx配置。
- 在浏览器中访问 http://yourdomain.com 如果配置正确的话就可以正常访问静态网站了。
说明:
如果遇到 403 Forbidden 错误,需要检查文件权限是否正确(Nginx 进程需有读取权限)。
也可以直接使用使用shell命令 curl http://localhost 可以快速验证本地配置是否生效。
2. 反向代理使用
Nginx 常被用作反向代理,将客户端请求转发到后端应用服务器(如 Java、Python 服务),从而实现隐藏后端架构并提升性能的目的。
配置示例:
server { listen 80; server_name api.yourdomain.com; location /api { proxy_pass http://backend-server:8080; # 后端服务地址 proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }}
server {
listen 80;
server_name api.yourdomain.com;
location /api {
proxy_pass http://backend-server:8080; # 后端服务地址
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
操作步骤:
图片
1)将 api.yourdomain.com 替换为你的实际域名。
2)确保后端服务(如 http://backend-server:8080)正常运行。
3)保存配置后重新加载 Nginx。
4)访问 http://api.yourdomain.com/api,请求会自动转发到后端服务。
说明:
- proxy_pass 支持直接写 IP 或域名,建议使用域名便于后续扩展。
- 如果后端服务需要 HTTPS,可以在 proxy_pass 中指定 https://。
3. 负载均衡:让多台服务器共同分担压力
当网站流量增大时,单台服务器可能无法承载访问压力。Nginx 可以通过负载均衡将请求分发到多台后端服务器,提升系统整体性能。
配置示例:
upstream backend { server 192.168.1.10 weight=3; # 权重高的服务器处理更多请求 server 192.168.1.20; least_conn; # 使用最少连接数算法}server { listen 80; server_name yourdomain.com; location / { proxy_pass http://backend; }}
upstream backend {
server 192.168.1.10 weight=3; # 权重高的服务器处理更多请求
server 192.168.1.20;
least_conn; # 使用最少连接数算法
}
server {
listen 80;
server_name yourdomain.com;
location / {
proxy_pass http://backend;
}
}
操作步骤:
确保后端服务器(如 192.168.1.10 和 192.168.1.20)已部署服务。
保存配置后重新加载 Nginx。
访问 http://yourdomain.com,请求会自动分配到后端服务器。
说明:
- least_conn 算法适合后端服务器性能差异较大的场景。
- 如果某台服务器故障,Nginx 会自动跳过该服务节点。
4. 配置 SSL/TLS 实现 HTTPS
HTTPS 是绝大多数网站安全的标配。Nginx 可以轻松配置 SSL 证书,给网站启用加密传输。
配置示例:
server { listen 443 ssl; server_name secure.yourdomain.com; ssl_certificate /etc/nginx/ssl/yourdomain.crt; # SSL 证书路径 ssl_certificate_key /etc/nginx/ssl/yourdomain.key; # 私钥路径 location / { root /var/www/secure; index index.html; }}
server {
listen 443 ssl;
server_name secure.yourdomain.com;
ssl_certificate /etc/nginx/ssl/yourdomain.crt; # SSL 证书路径
ssl_certificate_key /etc/nginx/ssl/yourdomain.key; # 私钥路径
location / {
root /var/www/secure;
index index.html;
}
}
操作步骤:
图片
- 申请 SSL 证书(如 Let's Encrypt 免费证书)。
- 将证书文件(.crt 和 .key)上传到 /etc/nginx/ssl。
- 修改配置文件中的证书路径。
- 保存配置后重新加载 Nginx。
- 浏览器访问 https://secure.yourdomain.com,确认 HTTPS 生效。
说明:
使用 openssl 工具可以验证证书是否有效(如 openssl x509 -in yourdomain.crt -text)。
如果证书过期,记得及时更新并重新加载 Nginx。
5. 日常维护:日志管理和性能优化
Nginx 的日志(access.log 和 error.log)是排查问题的关键。此外,通过调整参数可以进一步优化性能。
常用命令:
图片
查看访问日志:tail -f /var/log/nginx/access.log
查看错误日志:tail -f /var/log/nginx/error.log
重新加载配置:nginx -s reload
强制重启 Nginx:nginx -s stop && nginx
性能优化建议:
调整 worker_processes 和 worker_connections 参数以匹配服务器硬件。
启用 Gzip 压缩减少传输数据量:
gzip on;gzip_types text/plain text/css application/json;
gzip on;
gzip_types text/plain text/css application/json;
6、nginx常用的命令大全
命令 | 说明 | 示例 |
nginx -t | 检查配置文件语法是否正确 | sudo nginx -t |
nginx -s reload | 重新加载配置文件(无需重启服务) | sudo nginx -s reload |
nginx -s stop | 强制停止 Nginx 服务 | sudo nginx -s stop |
nginx -s quit | 平滑退出 Nginx 服务 | sudo nginx -s quit |
nginx -v | 查看 Nginx 版本信息 | nginx -v |
nginx -V | 查看 Nginx 编译参数(含模块信息) | nginx -V |
systemctl start nginx | 启动 Nginx 服务(Systemd 系统) | sudo systemctl start nginx |
systemctl restart nginx | 重启 Nginx 服务 | sudo systemctl restart nginx |
systemctl status nginx | 查看 Nginx 服务状态 | sudo systemctl status nginx |
tail -f /var/log/nginx/access.log | 实时查看访问日志 | tail -f /var/log/nginx/access.log |
tail -f /var/log/nginx/error.log | 实时查看错误日志 | tail -f /var/log/nginx/error.log |
curl http://localhost | 测试本地 Nginx 是否正常运行 | curl http://localhost |
curl -I http://yourdomain.com | 查看 HTTP 响应头(验证配置) | curl -I http://yourdomain.com |
gzip on; | 在配置文件中启用 Gzip 压缩 | 在 http 或 server 块中添加 |
proxy_pass http://backend-server; | 配置反向代理到后端服务 | 在 location 块中使用 |
upstream backend { server 192.168.1.10; } | 定义负载均衡的后端服务器组 | 在 nginx.conf 中定义 |
location /api { proxy_pass http://backend; } | 将 /api 路径代理到后端服务 | 在 server 块中配置 |
ssl_certificate /path/to/cert.pem; | 配置 SSL 证书路径 | 在 server 块中使用 |
ssl_certificate_key /path/to/privkey.pem; | 配置 SSL 私钥路径 | 在 server 块中使用 |
7、总结
通过以上技巧,即使是刚接触 Nginx 的小白,也能快速搭建起一个高效、安全的 Web 服务。