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内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论