1. 程式人生 > >Linux製作綠色版nginx

Linux製作綠色版nginx

在docker化的部署方式中,時常需要一個輕量化的nginx系統,主要用於實現動靜分離,實現路由轉發,或部署靜態資源(可類比web容器)。

輕量化的nginx,可以與靜態資源一起,或介面服務一起,方便的打包進docker應用中。

nginx的windows版本就類似這種結構,然而在linux下似乎沒有如下這種組織結構:

[[email protected] 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資料夾;
3、進入原始碼目錄,並執行configure命令

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

–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-green/”
nginx binary file: “/opt/nginx-green//sbin/nginx”
nginx modules path: “/opt/nginx-green//modules”
nginx configuration prefix: “/opt/nginx-green//conf”
nginx configuration file: “/opt/nginx-green//conf/nginx.conf”
nginx pid file: “/opt/nginx-green//logs/nginx.pid”
nginx error log file: “/opt/nginx-green//logs/error.log”
nginx http access log file: “/opt/nginx-green//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-green資料夾。
後續可基於此模板檔案,做一些優化配置,即可用於docker內部署。

進入到該資料夾nginx-green中,目錄結果如下:

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

直接啟動後,可看到如下程序:

[[email protected] 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