在CentOS系统中使用Nginx作为Web服务器是许多开发者和系统管理员的首选,因其高性能、稳定性和丰富的功能而备受青睐,本文将详细介绍在CentOS上安装、配置、优化及管理Nginx的完整流程,帮助用户快速上手并充分发挥其优势。

安装Nginx前的准备工作
在安装Nginx之前,确保系统已更新至最新状态,并安装必要的编译工具和依赖库,执行以下命令更新系统并安装基础工具:
sudo yum update -y sudo yum groupinstall "Development Tools" -y sudo yum install pcre-devel zlib-devel openssl-devel wget -y
这些依赖包是编译安装Nginx所必需的,其中PCRE库用于支持重写模块,zlib库用于启用gzip压缩功能,OpenSSL库则用于支持HTTPS协议。
编译安装Nginx
虽然CentOS的官方软件源中包含Nginx,但编译安装可以自定义功能并获取最新版本,首先从Nginx官网下载稳定版源码包,解压并进入目录:
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 --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-pcre
配置完成后执行编译和安装:
make sudo make install
安装完成后,Nginx默认位于/usr/local/nginx目录,可执行文件为sbin/nginx。
配置Nginx服务
创建系统服务
为方便管理Nginx,建议将其注册为系统服务,创建服务文件/usr/lib/systemd/system/nginx.service如下:

[Unit] Description=The NGINX HTTP and reverse proxy server After=network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=/usr/local/nginx/logs/nginx.pid ExecStartPre=/usr/local/nginx/sbin/nginx -t ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/usr/local/nginx/sbin/nginx -s stop PrivateTmp=true [Install] WantedBy=multi-user.target
启用并启动服务:
sudo systemctl enable nginx sudo systemctl start nginx
修改核心配置文件
Nginx的主配置文件为/usr/local/nginx/conf/nginx.conf,可通过调整以下参数优化性能:
worker_processes:建议设置为CPU核心数,如worker_processes auto;worker_connections:单进程最大连接数,如worker_connections 1024;keepalive_timeout:长连接超时时间,默认为75秒,可根据需求调整
配置虚拟主机时,可在http块中添加server段,
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;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
配置完成后,使用nginx -t检查语法并重载配置:
sudo nginx -t sudo nginx -s reload
配置防火墙与SELinux
确保防火墙允许HTTP和HTTPS流量:
sudo firewall-cmd --permanent --add-service=http sudo firewall-cmd --permanent --add-service=https sudo firewall-cmd --reload
若使用SELinux,需安装相应策略包:
sudo yum install policycoreutils-python -y sudo semanage port -a -t http_port_t -p tcp 80 sudo semanage port -a -t http_port_t -p tcp 443
优化与安全加固
性能优化
- 启用Gzip压缩:在
http块中添加以下配置:gzip on; gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
- 隐藏版本号:在
http块中添加server_tokens off;
安全加固
- 限制访问IP:通过
allow和deny指令控制访问权限 - 更改默认端口:避免使用80和443等常见端口
- 配置SSL证书:使用Let's Encrypt免费证书实现HTTPS
日志管理与维护
Nginx的默认日志位于/usr/local/nginx/logs/目录,可通过access_log和error_log指令自定义日志路径和格式,建议定期使用logrotate工具轮转日志,避免日志文件过大。

相关问答FAQs
Q1: 如何解决Nginx启动后无法访问的问题?
A: 首先检查防火墙和SELinux设置,确保端口未被阻止,其次使用nginx -t命令验证配置文件语法是否正确,若仍无法访问,检查/var/log/nginx/error.log中的错误信息,常见原因包括监听端口冲突、网站目录权限不足或域名解析错误。
Q2: 如何在Nginx中配置反向代理?
A: 在server块中添加location段,使用proxy_pass指令指向后端服务器,将所有请求代理到本地8080端口:
location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
同时确保后端服务正常运行,并配置适当的超时参数(如proxy_connect_timeout)以优化代理性能。