Nginx作为当前最主流的高性能Web服务器之一,其轻量级、高并发的特性使其在互联网基础设施中占据重要地位。我第一次在生产环境部署Nginx是在2015年,当时是为了解决Apache服务器在突发流量下的性能瓶颈问题。经过这些年的实践,我总结出一套适合不同场景的Nginx部署方法论。
在开始安装前,需要明确几个关键选择:
重要提示:生产环境务必避免使用root用户直接运行Nginx服务,这涉及到基础的安全规范。
以CentOS 7为例的完整编译流程:
bash复制# 安装编译依赖
yum install -y gcc make pcre-devel zlib-devel openssl-devel
# 下载稳定版源码包
wget http://nginx.org/download/nginx-1.24.0.tar.gz
tar zxvf nginx-1.24.0.tar.gz
cd nginx-1.24.0
# 配置编译参数(典型生产环境配置)
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_gzip_static_module
# 编译安装
make && make install
关键参数解析:
--with-http_ssl_module:启用HTTPS支持--with-http_realip_module:获取客户端真实IP(常用于反向代理场景)--with-threads:启用线程池提升性能(需Nginx 1.7.11+)虽然Nginx在Windows上的性能不如Linux,但开发测试环境仍可选用:
start nginx.exe常见问题:Windows下可能出现80端口被占用(通常被IIS或SQL Server占用),可通过
netstat -ano查找并终止占用进程。
nginx.conf采用模块化结构,主要包含以下上下文块:
nginx复制# 全局块(影响整个Nginx运行的配置)
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
# events块(网络连接配置)
events {
worker_connections 1024;
use epoll; # Linux高性能网络模式
}
# http块(核心功能配置)
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
# 服务器块(虚拟主机配置)
server {
listen 80;
server_name example.com;
# 位置块(URI路由规则)
location / {
root /usr/share/nginx/html;
index index.html;
}
}
}
worker_processes:
nproc查询)worker_connections:
gzip压缩配置:
nginx复制gzip on;
gzip_min_length 1k; # 大于1KB的文件才压缩
gzip_comp_level 6; # 压缩级别(1-9)
gzip_types text/plain application/javascript text/css;
生产环境推荐的基础安全配置:
nginx复制server {
listen 80 default_server;
server_name _;
return 444; # 丢弃非法域名请求
# 隐藏版本信息
server_tokens off;
# 禁用不安全的HTTP方法
if ($request_method !~ ^(GET|HEAD|POST)$ ) {
return 405;
}
}
upstream模块的典型配置:
nginx复制upstream backend {
least_conn; # 最少连接算法
server 192.168.1.101:8080 weight=5;
server 192.168.1.102:8080;
server backup.example.com:8080 backup;
}
server {
location / {
proxy_pass http://backend;
proxy_set_header Host $host;
}
}
健康检查配置补充:
nginx复制# 需要nginx-plus或第三方模块
health_check interval=5s fails=3 passes=2 uri=/health;
静态资源缓存配置示例:
nginx复制location ~* \.(jpg|png|css|js)$ {
expires 30d;
add_header Cache-Control "public, no-transform";
access_log off;
}
代理缓存配置:
nginx复制proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=mycache:10m inactive=60m;
server {
location / {
proxy_cache mycache;
proxy_cache_valid 200 302 10m;
proxy_cache_use_stale error timeout updating;
}
}
错误日志分级:
日志格式自定义:
nginx复制log_format main '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent"';
关键监控项:
netstat -anp | grep nginx | wc -lngx_http_stub_status_moduleps -o rss,command -p $(pgrep -f "nginx: master")status模块启用方法:
nginx复制location /nginx_status {
stub_status;
allow 127.0.0.1;
deny all;
}
问题1:502 Bad Gateway
nginx复制proxy_connect_timeout 60s;
proxy_read_timeout 60s;
问题2:地址重写循环
nginx复制# 错误示例
rewrite ^/(.*)$ /$1 permanent;
# 正确写法
rewrite ^/oldpath$ /newpath permanent;
经过多年运维实践,我发现Nginx的配置灵活性既是优势也是挑战。建议每次修改配置后执行nginx -t测试语法,重大变更前备份配置文件。对于复杂场景,可以考虑使用include指令拆分配置文件,例如将不同域名的配置存放在/etc/nginx/conf.d/目录下