背景
最近對項目進行了Https改造,改造過程涉及Nginx技術,因此進行簡單總結。
從本文開始將開啟一個新的專題Nginx系列,用于收集Nginx相關的文章,內容將包括:
- Nginx系列—1 Nginx安裝與使用
- Nginx系列—2 Nginx配置
- Nginx系列—3 支持Https
- Nginx系列—4 Nginx啟動流程
- Nginx系列—5 Nginx消息處理流程
- Nginx系列—6 Nginx自定義模塊
本文介紹nginx安裝過程和簡單使用,以及如何將Nginx委托給systemd管理。
1.nginx編譯與安裝
1.1 依賴軟件
(1) gcc編譯器,用于編譯C語言程序
yum install -y gcc
yum install -y gcc-c++
(2) pcre庫,提供正則表達式能力
yum install -y pcre
yum install -y pcre-devel
(3) zlib庫,用于提供gzip壓縮能力
yum install -y zlib
yum install -y zlib-devel
(4) openssl庫,用于HTTPS能力
yum install -y openssl
yum install -y openssl-devel
說明:如果所需的Nginx不需要SSL能力,則不需要openssl庫。
1.2 編譯和安裝
nginx資源包下載地址: http://nginx.org/download/, 可以選擇所需的版本。這里以1.26.0版本為例進行介紹:
#下載資源
wget http://nginx.org/download/nginx-1.26.0.tar.gz# 解壓資源
tar -zxvf nginx-1.26.0.tar.gz# 編譯nginx
cd nginx-1.26.0/
./configure
./make# 安裝nginx
./make install
configure命令檢測操作系統類型、依賴的軟件等,根據配置參數(后面介紹)生成C源碼文件和Makefile文件; make根據configure命令生成的Makefile文件編譯Nginx生成二進制文件; make install將編譯結果安裝到環境上。
上述執行configure命令時日志部分如下:
[root@124 nginx-1.26.0]# ./configure...+ using system PCRE library+ OpenSSL library is not used+ using system zlib library...
使用了PCRE和zlib,默認沒有使用OpenSSL,即此案例編譯生成的Nginx不支持HTTPS。
說明:執行configure命令時,支持傳入配置參數定制Nginx,不傳則使用Nginx的默認配置。
configure常用配置:
(1) 路徑配置
–prefix 指定安裝后Nginx的根目錄,不指定時默認為/usr/local/nginx;至于其他相對于nginx根路徑的配置,如sbin,conf,errorlog,pid等,一般不需要修改,使用Nginx的默認配置即可。
(2) http配置
nginx默認會將http必要的模塊編譯到nginx中,不需要額外指定;不需要對應模塊,可以使用–without-http_xxx_module配置。
部分http模塊提供定制的功能,需要用戶通過配置指定,如下所示:
[1]–with-http_ssl_module 支持HTTPS模塊(可通過–with-openssl=/path指定編譯環境上openssl的路徑)
[2]–with-http_realip_module 支持從客戶端請求的Header信息中獲取客戶端真是IP地址
[3]–with-http_addition_module 支持在返回HTTP響應給客戶端前在HTTP包體頭部或尾部添加內容
[4]–with-http_sub_module 支持將HTTP響應包中指定字符串進行替換
[5]–with-http_flv_module和–with-http_mp4_module 客戶端播放flv和map格式的視頻時可以拖動
(3) 其他配置
–user和–group可用來指定Nginx運行時的用戶
如nginx安裝的路徑為/usr/lcoal/ewen/nginx,且要求支持https, 上述./configure
命令可以修改為:
./configure --prefix=/usr/lcoal/ewen/nginx --with-http_ssl_module --with-http_sub_module
1.3 目錄介紹
上述通過–prex指定了nginx根路徑為/usr/lcoal/ewen/nginx,在執行make install命令后,nginx被安裝到了/usr/lcoal/ewen/nginx路徑下:
[root@124 conf]# cd /usr/lcoal/ewen/nginx
[root@host44 conf]# ls -al
總用量 4
drwxr-xr-x. 2 root root 4096 6月 30 11:26 conf
drwxr-xr-x. 2 root root 40 6月 30 10:22 html
drwxr-xr-x. 2 root root 6 6月 30 10:22 logs
drwxr-xr-x. 2 root root 19 6月 30 10:22 sbin
存在sbin、logs、html、conf四個目錄:
[1] sbin目錄下只有一個nginx文件, 是可執行的二進制文件,用于啟動/停止Nginx等;
[root@124 sbin]# ls -al
-rwxr-xr-x. 1 root root 3930704 6月 30 10:22 nginx
說明:nginx默認已具備可執行權限。
[2] logs目錄存放Nginx運行過程中產生的日志文件。
[3] html目錄存放靜態頁面資源:
[root@124 html]# ls -al
-rw-r--r--. 1 root root 497 6月 30 10:22 50x.html
-rw-r--r--. 1 root root 626 6月 30 11:47 index.html
說明:當前只有兩個文件, 50x.html和index.html分別表示異常頁面和Nginx默認首頁;
[4] conf目錄用于存放nginx的配置文件:
[root@124 conf]# ls -al
-rw-r--r--. 1 root root 1077 6月 30 10:22 fastcgi.conf
-rw-r--r--. 1 root root 1077 6月 30 10:22 fastcgi.conf.default
-rw-r--r--. 1 root root 1007 6月 30 10:22 fastcgi_params
-rw-r--r--. 1 root root 1007 6月 30 10:22 fastcgi_params.default
-rw-r--r--. 1 root root 2837 6月 30 10:22 koi-utf
-rw-r--r--. 1 root root 2223 6月 30 10:22 koi-win
-rw-r--r--. 1 root root 5349 6月 30 10:22 mime.types
-rw-r--r--. 1 root root 5349 6月 30 10:22 mime.types.default
-rw-r--r--. 1 root root 2656 6月 30 10:22 nginx.conf
-rw-r--r--. 1 root root 2656 6月 30 10:22 nginx.conf.default
-rw-r--r--. 1 root root 636 6月 30 10:22 scgi_params
-rw-r--r--. 1 root root 636 6月 30 10:22 scgi_params.default
-rw-r--r--. 1 root root 664 6月 30 10:22 uwsgi_params
-rw-r--r--. 1 root root 664 6月 30 10:22 uwsgi_params.default
-rw-r--r--. 1 root root 3610 6月 30 10:22 win-utf
其中,xx.defualt文件是一個備份或示例配置文件,Nginx不會加載這些文件。如nginx.conf被修改后,可用nginx.conf.default還原nginx.conf至初始安裝狀態。
去除default文件后,余下配置文件可以分為以下4類:
[1] koi-win、koi-utf、win-utf
編碼轉換映射時需要的文件,向客戶端發送響應時,將一種編碼轉換到另一種編碼。
[2] fastcgi, scgi, uwsgi
分別用于 FastCGI、SCGI 、uWSGI 相關的配置(暫未接觸過)。
[3] mime.types
文件擴展名與文件類型映射表,nginx將根據文件中的映射關系設置Http頭域的的Content-Type值;
默認配置一般指定為application/octet-stream。在nginx.conf的http塊中一般固定配置如下:
http {include mime.types;default_type application/octet-stream;#...其他配置
}
[4] nginx.conf
當Nginx被當做反向代理服務器或者web應用時,nginx.conf是配置的核心。這部分內容較多,鑒于篇幅考慮,將在Nginx系列2中進行介紹。
2.nginx命令介紹
[1] 啟動nginx:
./nginx# 可以使用 -c 指定配置文件路徑
./nginx -c /etc/nginx/nginx.conf# 可以使用 -p 指定prefix 相對路徑,此時會覆蓋configure編譯時執行的前綴
./nginx -p /usr/local/ewen/nginx
[2] 停止Ngnix
# 快速停止nginx
./nginx -s stop# 優雅停止,等所有請求處理完再停止
./nginx -s quit
[3] 重載配置文件
# 修改配置文件后,重新加載
./nginx -s reload
[4] 測試配置文件是否正常**
# 檢測配置文件是否正常
./nginx -t
4.使用systemctl管理
說明:由于80端口在當前服務器上已被其他應用占有,案例將nginx.conf配置文件中的80端口修改為8000進行演示.
nginx.service 文件:
[Unit]
Description=nginx-ewen
After=network.target[Service]
User=ewen
Group=ewen
Type=forkingExecStart=/usr/local/ewen/nginx/sbin/nginx -p /usr/local/ewen/nginx
ExecReload=/usr/local/ewen/nginx/sbin/nginx -p /usr/local/ewen/nginx -s reload
ExecStop=/usr/local/ewen/nginx/sbin/nginx -p /usr/local/ewen/nginx -s stop
PrivateTmp=true[Install]
WantedBy=multi-user.target
將nginx.service 文件放到**/etc/systemd/system**目錄下,執行如下命令后將nginx委托給systemctl管理:
# systemd重載service配置文件
systemctl daemon-reload# 設置nginx開啟自啟動
systemctl enable nginx
由此,可通過systemctl命令來控制nginx的啟停:
[root@124 conf]# systemctl start nginx
[root@124 conf]# systemctl status nginx
● nginx.service - nginx-ewenLoaded: loaded (/etc/systemd/system/nginx.service; enabled; vendor preset: disabled)Active: active (running) since 三 2024-06-30 12:17:16 CST; 8s agoProcess: 22705 ExecStop=/usr/local/ewen/nginx/sbin/nginx -p /usr/local/ewen/nginx -s stop (code=exited, status=0/SUCCESS)Process: 22920 ExecStart=/usr/local/ewen/nginx/sbin/nginx -p /usr/local/ewen/nginx (code=exited, status=0/SUCCESS)Main PID: 22922 (nginx)Tasks: 2CGroup: /system.slice/nginx.service├─22922 nginx: master process /usr/local/ewen/nginx/sbin/nginx -p /usr/local/ewen/nginx└─22923 nginx: worker process6月 30 12:17:16 host44 systemd[1]: Starting nginx-ewen...
6月 30 12:17:16 host44 systemd[1]: Started nginx-ewen.
查看nginx啟動狀態:
[root@124 conf]# ps -ef | grep nginx | grep -v grep
ewen 22922 1 0 12:17 ? 00:00:00 nginx: master process /usr/local/ewen/nginx/sbin/nginx -p /usr/local/ewen/nginx
ewen 22923 22922 0 12:17 ? 00:00:00 nginx: worker process[root@124 conf]# netstat -anp | grep 22922
tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN 22922/nginx: master
unix 3 [ ] STREAM CONNECTED 78715317 22922/nginx: master
unix 3 [ ] STREAM CONNECTED 78715316 22922/nginx: master
此時,nginx服務已經啟動,屬組為ewen, 監聽的端口號為8000.
訪問8000主頁面:
[root@host44 conf]# curl http://localhost:8000
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p><p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p><p><em>Thank you for using nginx gracefully.</em></p>
</body>
</html>
5.日志輪轉
nginx日志需要借助日志輪轉工具實現日志的備份和分割,已在前面的文章中介紹過,請參考:日志輪轉—cron和logrotate.