Nginx(”engine x”)是一个高性能的 HTTP 和反向代理服务器,以轻量、稳定、高并发著称,是目前最主流的 Web 服务器之一。
本文将详细介绍在 Linux CentOS 平台下,通过源码编译的方式安装 Nginx,并完成反向代理配置、gzip 压缩开启的完整过程。
一、前置准备:安装编译环境
源码安装需要先安装编译工具(gcc、g++)和构建工具(make、automake 等),用于将源代码编译成可执行文件。
1. 安装基础编译工具
1 2 3 4 5
| yum install gcc gcc-c++ -y
yum -y install gcc automake autoconf libtool make
|
二、安装 Nginx 依赖库
Nginx 的部分功能依赖第三方库,需要提前安装:
- PCRE:Nginx 的 Rewrite 模块依赖,用于 URL 重写;
- zlib:用于 gzip 压缩,减少传输体积;
- OpenSSL:用于 HTTPS 支持(SSL 模块)。
1. 安装 PCRE
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| cd /usr/local/src
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.39.tar.gz
tar -zxvf pcre-8.39.tar.gz
cd pcre-8.39
./configure make make install
|
2. 安装 zlib
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| cd /usr/local/src
wget http://zlib.net/zlib-1.2.8.tar.gz
tar -zxvf zlib-1.2.8.tar.gz
cd zlib-1.2.8
./configure make make install
|
3. 安装 OpenSSL
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| cd /usr/local/src
wget http://www.openssl.org/source/openssl-1.0.1t.tar.gz
tar -zxvf openssl-1.0.1t.tar.gz
cd openssl-1.0.1t
./config shared --prefix=/usr/local --openssldir=/usr/local/ssl make depend make sudo make install
|
三、源码安装 Nginx
依赖安装完成后,开始编译安装 Nginx。
1. 下载并解压 Nginx 源码
1 2 3 4 5 6 7 8 9 10 11
| cd /usr/local/src
wget http://nginx.org/download/nginx-1.10.1.tar.gz
tar -zxvf nginx-1.10.1.tar.gz
cd nginx-1.10.1
|
2. 配置编译选项
通过 ./configure 指定安装路径、启用模块、关联依赖库路径:
1 2 3 4 5 6 7 8
| ./configure \ --sbin-path=/usr/local/nginx/sbin/nginx \ --conf-path=/usr/local/nginx/conf/nginx.conf \ --pid-path=/usr/local/nginx/nginx.pid \ --with-http_ssl_module \ --with-pcre=/usr/local/src/pcre-8.39 \ --with-zlib=/usr/local/src/zlib-1.2.8 \ --with-openssl=/usr/local/src/openssl-1.0.1t
|
3. 编译并安装
四、Nginx 基本操作
安装完成后,进入 Nginx 的 sbin 目录,执行启动、重启等操作。
1. 启动 Nginx
1 2 3 4
| cd /usr/local/nginx/sbin
./nginx
|
启动后,在浏览器访问服务器的公网 IP(默认 80 端口),如果看到 Nginx 欢迎页,说明安装成功。
2. 重启 Nginx(平滑重启,不中断服务)
修改配置文件后,必须执行此命令让配置生效。
3. 其他常用命令
1 2 3 4 5 6 7 8
| ./nginx -s stop
./nginx -s quit
./nginx -t
|
五、配置反向代理(以 Node.js 服务为例)
反向代理是 Nginx 最常用的功能之一,可以将请求转发到后端服务(如 Node.js、Java、Python 等)。
假设后端 Node.js 服务监听在 127.0.0.1:3000,我们需要将 Nginx 的 80 端口请求转发到该服务。
1. 修改 Nginx 配置文件
1 2 3 4
| cd /usr/local/nginx/conf
vim nginx.conf
|
2. 修改 server 配置块
找到 http 块下的 server 配置,修改为如下内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| server { listen 80; charset utf-8;
location / { proxy_pass http://127.0.0.1:3000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
|
3. 重启 Nginx 使配置生效
1 2
| cd /usr/local/nginx/sbin ./nginx -s reload
|
此时,访问服务器的 80 端口,请求就会被转发到后端的 Node.js 服务了。
六、开启 gzip 压缩
开启 gzip 压缩可以减少传输文件的体积,加快页面加载速度。
1. 修改 nginx.conf 的 http 块
在 http 配置块中添加或修改 gzip 相关配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| http {
gzip on; gzip_min_length 1k; gzip_buffers 16 64k; gzip_http_version 1.1; gzip_comp_level 6; gzip_types application/javascript text/javascript text/plain application/x-javascript text/css application/xml; gzip_vary on; }
|
2. 重启 Nginx 使配置生效
七、注意事项与后续
- 版本建议:本文示例使用的是较老的版本(Nginx 1.10.1、PCRE 8.39 等),生产环境建议使用最新稳定版,旧版本可能存在安全漏洞。
- 防火墙配置:确保服务器防火墙开放了 80 端口(或其他你配置的端口),否则外部无法访问。
- 权限问题:安装和启动过程中如果遇到权限不足,使用
sudo 或切换到 root 用户。
- 配置检查:修改配置文件后,先执行
./nginx -t 检查语法是否正确,再重启,避免配置错误导致 Nginx 无法启动。
未完待续,后续将补充 Nginx 虚拟主机配置、HTTPS 证书配置、负载均衡等内容~