002.Nginx安裝及啟動

一 Nginx yum安裝

1.1 前置準備

  1 [root@nginx01 ~]# systemctl status firewalld.service	#檢查防火牆
  2 [root@nginx01 ~]# getenforce				#檢查SELinux
  3 Disabled

提示:建議關閉防火牆,或通過如下方式放通相關80或443埠:

  1 firewall-cmd --permanent --add-port=80/tcp
  2 firewall-cmd --permanent --add-port=443/tcp

1.2 配置yum源

  1 [root@nginx01 ~]# cat > /etc/yum.repos.d/nginx.repo <<EOF
  2 [nginx-stable]
  3 name=nginx stable repo
  4 baseurl=//nginx.org/packages/centos/\$releasever/\$basearch/
  5 gpgcheck=1
  6 enabled=1
  7 gpgkey=//nginx.org/keys/nginx_signing.key
  8 module_hotfixes=true
  9 
 10 [nginx-mainline]
 11 name=nginx mainline repo
 12 baseurl=//nginx.org/packages/mainline/centos/\$releasever/\$basearch/
 13 gpgcheck=1
 14 enabled=0
 15 gpgkey=//nginx.org/keys/nginx_signing.key
 16 module_hotfixes=true
 17 EOF

1.3 安裝Nginx

  1 [root@nginx01 ~]# yum -y install nginx
  2 [root@nginx01 ~]# nginx -v
  3 nginx version: nginx/1.18.0

提示:如上安裝默認安裝為當前最新穩定版,若需要安裝開發版,可執行yum-config-manager –enable nginx-mainline,然後yum安裝,不建議安裝開發版。

參考://nginx.org/en/linux_packages.html。

  1 [root@nginx01 ~]# systemctl start nginx
  2 [root@nginx01 ~]# systemctl enable nginx			#啟動服務

1.4 測試訪問

瀏覽器訪問://172.24.8.71/

clipboard

1.5 其他資訊

  1 [root@nginx01 ~]# nginx -V			#查看yum安裝所編譯的模組及參數
  2 nginx version: nginx/1.18.0
  3 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
  4 built with OpenSSL 1.0.2k-fips  26 Jan 2017
  5 TLS SNI support enabled
  6 configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
  7 [root@nginx01 ~]# rpm -ql nginx			#查看所安裝的文件
  8 [root@nginx01 ~]# rpm -qc nginx 		        #查看相關的配置文件

二 Nginx源碼編譯安裝

2.1 依賴組件

  1 [root@nginx01 ~]# yum -y install gcc gcc-c++ wget autoconf pcre pcre-devel openssl openssl-devel openssh-clients net-tools vim ntp screen lrzsz bash-completion bash-completion-extras lvm2 make automake epel-release tree zlib zlib-devel libtool
提示:部分依賴包為比如,如:


zlib庫:zlib庫是ngx_http_gzip_module(gzip壓縮模組)所必需的


openssl庫 :–with-http_ssl_module使用該模組必需裝openssl庫,來實現http支援https協議。

