CentOS 7下编译安装 Nginx 并配置 Web、HTTPS、PHP 与反向代理
1. 服务介绍
Nginx 是高性能 Web 服务器和反向代理服务器,可发布静态网站、终止 HTTPS、把 PHP 请求交给 PHP-FPM,并将客户端请求转发到多个后端节点。它常用于网站入口、动静分离、负载均衡和应用网关。本实验使用源码包编译安装 Nginx,并完成常见站点功能。
2. 准备运行环境
示例环境如下:
| 角色 | IP 地址 | 用途 |
| --- | --- | --- |
| Nginx 服务器 | 10.1.100.101 | Web、HTTPS、PHP、反向代理入口 |
| 后端 Web 1 | 10.1.100.201 | 负载均衡节点 |
| 后端 Web 2 | 10.1.100.202 | 负载均衡节点 |
| 测试客户端 | 10.1.100.50 | 浏览器和 curl 验证 |
• 操作系统:CentOS 7.x。
• 操作账号:root 或具备 sudo 权限的管理员。
• 软件源:YUM 可用。
• 源码包:本实验沿用原始资料中的 nginx-1.4.2.tar.gz。生产环境应使用仍受支持的稳定版本。
• 网络:客户端可访问服务端 80、443 端口,Nginx 可访问两个后端节点。
开始前检查系统和网络:
cat /etc/centos-release uname -r ip addr ping -c 3 10.1.100.201 ping -c 3 10.1.100.202
3. 相关知识
• Nginx 主配置文件由 main、events、http、server、location 等上下文组成,指令必须放在允许的上下文中。
• HTTP 和 HTTPS 常用 TCP 80、443。访问失败时先检查监听端口、防火墙、SELinux 和路由。
• 修改配置后先执行 nginx -t,语法正确再重载,避免错误配置中断现有服务。
• server_name 决定虚拟主机匹配;测试域名时可先在客户端 hosts 文件中绑定服务器 IP。
• PHP 代码不能由 Nginx 自己解释,需要通过 FastCGI 转交 PHP-FPM。
• 反向代理隐藏后端服务器;正向代理代表客户端访问外部网络。Nginx 核心模块不适合作为通用 HTTPS 正向代理,完整正向代理通常使用 Squid 等软件。
• 负载均衡默认使用轮询,也可使用 least_conn、ip_hash 或权重。必须同时配置健康检查或失败重试策略。
• 排错重点:/usr/local/nginx/logs/error.log、访问日志、curl 状态码、后端连通性和 SELinux 审计日志。
4. 实验步骤
4.1 安装编译依赖
yum install -y gcc gcc-c++ make pcre-devel zlib-devel openssl-devel useradd -r -s /sbin/nologin nginx
4.2 上传并检查源码包
在 Windows 客户端使用 SCP 上传源码包:

在服务器确认文件已经上传:

