在开始Nginx的源码编译安装之前,我们需要做好充分的环境准备工作。对于大多数Linux发行版来说,编译Nginx需要一些基础的开发工具和依赖库。以openEuler 22.03为例,以下是必须安装的依赖项:
bash复制yum install -y gcc make pcre-devel zlib-devel openssl-devel
这些依赖包的作用分别是:
提示:如果你使用的是其他Linux发行版,如Ubuntu,对应的包管理命令应为
apt-get install build-essential libpcre3-dev zlib1g-dev libssl-dev
获取Nginx源码有两种推荐方式:
bash复制wget http://nginx.org/download/nginx-1.20.1.tar.gz
建议总是使用稳定版(Stable version)而非开发版(Mainline version)用于生产环境。截至本文撰写时,1.20.x系列是最新的稳定分支。
合理的目录结构规划是系统管理的重要基础。按照Linux的目录规范,我们通常将第三方软件安装在/usr/local下:
bash复制mkdir -p /usr/local/nginx
cd /usr/local/nginx
tar -zxvf nginx-1.20.1.tar.gz
这种布局的优势在于:
进入解压后的源码目录,执行configure脚本进行编译配置:
bash复制cd nginx-1.20.1
./configure --prefix=/usr/local/nginx --with-http_ssl_module
这里的关键参数说明:
--prefix:指定安装路径,所有生成的文件都将安装在此目录下--with-http_ssl_module:启用HTTPS支持模块,这是配置SSL/TLS的基础实际生产环境中,你可能还需要添加以下常用模块:
bash复制./configure \
--prefix=/usr/local/nginx \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-stream
注意事项:如果在configure阶段出现依赖缺失错误,请根据报错信息安装对应的开发包。常见的错误包括"the HTTP rewrite module requires the PCRE library"等。
配置完成后,执行编译和安装:
bash复制make && make install
这个过程可能会花费几分钟时间,具体取决于服务器性能。make命令会:
编译完成后,你应该在/usr/local/nginx目录下看到以下结构:
code复制├── conf/ # 配置文件目录
├── html/ # 默认网页目录
├── logs/ # 日志文件目录
└── sbin/ # 可执行程序目录
Nginx的主配置文件nginx.conf采用模块化结构,主要包含以下几个部分:
nginx复制# 全局块
user nobody;
worker_processes auto;
error_log logs/error.log warn;
# events块
events {
worker_connections 1024;
use epoll;
}
# http块
http {
include mime.types;
default_type application/octet-stream;
# server块
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html;
}
}
}
生产环境推荐配置调整:
现代网站必须使用HTTPS,下面是一个完整的HTTPS代理配置示例:
nginx复制server {
listen 443 ssl;
server_name example.com;
ssl_certificate /path/to/cert.pem;
ssl_certificate_key /path/to/key.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384';
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
location / {
proxy_pass http://backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
关键安全配置说明:
建议采用以下目录结构管理配置:
code复制/usr/local/nginx/
├── conf/
│ ├── nginx.conf # 主配置
│ ├── conf.d/ # 自定义配置
│ │ ├── app1.conf
│ │ └── app2.conf
│ └── snippets/ # 可复用配置片段
│ ├── ssl.conf
│ └── security.conf
在nginx.conf中通过include指令加载:
nginx复制http {
include conf.d/*.conf;
include snippets/ssl.conf;
}
这种结构的好处是:
Nginx提供了一系列管理命令:
bash复制# 测试配置
/usr/local/nginx/sbin/nginx -t
# 启动
/usr/local/nginx/sbin/nginx
# 重载配置(不中断服务)
/usr/local/nginx/sbin/nginx -s reload
# 优雅停止
/usr/local/nginx/sbin/nginx -s quit
对于现代Linux系统,建议使用systemd管理Nginx服务。创建/etc/systemd/system/nginx.service文件:
ini复制[Unit]
Description=Nginx Web Server
After=network.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 quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
然后执行:
bash复制systemctl daemon-reload
systemctl enable nginx
systemctl start nginx
Nginx默认生成两种日志:
推荐配置日志轮转(logrotate),创建/etc/logrotate.d/nginx:
bash复制/usr/local/nginx/logs/*.log {
daily
missingok
rotate 30
compress
delaycompress
notifempty
create 640 root adm
sharedscripts
postrotate
[ -f /usr/local/nginx/logs/nginx.pid ] && kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
endscript
}
对于监控,可以使用ngxtop实时查看访问情况:
bash复制ngxtop -l /usr/local/nginx/logs/access.log
如果启动时报错"Address already in use",可能是端口被占用:
bash复制netstat -tulnp | grep :80
lsof -i :443
解决方案:
bash复制setcap 'cap_net_bind_service=+ep' /usr/local/nginx/sbin/nginx
常见的权限相关错误:
解决方案:
bash复制chown -R nobody:nobody /usr/local/nginx/html
chmod -R 755 /usr/local/nginx
# 如果使用SELinux
setsebool -P httpd_can_network_connect 1
SSL证书配置不当会导致HTTPS无法工作,常见问题:
验证工具:
bash复制openssl s_client -connect example.com:443 -servername example.com
对于高流量网站,建议调整以下参数:
nginx复制events {
worker_connections 4096;
multi_accept on;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 30;
keepalive_requests 100;
gzip on;
gzip_types text/plain text/css application/json;
}
这些配置可以显著提高吞吐量,减少资源消耗。