在 Linux 系统上安装与配置 Nginx
本文档旨在指导用户在 Linux 环境下完成 Nginx 的下载、编译、安装、启动、停止及配置管理。
准备工作
下载 Nginx 源码包
访问 Nginx 官方下载页面获取最新稳定版源码包:Nginx Download。
解压源码包
将下载的源码包上传至服务器,并进行解压。假设源码包名为 nginx-1.27.2.tar.gz,并放置在 /data/soft 目录下:
cd /data/soft
tar -zxvf nginx-1.27.2.tar.gz
编译与安装
配置编译选项
进入解压后的源码目录,执行 configure 脚本以生成编译所需的 Makefile。在此示例中,我们将 Nginx 安装到 /data/soft/nginx 目录,并启用 stream 模块以支持 TCP/UDP 代理:
cd /data/soft/nginx-1.27.2
./configure --prefix=/data/soft/nginx --with-stream
编译环境问题排查
如果在执行 configure 时遇到 C compiler cc is not found 错误,表示缺少 C 语言编译器。请安装开发工具组:
sudo yum groupinstall "Development Tools" -y
执行编译与安装
在配置成功后,执行 make 命令进行编译,然后执行 make install 进行安装:
cd /data/soft/nginx-1.27.2
make
make install
编译安装错误排查
make: command not found:如果提示make命令未找到,请先安装make工具:yum install make -ymake: *** No rule to make target 'build'. Stop.:此错误通常表明缺少编译 Nginx 所需的依赖库。请安装以下开发库:yum install pcre-devel zlib-devel openssl-devel -y安装完依赖后,需要重新执行
configure、make和make install命令。
Nginx 服务管理
启动 Nginx
Nginx 的可执行文件位于安装目录下的 sbin 文件夹中。执行以下命令启动 Nginx:
cd /data/soft/nginx/sbin
./nginx
查看 Nginx 进程
使用 ps 命令可以查看 Nginx 的主进程 (master process) 和工作进程 (worker process):
ps -ef | grep nginx
示例输出:
root 117364 1 0 11:08 ? 00:00:00 nginx: master process ./nginx
nobody 117365 117364 0 11:08 ? 00:00:00 nginx: worker process
root 117369 106422 0 11:08 pts/0 00:00:00 grep --color=auto nginx
停止 Nginx
有两种常用的停止 Nginx 的方法:
- 平滑停止:发送
QUIT信号给主进程,允许当前正在处理的请求处理完毕后再退出。cd /data/soft/nginx/sbin ./nginx -s quit - 强制停止:直接通过进程 ID (PID) 终止 Nginx 进程。
ps -ef | grep nginx | awk '{print $2}' | xargs kill -9
配置管理
检查配置文件语法
在修改 Nginx 配置文件(通常位于 conf/nginx.conf)后,务必检查语法是否正确,以避免服务启动失败。使用 -t 参数进行测试:
vi /data/soft/nginx/conf/nginx.conf
/data/soft/nginx/sbin/nginx -t
如果配置文件语法无误,将看到类似以下的输出:
nginx: the configuration file /data/soft/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /data/soft/nginx/conf/nginx.conf test is successful
在线重新加载配置
当配置文件修改后,可以使用 reload 信号使 Nginx 在不中断服务的情况下重新加载配置:
/data/soft/nginx/sbin/nginx -s reload
配置样例
HTTP 端口映射 (反向代理)
此配置将 Nginx 作为反向代理,将 80 端口的 HTTP 请求转发给后端的多个服务器。
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream backend_servers {
ip_hash; # 使用 ip_hash 负载均衡算法,实现会话保持
server 192.168.1.1:80; # 后端服务器地址1
server 192.168.1.2:80; # 后端服务器地址2
}
server {
listen 80; # Nginx 监听的端口
server_name your_domain.com; # 访问域名
location / {
proxy_pass http://backend_servers/; # 将请求转发给 upstream
proxy_set_header Host $host:$server_port; # 传递原始 Host 头
client_max_body_size 100m; # 允许的最大客户端请求体大小
}
}
}
TCP/UDP 流处理 (Stream 模块)
使用 stream 模块可以实现 TCP/UDP 协议的代理。此示例将 3306 端口的数据库连接请求转发给后端数据库服务器。
stream {
upstream database_cluster {
server 192.168.1.3:3306 ; # 后端数据库服务器地址
}
server {
listen 3306 ; # Nginx 监听的端口
proxy_responses 1; # 确保在发送数据前收到响应
proxy_pass database_cluster; # 将连接转发给 upstream
}
}
文件目录映射 (静态文件服务)
配置 Nginx 直接提供静态文件的访问。此示例将 /images/ 和 /videos/ 路径下的请求映射到服务器的 /data/soft/files 目录下。
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
client_max_body_size 50m;
server {
listen 6868; # Nginx 监听的端口
server_name localhost;
location ~ /(images|videos)/ { # 匹配以 /images/ 或 /videos/ 开头的路径
root /data/soft/files; # 指定静态文件根目录
index index.html index.htm; # 默认查找的文件
}
}
}