
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.2023. 相关知识• 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 nginx4.2 上传并检查源码包在 Windows 客户端使用 SCP 上传源码包在服务器确认文件已经上传ls -lh /root/nginx-1.4.2.tar.gz sha256sum /root/nginx-1.4.2.tar.gz4.3 解压并编译安装cd /root tar -zxvf nginx-1.4.2.tar.gz编译并安装cd /root/nginx-1.4.2 ./configure \ --prefix/usr/local/nginx \ --usernginx \ --groupnginx \ --with-http_ssl_module \ --with-http_stub_status_module make -j2 make install /usr/local/nginx/sbin/nginx -V4.4 配置 systemd 服务创建/etc/systemd/system/nginx.service[Unit] DescriptionNginx Web Server Afternetwork.target [Service] Typeforking 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 PrivateTmptrue [Install] WantedBymulti-user.targetsystemctl daemon-reload systemctl enable nginx4.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.confmkdir -p /usr/local/nginx/conf/conf.dserver { 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.test4.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 /CCN/STZhejiang/LHangzhou/OLab/CNwww.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.confupstream backend_pool { least_conn; server 10.1.100.201:80 max_fails3 fail_timeout10s; server 10.1.100.202:80 max_fails3 fail_timeout10s; 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.test4.9 检查配置并启动服务/usr/local/nginx/sbin/nginx -t systemctl restart nginx systemctl status nginx --no-pager firewall-cmd --permanent --add-servicehttp firewall-cmd --permanent --add-servicehttps firewall-cmd --reload5. 验证结果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-pager5.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基础页面返回200HTTPS 能建立连接PHP 页面显示版本静态文件由 Nginx 返回多次访问代理域名能到达后端节点说明配置生效。5.3 后端故障验证停止一个后端 Web 服务再连续访问代理域名for i in {1..6}; do curl -s http://app.example.test/; done请求仍能由另一台后端处理同时错误日志记录失败节点说明反向代理失败重试生效。恢复后端后再次验证轮询结果。