Nginx的源码编译
1.下载软件
[root@Nginx ~]# wget https://nginx.org/download/nginx-1.28.1.tar.gz2.解压
[root@Nginx ~]# tar zxf nginx-1.28.1.tar.gz[root@Nginx ~]# cd nginx-1.28.1/[root@Nginx nginx-1.28.1]# lsauto CHANGES.ru conf contrib htmlmanSECURITY.md CHANGES CODE_OF_CONDUCT.md configure CONTRIBUTING.md LICENSE README.md src- auto/
编译辅助脚本,./configure执行时会调用这里面脚本,检测系统环境、生成 Makefile,一般不用手动改。 - conf/
源码自带的 nginx 示例配置文件。
注意:编译安装完成后,真正使用的配置文件在安装目录(默认/usr/local/nginx/conf),这里只是源码里的模板。 - contrib/
附加工具,提供 vim 语法高亮脚本、nginx 脚本示例等。 - html/
默认网页页面模板(欢迎页、50x 错误页面),编译后复制到安装目录 html。 - man/
nginx 帮助手册。 - src/
最重要:nginx 全部 C 语言源代码
3.检测环境
#安装依赖性[root@Nginx ~]# dnf install gcc openssl-devel.x86_64 pcre2-devel.x86_64 zlib-devel -y[root@Nginx nginx-1.28.1]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module- –prefix=/usr/local/nginx:安装根目录,后续所有程序、配置、日志都放在这个目录。卸载直接删这个文件夹即可。
- –user=nginx --group=nginx:nginx 工作进程以 nginx 用户运行,必须提前创建 nginx 用户组和用户,不能用 root 身份跑业务进程,安全规范。
- –with-http_ssl_module:开启 HTTPS ssl 模块,实现 443 端口加密访问。
- –with-http_v2_module:开启 HTTP/2 协议,网页加载性能优化,HTTPS 下生效。
- –with-http_realip_module:获取真实客户端 IP;Nginx 做反向代理时,拿到原始用户 IP,而不是代理服务器 IP。
- –with-http_stub_status_module:状态监控模块,可以访问
/status页面查看连接数、请求数等运行指标。 - –with-http_gzip_static_module:预压缩 gzip 模块,直接返回已经压缩好的
.gz文件,比运行时 gzip 效率更高。 - –with-pcre:编译内置 pcre 正则库,rewrite、location 正则匹配。
- –with-stream:四层 TCP/UDP 反向代理模块,做端口转发、数据库代理、tcp 负载均衡。
- –with-stream_ssl_module:四层 stream 的 SSL 解密,TCP 流量的 SSL 终止。
- –with-stream_realip_module:四层代理下获取真实客户端 IP。
4.编译
[root@Nginx nginx-1.28.1]# make [root@Nginx nginx-1.28.1]# make install5.nginx启动
#设定环境变量 [root@Nginx sbin]# vim ~/.bash_profile export PATH=$PATH:/usr/local/nginx/sbin [root@Nginx sbin]# source ~/.bash_profile [root@Nginx logs]# useradd -s /sbin/nologin -M nginx [root@Nginx logs]# nginx [root@Nginx logs]# ps aux | grep nginx root 44012 0.0 0.1 14688 2356 ? Ss 17:01 0:00 nginx: master process nginx nginx 44013 0.0 0.2 14888 3892 ? S 17:01 0:00 nginx: worker process root 44015 0.0 0.1 6636 2176 pts/0 S+ 17:01 0:00 grep --color=auto nginx #测试 [root@Nginx logs]# echo timinglee > /usr/local/nginx/html/index.html [root@Nginx logs]# curl 172.25.254.100 timinglee6.编写启动文件
[root@Nginx ~]# vim /lib/systemd/system/nginx.service[Unit]Description=The NGINX HTTP and reverse proxy serverAfter=syslog.target network-online.target remote-fs.target nss-lookup.targetWants=network-online.target[Service]Type=forkingExecStartPre=/usr/local/nginx/sbin/nginx-tExecStart=/usr/local/nginx/sbin/nginxExecReload=/usr/local/nginx/sbin/nginx-sreloadExecStop=/bin/kill-sQUIT$MAINPIDPrivateTmp=true[Install]WantedBy=multi-user.target[root@Nginx ~]# systemctl daemon-reload#验证[root@Nginx ~]# systemctl status nginx.service○ nginx.service - The NGINX HTTP and reverse proxy server Loaded: loaded(/usr/lib/systemd/system/nginx.service;disabled;preset: disabled)Active: inactive(dead)[root@Nginx ~]# systemctl enable --now nginx[root@Nginx ~]# ps aux | grep nginxroot18390.00.1146882356? Ss 09:530:00 nginx: master process /usr/local/nginx/sbin/nginx nginx18400.00.2148883828? S 09:530:00 nginx: worker process[root@Nginx ~]# systemctl status nginx.service