ls -lh /root/nginx-1.4.2.tar.gz sha256sum /root/nginx-1.4.2.tar.gz
4.3 解压并编译安装
cd /root tar -zxvf nginx-1.4.2.tar.gz
![]()
编译并安装:
cd /root/nginx-1.4.2 ./configure \ --prefix=/usr/local/nginx \ --user=nginx \ --group=nginx \ --with-http_ssl_module \ --with-http_stub_status_module make -j2 make install /usr/local/nginx/sbin/nginx -V
4.4 配置 systemd 服务
创建 /etc/systemd/system/nginx.service:
[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
systemctl daemon-reload systemctl enable nginx
4.5 配置基础网站
创建站点目录和首页:
mkdir -p /data/www/site echo 'Nginx site on 10.1.100.101' > /data/www/site/index.html chown -R nginx:nginx /data/www/site
在 /usr/local/nginx/conf/nginx.conf 的 http 段加入:
include /usr/local/nginx/conf/conf.d/*.conf;
创建配置目录和 /usr/local/nginx/conf/conf.d/site.conf:
mkdir -p /usr/local/nginx/conf/conf.d
server {
listen 80;
server_name www.example.test;
root /data/www/site;
index index.html index.php;
access_log logs/site_access.log;
error_log logs/site_error.log;
location / {
try_files $uri $uri/ =404;
}
}客户端 hosts 文件加入:
10.1.100.101 www.example.test
4.6 配置 HTTPS 网站
创建实验自签名证书:
mkdir -p /usr/local/nginx/conf/ssl openssl req -x509 -nodes -newkey rsa:2048 -days 365 \ -keyout /usr/local/nginx/conf/ssl/www.example.test.key \ -out /usr/local/nginx/conf/ssl/www.example.test.crt \ -subj '/C=CN/ST=Zhejiang/L=Hangzhou/O=Lab/CN=www.example.test' chmod 600 /usr/local/nginx/conf/ssl/www.example.test.key
在 site.conf 增加 HTTPS 虚拟主机:
server {
listen 443 ssl;
server_name www.example.test;
root /data/www/site;
ssl_certificate /usr/local/nginx/conf/ssl/www.example.test.crt;
ssl_certificate_key /usr/local/nginx/conf/ssl/www.example.test.key;
ssl_protocols TLSv1.2;
location / {
try_files $uri $uri/ =404;
}
}自签名证书只用于实验。生产环境应使用可信 CA 证书并启用当前安全协议和加密套件。
4.7 配置 PHP 解析
yum install -y epel-release php php-fpm sed -i 's/^user = apache/user = nginx/' /etc/php-fpm.d/www.conf sed -i 's/^group = apache/group = nginx/' /etc/php-fpm.d/www.conf systemctl enable --now php-fpm echo '<?php echo "PHP OK ".PHP_VERSION; ?>' > /data/www/site/info.php chown nginx:nginx /data/www/site/info.php
在两个 server 段中按需加入:
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}4.8 配置反向代理、动静分离和负载均衡
创建 /usr/local/nginx/conf/conf.d/proxy.conf:
upstream backend_pool {
least_conn;
server 10.1.100.201:80 max_fails=3 fail_timeout=10s;
server 10.1.100.202:80 max_fails=3 fail_timeout=10s;
keepalive 16;
}
server {
listen 80;
server_name app.example.test;
location /static/ {
alias /data/www/static/;
expires 7d;
}
location / {
proxy_pass http://backend_pool;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Connection '';
proxy_next_upstream error timeout http_502 http_503 http_504;
}
}准备静态测试文件:
mkdir -p /data/www/static echo 'static file from nginx' > /data/www/static/test.txt chown -R nginx:nginx /data/www/static
客户端 hosts 文件再加入:
10.1.100.101 app.example.test
4.9 检查配置并启动服务
/usr/local/nginx/sbin/nginx -t systemctl restart nginx systemctl status nginx --no-pager firewall-cmd --permanent --add-service=http firewall-cmd --permanent --add-service=https firewall-cmd --reload
5. 验证结果
5.1 服务端检查
/usr/local/nginx/sbin/nginx -t systemctl status nginx --no-pager systemctl status php-fpm --no-pager ss -lntp | grep -E ':80|:443|:9000' curl -I http://127.0.0.1/ curl -kI https://www.example.test/ tail -n 50 /usr/local/nginx/logs/error.log journalctl -u nginx -n 50 --no-pager
5.2 客户端功能验证
curl -I http://www.example.test/
curl -k https://www.example.test/
curl http://www.example.test/info.php
curl http://app.example.test/static/test.txt
for i in {1..6}; do curl -s http://app.example.test/; done基础页面返回 200,HTTPS 能建立连接,PHP 页面显示版本,静态文件由 Nginx 返回,多次访问代理域名能到达后端节点,说明配置生效。
5.3 后端故障验证
停止一个后端 Web 服务,再连续访问代理域名:
for i in {1..6}; do curl -s http://app.example.test/; done请求仍能由另一台后端处理,同时错误日志记录失败节点,说明反向代理失败重试生效。恢复后端后再次验证轮询结果。
到此这篇关于CentOS 7下编译安装 Nginx 并配置 Web、HTTPS、PHP 与反向代理的文章就介绍到这了,更多相关CentOS 7编译安装 Nginx内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
- 详解Centos7 源码编译安装 Nginx1.13
- CentOS 7.3.1611编译安装Nginx1.10.3+MySQL5.7.16+PHP7.1.2
- CentOS 6.5编译安装Nginx 1.10.2+MySQL 5.5.52+PHP5.5.38
- CentOS下编译安装nginx及配置缩略图插件的方法教程
- CentOS 7.2.1511 编译安装Nginx1.10.1+MySQL5.6.33+PHP5.6.26运行环境
- CentOS 7.2.1511 编译安装Nginx1.10.1+MySQL5.7.14+PHP7.0.11
- CentOS 7.0编译安装lnmp教程(Nginx1.6.0+MySQL5.6.19+PHP5.5.14)
- CentOS 6.6服务器编译安装lnmp(Nginx1.6.2+MySQL5.6.21+PHP5.6.3)
- CentOS 7.2 下编译安装PHP7.0.10+MySQL5.7.14+Nginx1.10.1的方法详解(mini版本)
- CentOS下编译、安装与配置nginx
相关文章
Ubuntu服务器已下载Nginx安装包的安装步骤(最新推荐)
在Ubuntu服务器上安装已下载的 Nginx,需完成依赖安装、解压编译、配置安装及服务验证等步骤,本文给大家介绍Ubuntu服务器已下载Nginx安装包的安装步骤,感兴趣的朋友跟随小编一起看看吧2025-10-10


最新评论