2.2 編譯安裝

  1 [root@nginx01 ~]# useradd -s /sbin/nologin -M nginx	#提前創建用戶及用戶組
  2 [root@nginx01 ~]# wget //nginx.org/download/nginx-1.17.8.tar.gz
  3 [root@nginx01 ~]# tar -xvf nginx-1.17.8.tar.gz
  4 [root@nginx01 ~]# cd nginx-1.17.8/
  5 [root@nginx01 nginx-1.17.8]# ./configure \
  6 --conf-path=/usr/local/nginx/conf/nginx.conf \
  7 --error-log-path=/var/log/nginx/error.log \
  8 --group=nginx \
  9 --http-client-body-temp-path=/var/cache/nginx/client_temp \
 10 --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
 11 --http-log-path=/var/log/nginx/access.log \
 12 --http-proxy-temp-path=/var/cache/nginx/proxy_temp \
 13 --http-scgi-temp-path=/var/cache/nginx/scgi_temp \
 14 --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
 15 --lock-path=/var/run/nginx.lock \
 16 --pid-path=/var/run/nginx.pid \
 17 --prefix=/usr/local/nginx \
 18 --sbin-path=/usr/local/bin/nginx \
 19 --user=nginx \
 20 --with-http_gzip_static_module \
 21 --with-http_realip_module \
 22 --with-http_ssl_module \
 23 --with-http_stub_status_module \
 24 --with-http_sub_module \
 25 --with-http_v2_module \
 26 --with-stream \
 27 --with-stream_realip_module \
 28 --with-stream_ssl_module
 29 [root@nginx01 nginx-1.17.8]# make && make install
 30 [root@nginx01 ~]# nginx -V			        #查看安裝版本
 31 [root@nginx01 ~]# tree /usr/local/nginx/		#查看目錄結構

clipboard



目錄

作用
conf
用於存儲nginx配置文件
html
用於存放靜態網頁
logs
存放日誌
sbin
用於存放 nginx 執行命令

2.3 服務管理

  1 [root@nginx01 ~]# echo $PATH
  2 /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
  3 [root@nginx01 ~]# mkdir -p /var/cache/nginx/
  4 [root@nginx01 ~]# ll /usr/local/bin/
  5 total 7.5M
  6 -rwxr-xr-x 1 root root 7.5M Mar  5 01:09 nginx

提示:若nginx的prefix未編譯系統PATH中,如/opt/nginx/,需要在PATH中,可通過如下方式添加:


echo ‘PATH=/opt/nginx/:\$PATH’ > /etc/profile.d/nginx.sh
  1 [root@nginx01 ~]# nginx			#服務啟動
  2 [root@nginx01 ~]# nginx -s stop		#服務關閉
  3 [root@nginx01 ~]# nginx -s reload	        #重載配置文件
  4 [root@nginx01 ~]# nginx -s reopen	        #重啟Nginx
  5 [root@nginx01 ~]# nginx -s quit		#關閉Nginx
  6 [root@nginx01 ~]# nginx -t		        #測試配置文件
  7 [root@nginx01 ~]# nginx -t -c 【file】	#使用額外的配置文件測試
  8 [root@nginx01 ~]# ps aux | grep nginx	#查看進程
  9 [root@nginx01 ~]# netstat -ano | grep 80	#查看埠

2.4 開機啟動

  1 [root@nginx01 ~]# vi /usr/lib/systemd/system/nginx.service
  2 [Unit]
  3 Description=nginx - high performance web server
  4 Documentation=//nginx.org/en/docs/
  5 After=network-online.target remote-fs.target nss-lookup.target
  6 Wants=network-online.target
  7 
  8 [Service]
  9 Type=forking
 10 PIDFile=/var/run/nginx.pid
 11 ExecStart=/usr/local/bin/nginx -c /usr/local/nginx/conf/nginx.conf
 12 ExecReload=/bin/kill -s HUP $MAINPID
 13 ExecStop=/bin/kill -s TERM $MAINPID
 14 
 15 [Install]
 16 WantedBy=multi-user.target
 17 [root@nginx01 ~]# systemctl daemon-reload
 18 [root@nginx01 ~]# systemctl start nginx.service		#啟動服務
 19 [root@nginx01 ~]# systemctl enable nginx.service	#開機啟動

說明:


Description:描述服務


After:描述服務類別


[Service]:服務運行參數的設置


Type=forking:是後台運行的形式


ExecStart:為服務的具體運行命令


ExecReload:為重啟命令


ExecStop:為停止命令


PrivateTmp=True:表示給服務分配獨立的臨時空間


注意:[Service]的啟動、重啟、停止命令全部要求使用絕對路徑


[Install]運行級別下服務安裝的相關設置,可設置為多用戶,即系統運行級別為3


systemctl start nginx.service (啟動nginx服務)


