Nginx 安装常见问题与排障方案

源码编译报错:PCRE library not found?

在执行 ./configure 时,提示 the HTTP rewrite module requires the PCRE library

解决方案:

这是因为缺少正规表达式库开发包。请根据您的系统安装:

# Ubuntu/Debian
sudo apt install libpcre3 libpcre3-dev -y

# CentOS/RHEL
sudo yum install pcre pcre-devel -y

启动失败:Address already in use?

启动 Nginx 时报错 bind() to 0.0.0.0:80 failed (98: Address already in use)

解决方案:

说明 80 端口已被其他程序(如 Apache 或已运行的 Nginx)占用。使用以下指令排查并关闭占用进程:

# 查找占用 80 端口的 PID
sudo lsof -i :80

# 或者使用
sudo netstat -tpln | grep :80

# 杀掉占用进程(替换 PID)
sudo kill -9 [PID]

配置 HTTPS 时提示 SSL 模块未开启?

在配置文件中写了 ssl onlisten 443 ssl,但报错 unknown directive "ssl"

原因与对策:

如果您是源码编译安装,说明在 ./configure 时没有添加 SSL 模块。您需要重新编译:

# 回到源码目录,重新配置并加入 ssl 模块
./configure --with-http_ssl_module [其他原有参数]
make
sudo make install

访问出现 403 Forbidden 错误?

Nginx 已启动,但访问网页显示 403。

解决方案:

1. 检查网站目录权限:确保 nginx 运行用户对网页目录有读取权限。

sudo chmod -R 755 /var/www/html

2. 检查 SELinux(仅限 CentOS):

# 临时关闭测试
sudo setenforce 0