Linux制作绿色版nginx

在docker化的部署方式中,时常需要一个轻量化的nginx系统,主要用于实现动静分离,实现路由转发,或部署静态资源(可类比web容器)。

轻量化的nginx,可以与静态资源一起,或接口服务一起,方便的打包进docker应用中。

nginx的windows版本就类似这种结构,然而在linux下似乎没有如下这种组织结构:

[root@5798e1897433 nginx]# ls -al
drwxr-xr-x 2 root root 4096 Dec 17 04:45 conf --nginx.conf配置文件
drwxr-xr-x 3 root root 4096 Dec 18 10:03 html --存放静态资源文件,或在root中指定到一个目录中
drwxr-xr-x 2 root root 4096 Dec 12 12:34 logs
drwxr-xr-x 2 root root 4096 Dec 12 06:49 sbin --NG二进制启动脚本,windows版本通过在外部

Nginx在日常使用中(这里主要指linux系统),主要通过以下两种方式安装:

1、各linux发行版的安装命令安装;
2、源码安装;

经过实验,发现如下步骤可以实现这个目标:

以下通过源码编译安装方式制作绿色版nginx软件:
1、在某个目录下(如/opt)下载nginx-1.14.2.tar.gz,解压;
2、/opt下创建nginx-green文件夹,注意此处如果的文件夹名称会被编译至二进制命令sbin/nginx文件中,导致后续使用时,如果nginx文件夹不是nginx-green,将报错,如下所示。

建议直接在/opt下新建nginx文件夹,生成绿色版文件后,在备份模板文件为nginx-green,拷贝模板至docker中时建议修改为nginx;

nginx: [alert] could not open error log file: open() "/opt/nginx-green//logs/error.log" failed (2: No such file or directory)
2019/01/16 14:03:52 [emerg] 7#0: open() "/opt/nginx-green//conf/nginx.conf" failed (2: No such file or directory)

说明
以下安装均在/opt/nginx目录下进行

3、进入源码目录,并执行configure命令

cd nginx-1.14.2;
./configure --prefix=/opt/nginx/

–prefix 用于指定nginx编译后的安装目录

Console中看到如下提示,已成功完成配置。

checking for PCRE library … found
checking for PCRE JIT support … found
checking for zlib library … found
creating objs/Makefile

Configuration summary

  • using system PCRE library
  • OpenSSL library is not used
  • using system zlib library

nginx path prefix: “/opt/nginx/”
nginx binary file: “/opt/nginx//sbin/nginx”
nginx modules path: “/opt/nginx//modules”
nginx configuration prefix: “/opt/nginx//conf”
nginx configuration file: “/opt/nginx//conf/nginx.conf”
nginx pid file: “/opt/nginx//logs/nginx.pid”
nginx error log file: “/opt/nginx//logs/error.log”
nginx http access log file: “/opt/nginx//logs/access.log”
nginx http client request body temporary files: “client_body_temp”
nginx http proxy temporary files: “proxy_temp”
nginx http fastcgi temporary files: “fastcgi_temp”
nginx http uwsgi temporary files: “uwsgi_temp”
nginx http scgi temporary files: “scgi_temp”

4、编译
make

5、安装
make install

如上步骤完成后,绿色版的nginx即制作完成,即为刚才创建的nginx文件夹。
后续可基于此模板文件,做一些优化配置,即可用于docker内部署。

进入到该文件夹nginx中,目录结果如下:

nginx
├── conf
│ ├── fastcgi.conf
│ ├── fastcgi.conf.default
│ ├── fastcgi_params
│ ├── fastcgi_params.default
│ ├── koi-utf
│ ├── koi-win
│ ├── mime.types
│ ├── mime.types.default
│ ├── nginx.conf
│ ├── nginx.conf.default
│ ├── scgi_params
│ ├── scgi_params.default
│ ├── uwsgi_params
│ ├── uwsgi_params.default
│ └── win-utf
├── html
│ ├── 50x.html
│ └── index.html
├── logs
│ ├── access.log
│ ├── error.log
│ └── nginx.pid
└── sbin
└── nginx

docker中部署时,需要注意,容器内,即使配过环境变量,也不能直接启动sbin目录下的nginx,需要携带目录启动,如下:
sbin/nginx

直接启动后,可看到如下进程:

[root@testxxx sbin]# ps -ef|grep nginx
root     18295     1  0 20:29 ?        00:00:00 nginx: master process ./nginx
nobody   18296 18295  0 20:29 ?        00:00:00 nginx: worker process
root     18304  6114  0 20:29 pts/0    00:00:00 grep --color=auto nginx

附加1:


一. 前期安装的nginx未添加ssl模块,无法支持配置https访问.
可能会报如下错误:
nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module in /opt/nginx/conf/nginx.conf:36

解决方案:
./configure --prefix=/opt/nginx --with-http_stub_status_module --with-http_ssl_module

引用列表,致谢:

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值