systemctl stop nginx.service (停止nginx服務)


systemctl enable nginx.service (設置開機自啟動)


systemctl disable nginx.service (停止開機自啟動)


systemctl status nginx.service (查看服務當前狀態)


systemctl restart nginx.service (重新啟動服務)


systemctl list-units –type=service (查看所有已啟動的服務)

2.5 測試訪問

瀏覽器訪問://172.24.8.71/

2.6 編譯選項

  1 [root@nginx01 nginx-1.17.8]# ./configure --help		#查看編譯選項

如下為常見編譯選項及其釋義:






















































編譯選項

作用

–prefix=/etc/nginx

程式安裝目錄和路徑

–sbin-path=/usr/sbin/nginx

Nginx啟動停止名

–modules-path=/usr/lib64/nginx/modules

Nginx模組路徑

–conf-path=/etc/nginx/nginx.conf

Nginx主配置文件路徑

–error-log-path=/var/log/nginx/error.log

Nginx錯誤日誌路徑

–http-log-path=/var/log/nginx/access.log

Nginx訪問日誌路徑

–pid-path=/var/run/nginx.pid

Nginx Pid路徑

–lock-path=/var/run/nginx.lock

Nginx鎖路徑

–http-client-body-temp-path=/var/cache/nginx/client_temp

client頭部臨時快取文件

–http-proxy-temp-path=/var/cache/nginx/proxy_temp

proxy臨時快取文件

–http-fastcgi-temp-path=/var/cache/nginx/proxy_temp

fastcgi臨時快取文件

–http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp

uwsgi臨時快取文件

–http-scgi-temp-path=/var/cache/nginx/scgi_temp

scgi臨時快取文件

–user=nginx

設置Nginx進程啟動用戶

–group=nginx

設置Nginx進程啟動用戶組

–with-cc-opt

設置額外的參數將被添加到CFLACS變數

–with-id-opt

設置額外的參數,鏈接系統庫

三 Nginx目錄及模組

3.1 相關目錄

如下以Nginx yum安裝後的目錄為例:



















































路徑

類型

作用

/etc/nginx


/etc/nginx/nginx.conf


/etc/nginx/conf.d


/etc/nginx/conf.d/default.conf

配置文件

Nginx主配置文件

/etc/nginx/fastcgi_params


/etc/nginx/scgi_params


/etc/nginx/uwsgi_params

配置文件

Cgi、Fastcgi、Uwsgi配置文件

/etc/nginx/win-utf


/etc/nginx/koi-utf


/etc/nginx/koi-win

配置文件

Nginx編碼轉換映射文件

/etc/nginx/mime.types

配置文件

http協議的Content-Type

/etc/rc.d/init.d/nginx


/etc/rc.d/init.d/nginx-debug


/etc/sysconfig/nginx


/etc/sysconfig/nginx-debug

配置文件

配置系統守護進程管理器

/etc/logrotate.d/nginx

配置文件

Nginx日誌輪詢、日誌切割

/usr/sbin/nginx


/usr/sbin/nginx-debug

命令

Nginx終端管理器命令

/usr/share/doc/nginx-1.xx.x


/usr/share/man/man8/nginx.8.gz

目錄

Nginx的幫助手冊

/var/cache/nginx

目錄

Nginx的快取目錄

/var/log/nginx

目錄

Nginx的日誌目錄

/etc/nginx/modules


/etc/lib64/nginx


/etc/lib64/nginx/modules

目錄

Nginx的模組目錄

/usr/share/nginx


/usr/share/nginx/html


/usr/share/nginx/html/50x.html


/usr/share/nginx/html/index.html

目錄

Nginx默認站點目錄

3.2 Nginx模組

Nginx模組分為Nginx官方模組和Nginx第三方模組。






































Nginx編譯選項

模組作用

ngx_http_core_module

包含一些核心的http參數配置,對應Nginx的配置區塊部分。

ngx_http_access_module

