二进制编译nginx方案
- 将nginx源码按所需模块编译制作rpm,提供centos7使用rpm包管理器安装。
- 需要软件:
yum -y install rpm-build wget gcc zlib-devel pcre-devel
rpm制作步骤
- dajx_nginx_1.18.0-2.rpm
基于vmware 中 centos7虚拟机制作rpm包
# 准备好centos7虚拟机
# 下载stale版本nginx源码
mkdir -pv ~/rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS,BUILDROOT}
wget -O ~/rpmbuild/SOURCES/nginx-1.18.0.tar.gz http://nginx.org/download/nginx-1.18.0.tar.gz
wget -O ~/rpmbuild/SOURCES/openssl-1.1.1a.tar.gz https://www.openssl.org/source/old/1.1.1/openssl-1.1.1a.tar.gz
cd ~/rpmbuild/SPECS/
# 编写rpm构建步骤
[root@localhost SPECS]# cat dajx_nginx.spec
Name: dajx_nginx
Version: 1.18.0
Release: 1
Summary: test
License: GPL
Group: Applications/System
Vendor: lg
Buildarch: noarch
Source: nginx-1.18.0.tar.gz
BuildRoot: %_topdir/BUILDROOT
BuildRequires: gcc
%description
this is a nginx rpm package.
%prep
tar xf $RPM_SOURCE_DIR/nginx-1.18.0.tar.gz
tar xf $RPM_SOURCE_DIR/openssl-1.1.1a.tar.gz
%build
cd nginx-1.18.0
./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-openssl=../openssl-1.1.1a
make %{?_smp_mflags}
%install
cd nginx-1.18.0
make install DESTDIR=%{buildroot}
%pre
%post
if ! id www >/dev/null 2>&1 ;then
groupadd www;useradd -g www www
fi
[ ! -f /sbin/nginx ] && cp /usr/local/nginx/sbin/nginx /sbin/nginx
%preun
%postun
%define _binaries_in_noarch_packages_terminate_build 0
%files
/usr/local/nginx/*
%changelog
rpmbuild -bb dajx_nginx.spec
- dajx_nginx_1.18.0-2.rpm
- 1.18.0-2 更改
- 添加新三方模块,基于1.18.0制作,对响应头有更加灵活设定
- 模块详细,参考地址
- 制作rpm(只列出上面步骤更改的地方)
#1. 下载模块源码
wget -O ~/rpmbuild/SOURCES/master.zip https://github.com/openresty/headers-more-nginx-module/archive/refs/heads/master.zip
#2. 修改dajx_nginx.spec中的./configure参数
%prep
unzip $RPM_SOURCE_DIR/master.zip
%build
./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-openssl=../openssl-1.1.1a --add-module=../headers-more-nginx-module-master
- 使用模块headers-more-nginx-module配置响应头
more_set_headers 'Access-Control-Allow-Origin: *';
more_set_headers 'Access-Control-Allow-Methods: GET, POST, OPTIONS';
more_set_headers 'Access-Control-Allow-Headers: DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization,Powder-ReqIndex,Powder-TrackID,token';
more_set_headers 'Access-Control-Allow-Credentials: true';
rpm包使用
yum -y install zlib-devel pcre-devel
rpm -ivh dajx_nginx_1.18.0-1.rpm
其他
- 优点:
1. 节省编译过程,安装更迅速
2. 适合批量环境定制化需求
- 缺点:
1. 为了二进制程序的兼容性(硬件架构),常常用比较老的硬件架构
2. 无法结合每个机器的特性编译出适合硬件特性的程序
3. 一般不可以添加和删除编译进二进制程序的依赖