本文更新於2021-04-29,使用nginx 1.16。
目錄
安裝
各系統的安裝包可於官網http://nginx.org/en/download.html下載。
Windows下安裝
從http://nginx.org/download/nginx-1.16.1.zip下載安裝包,並解壓,解壓目錄即為安裝目錄。
Linux下安裝
-
安裝依賴庫(按需選擇版本),用於編譯時指定依賴庫的源代碼目錄:
-
CentOS(7.5)下安裝依賴庫:
yum install pcre-devel yum install zlib-devel yum install openssl-devel
-
Debian(8.9)下安裝依賴庫(CentOS下也可使用):
cd /usr/local/src wget https://ftp.pcre.org/pub/pcre/pcre-8.44.tar.gz wget http://www.zlib.net/zlib-1.2.11.tar.gz wget https://www.openssl.org/source/openssl-1.1.1f.tar.gz tar -xvz -f pcre-8.44.tar.gz tar -xvz -f zlib-1.2.11.tar.gz tar -xvz -f openssl-1.1.1f.tar.gz
可刪除.tar.gz文件。
-
-
創建用戶:
groupadd nginxd useradd -g nginxd nginxd
-
創建安裝目錄:
cd /usr/local mkdir nginx chown nginxd:nginxd nginx
-
安裝nginx:
-
CentOS下安裝:
wget http://nginx.org/download/nginx-1.16.1.tar.gz tar -xvz -f nginx-1.16.1.tar.gz cd nginx-1.16.1 ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre make make install
-
Debian下安裝(CentOS下也可使用):
wget http://nginx.org/download/nginx-1.16.1.tar.gz tar -xvz -f nginx-1.16.1.tar.gz cd nginx-1.16.1 ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module \ --with-openssl=/usr/local/src/openssl-1.1.1f --with-zlib=/usr/local/src/zlib-1.2.11 --with-pcre=/usr/local/src/pcre-8.44 make make install
./configure
參數含義如下:- --prefix:安裝目錄。
- --with-http_ssl_module:SSL模塊,用於支持SSL,需要安裝依賴openssl。
- --with-http_stub_status_module:監控模塊,用於監控nginx狀態。
- --with-pcre:PCRE模塊,用於支持正則表達式,需要安裝依賴pcre。
- --with-zlib:ZLIB模塊,用於支持壓縮算法,需安裝依賴zlib。
注意
make install
會覆蓋此前安裝的所有文件,包括配置文件。 -
-
權限配置
chown -R nginxd:nginxd /usr/local/nginx vi /usr/local/nginx/nginx.conf
修改/usr/local/nginx/confi/nginx.conf的user為nginxd。
運行
需先進入安裝目錄。
運行nginx時不使用參數,會啟動nginx服務,包含主進程和工作進程兩個nginx進程。
運行nginx時也可使用以下參數:
- -?, -h:查看幫助。
- -c filename:設置配置文件名(默認為conf/nginx.conf)。
- -g directives:設置配置文件之外的全局指令。
- -p prefix:設置路徑前綴(默認無)。
- -q:檢查配置期間禁止打印非錯誤消息。
- -s signal:發送信號至主進程,signal可為:
- stop:強制停止服務。
- quit:優雅退出服務。
- reopen:打開新的日志文件。需要先重命名原來的日志文件。
- reload:重新加載配置文件,服務不會中止。
- -t:檢查配置后退出。
- -T:檢查配置,轉儲后退出。
- -v:打印版本后退出。
- -V:打印版本和configure編譯配置參數后退出。
日志
請求日志為安裝目錄下的logs/access.log。
請求日志使用組合日志格式,各字段依次為:
- 客戶端主機名或IP。
- ident查找的用戶名。
- 已認證的用戶名。
- 日期時間,以“[]”擴起。
- 請求行,以“""”引起。
- 響應狀態碼。
- 響應Content-Length,如無則為0。
- 請求Referer。
- 請求User-Agent。