訪問控制模組,用來控制網站用戶對Nginx的訪問。

ngx_http_gzip_module

壓縮模組,對Nginx返回的數據壓縮,屬於性能優化模組。

ngx_http_fastcgi_module

fastcgi模組,和動態應用相關的模組,例如PHP。

ngx_http_proxy_module

proxy代理模組。

ngx_http_upstream_module

負載均衡模組,實現網站的負載均衡功能機健康檢查。

ngx_http_rewrite_module

URL地址重寫模組。

ngx_http_limit_conn_module

限制用戶並發連接數及請求連接數。

ngx_http_limit_req_module

限制Nginx request processing rate根據定義的key。

ngx_http_log_module

訪問日誌模組,以指定的格式記錄Nginx客戶訪問日誌等資訊。

ngx_http_auth_basic_module

Web認證模組,設置Web用戶通過帳號密碼訪問Nginx。

ngx_http_ssl_module

ssl模組,用於加密的http連接,如https。

四 Nginx變數及狀態碼

4.1 Nginx變數

ngx_http_core_module的內置變數通常有:http請求變數、Nginx內置變數、自定義變數。


$uri:當前請求的URI,不帶參數;


$request_uri:請求的URI,帶完整參數;


$host:http請求報文中的host首部,如果沒有則以處理此請求的虛擬主機的主機名代替;


$hostname:Nginx服務運行所在主機的主機名;


$remote_addr:客戶端IP;


$remote_prot:客戶端埠;


$remote_user:使用用戶認證時客戶端用戶輸入的用戶名;


$request_filename:用戶請求中的URI經過本地root或alias轉換後映射的本地文件路徑;


$request_method:請求方法,GET、POST、PUT;


$server_addr:伺服器地址;


$server_name:伺服器名稱;


$server_port:伺服器埠;


$server_protocol:伺服器向客戶端發送響應時的協議,如http/1.1、http/1.0;


$scheme:在請求中使用scheme。如//xxx.com中的http;


$http_HEADER:匹配請求報文中指定的HEADER;


$http_host:匹配請求報文中的host首部。

4.2 http狀態碼

http狀態碼是用以表示網頁伺服器HTTP響應狀態的3位數字程式碼。可通過查看HTTP狀態碼來判斷伺服器狀態,常見的有404、502等。

  • 301:永久移動,被請求的資源已被永久移動位置;
  • 302:請求的資源限制臨時從不同的URI響應請求;
  • 305:使用代理,被請求的資源必須通過指定的代理才能訪問;
  • 307:臨時跳轉,被請求的資源在臨時從不同的URL響應請求;
  • 400:錯誤請求;
  • 402:需要付款,預留狀態碼,用於將來一些數字貨幣或者微支付;
  • 403:禁止訪問,伺服器已理解請求,但拒絕執行它;
  • 404:找不到對象,請求失敗,資源不存在;
  • 406:不可接受的,請求的資源內容特性無法滿足請求頭部中的條件,因而無法生成響應實體;
  • 408:請求超時;
  • 409:衝突,由於和被請求的資源的當前狀態之間存在衝突,請求無法完成;
  • 410:遺失的,被請求的資源在伺服器上已經不再可用,而且沒有任何已知的轉發地址;
  • 413:響應實體太大,伺服器拒絕處理當前請求,請求超過伺服器所能處理和允許的最大值;
  • 417:期望失敗。在請求頭 Expect 中指定的預期內容無法被伺服器滿足;
  • 418:我是一個茶壺。超文本咖啡罐控制協議,但是並沒有被實際的HTTP伺服器實現;
  • 420:方法失效;
  • 422:不可處理的實體。請求格式正確,但是由於含有語義錯誤,無法響應;
  • 500:伺服器內部錯誤。伺服器遇到了一個未曾預料的狀況,導致了它無法完成對請求的處理;
  • 502:請求後端失敗;
  • 504:請求成功,但是響應超時。