盤點Linux運維常用工具(二)-web篇之nginx

  • 2020 年 3 月 11 日
  • 筆記

1.nginx的概述

1、nginx是一個開源的、支持高性能、高並發的WWW服務和代理服務軟件  2、是由俄羅斯人Igor Sysoev開發的,具有高並發、佔用系統資源少等特性  3、官網:http://nginx.org

 

#特點

1、支持高並發:能支持幾萬並發連接  2、資源消耗少:在3萬並發連接下,開啟10個nginx進程消耗的內存不到200MB  3、開源做HTTP反向代理及加速緩存,即負載均衡  4、具備Squid等專業緩存軟件等的緩存功能  5、支持異步網絡I/O時間模型epoll(Linux2.6+ 內核)

#擴展:異步網絡和同步網絡

#異步網絡:將數據發送到緩衝區就返回,發送成功的消息是通過事件通知的  #同步網絡:收發數據,等到數據真正發送出去或者接收到,才返回

 

#nginx的企業應用

1、作為Web服務軟件  2、反向代理或負載均衡  3、前端業務數據緩存服務          可通過proxy_cache模塊進行緩存

 

#nginx的應用場景

1、使用nginx運行HTML、JS、CSS、小圖片等靜態數據  2、nginx結合FastCGI運行PHP等動態程序  3、nginx結合Tomcat/Resin等支持Java動態程序

 

#nginx軟件使用排名

#查看地址:https://w3techs.com/technologies/overview/web_server/all

#2020年的使用排名

 

2.nginx的安裝

1.編譯安裝  2.rpm安裝

1.rpm安裝

[root@ctos2 ~]# wget -q http://mirrors.ustc.edu.cn/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm  [root@ctos2 ~]# rpm -ivh epel-release-6-8.noarch.rpm  [root@ctos2 ~]# yum install nginx -y #安裝  [root@ctos2 ~]# rpm -qa nginx #查看軟件是否安裝  nginx-1.16.1-1.el7.x86_64

 

2.編譯安裝

[root@ctos3 ~]# yum install  gcc  pcre pcre-devel wget openssl  openssl-devel.x86_64  -y   #安裝相關依賴包  [root@ctos3 ~]# useradd  nginx -s /sbin/nologin -M    [root@ctos3 ~]# mkdir -p /home/demo/tools/  [root@ctos3 ~]# cd /home/demo/tools/  [root@ctos3 tools]# wget http://nginx.org/download/nginx-1.16.0.tar.gz  [root@ctos3 tools]# tar xf nginx-1.16.0.tar.gz  [root@ctos3 tools]# cd nginx-1.16.0/  [root@ctos3 nginx-1.16.0]#   ./configure  --user=nginx --group=nginx   --prefix=/application/nginx --with-http_stub_status_module --with-http_ssl_module  [root@ctos3 nginx-1.16.0]# make -j 2 && make instal #安裝    [root@ctos3 nginx]# /application/nginx/sbin/nginx  -t #查看語法有誤錯誤  nginx: the configuration file /application/nginx/conf/nginx.conf syntax is ok  nginx: configuration file /application/nginx/conf/nginx.conf test is successful    [root@ctos3 nginx]# /application/nginx/sbin/nginx  #啟動服務  [root@ctos3 nginx]# ss -untpl | grep 80 #查看服務是否啟動    [root@ctos3 ~]# /application/nginx/sbin/nginx -V #安裝完後查看版本  nginx version: nginx/1.16.0  built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)  built with OpenSSL 1.0.2k-fips  26 Jan 2017  TLS SNI support enabled  configure arguments: --user=nginx --group=nginx --prefix=/application/nginx --with-http_stub_status_module --with-http_ssl_module

 

#參數介紹

1.yum install openssl-devel  #是為了支持SSL  2.可以使用./configure --help查看相關參數的幫助  3. --prefix=PATH   #設置安裝路徑  4. --user=USER     #進程用戶權限  5. --group=GROUP   #進程用戶組權限  6. --with-http-stub_status_module  #激活狀態信息  7. --with-http_ssl_module #激活ssl功能  8. /application/nginx/sbin/nginx -t  #語法檢查  9. /application/nginx/sbin/nginx     #啟動服務  10. /application/nginx/sbin/nginx -s reload #重啟

 

#查看配置編譯後的配置文件信息

[root@ctos3 ~]# tree /application/nginx/  /application/nginx/  ├── client_body_temp  ├── 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  ├── fastcgi_temp  ├── html  │   ├── 50x.html  │   └── index.html  ├── logs  │   ├── access.log  │   ├── error.log  │   └── nginx.pid  ├── proxy_temp  ├── sbin  │   └── nginx  ├── scgi_temp  └── uwsgi_temp

#提示:

#1.temp結尾的文件是臨時文件

#2.default結尾的文件是備份文件

 

3.nginx的常用模塊

 

 

4.nginx的虛擬主機

1、虛擬主機就是一個獨立的站點,這個站點對應獨立的域名、或者使IP或端口,也有獨立的程序和資源目錄  2、由一定的格式標籤段標記,Apache使用<VirtualHost></VirtualHost>,nginx使用server{} 來標籤一個虛擬主機,也支持多個虛擬主機  3、虛擬主機的官網配置文檔:http://Nginx.org/en/docs/http/request_processing.html

 

#虛擬主機的類型

1.基於域名的虛擬主機  2.基於端口的虛擬主機  3.基於IP的虛擬主機

 

#配置不同類型的虛擬主機

#1.配置基於域名的虛擬主機

[root@ctos3 ~]# cd /application/nginx/conf/  [root@ctos3 conf]# grep -Ev '^$|#' nginx.conf.default > nginx.conf  [root@ctos3 conf]# cat nginx.conf  http {      server {          listen       80;          server_name  www.guoke.com;          location / {              root   html;              index  index.html index.htm;          }      }      server {          listen       80;          server_name  bbs.guoke.com;          location / {              root   html/bbs;              index  index.html index.htm;          }        }  }

#2.配置基於端口的虛擬主機

只需將端口改成不同的就可以了

#3.配置基於IP的虛擬主機

本地有多個IP,然後listen監聽改成是地址,server_name也相應的修改一下

#總結配置虛擬主機的步驟

1、增加一個完整的server標籤段,要放再http裏面  2、更改server_name及root根目 錄  3、創建index.html文件  4、檢查語法然後重啟服務  5、訪問

 

5.nginx的反向代理

反向代理:接收用戶請求代替用戶去後端訪問

#反向代理的重要參數

 

 #例子:為10.1.1.1做域名www.guoke.com的代理,當訪問www.guoke.com就會訪問到10.1.1.1服務器上,也可以寫upstream服務器池

Server {          Listen 80;          Server_name www.guoke.com;          Location / {              Proxy_pass http://10.1.1.1;              Proxy_set_header Host $host;          }  }

 

6.nginx的負載均衡

可以對用戶的訪問請求進行調度處理,對用戶的訪問請求進行壓力分擔

#關鍵參數upstream

#upstream的相關參數說明

server 10.10.1.1:80 weight=2 max_fails=3 fail_timeout=10 backup;

 

#配置例子:為www.guoke.com域名做負載均衡調度

http  {       upstream server{              server 192.168.226.146:80;              server 192.168.226.147:80;      }          server {              listen       80;              server_name www.guoke.com;                #access_log  logs/host.access.log  main;                location / {                  root   html;                  index  index.html index.htm;                  proxy_pass http://server;              }  }

 

#附加負載均衡有關的面試題

nginx有哪幾種調度算法,這幾種區別是什麼      常用的有3種調度算法(輪詢,ip hash,權重)            輪詢是默認的,每個請求按時間順序逐一分配都不同的後端服務,如果後端某台服務器死機就會自動剔除故障系統,讓用戶訪問不受影響            權重:權重的值越大,訪問的概率就越高         iphash:請求按訪問的IP的哈希結果分配,使來自同一個IP的訪客固定訪問一台後端服務器,可以解決會話問題

 

7.nginx的其他相關功能

7.1.別名

別名就是為虛擬主機設置除了主域名以外的一個或多個域名名字    配置方法      在原有的域名上添加server_name www.baidu.com  baidu.com    應用場景  多數企業網站希望訪問www.baidu.com和baidu.com,所瀏覽的事一個頁面

 

7.2.狀態信息功能

Nginx status介紹      模塊為ngx_http_stub_status_module,主要是記錄nginx的基本訪問狀態信息,如果想要添加,在編譯的時候就加入http_stub_status_module  配置:在location / {          stub_status on      }

 

7.3.錯誤日誌

 #常見的日誌級別:[debug|info|notice|warn|error|crit|alert|emerg],級別越高,記錄的信息越少    #error_log的默認值為      #default:  error_log   logs/error.log   error;  #參考資料:http://nginx.org/en/docs/ngx_core_module.html#error_log    #配置      error_log logs/error.log;

 

7.4.訪問日誌

 #access_log  logs/access.log  main;

 

7.5.日誌輪詢切割

默認情況下nginx會把所有的訪問日誌生成到一個指定日誌文件access.log中,但是如果時間長了日誌文件會很大,不利於分析和處理,所以又必要對日誌按天或按小時進行切割    #切割腳本  [root@ctos3 script]# pwd  /script  [root@ctos3 script]# cat cut_ng_log.sh  #!/bin/bash    Dateformat=`date +%Y%m%d`     #定義時間格式  Basedir="application/nginx"   #目錄名  Nginxlogdir="$Basedir/logs"   #日誌目錄  Logname="access_www"          #日誌的名字    [ -d $Nginxlogdir ] && cd $Nginxlogdir  exit 1  [ -f ${Logname}.log ]  exit 1  /bin/mv ${Logname}.log ${Dateformat}_${Logname}.log #放訪問的日誌更名,加上時間  $Basedir/sbin/nginx -s reload #重啟服務    #然後將腳本放進定時任務裏面,每天的00:00執行  [root@ctos3 ~]# cat /etc/crontab  00 00 * * * /bin/sh /script/cut_ng_log.sh > /dev/null

 

7.6.location

location指令的作用是根據用戶請求的URI來執行不同的應用
語法:location [=|~|~*|^~] uri{...}

 

7.7.rewrite

Nginx rewrite的主要功能是實現URL地址重寫
指令語法:rewrite regex replacement[flag]; #例子: server { listen
80; server_name guoke.com; rewrite ^/ (*.)http://www.guoke.com/$1 permanent;
#參數介紹
  rewrite為固定關鍵字
  regex匹配正則表達式
  $1:取前面regex部分括號里的內容
  permanent:301永久跳轉

 

7.8.訪問認證

通常我們會為網站設置一些訪問認證,設置需要用戶認證訪問的,一般主要應用在企業內部人員的訪問地址上,例如企業網站後台

#例子:

#配置基本用戶認證  [root@ctos3 conf]# cat nginx.conf  server {          listen       80;          server_name  localhost;          location / {              root   html;              index  index.html index.htm;              auth_basic "guoke";              auth_basic_user_file /application/nginx/conf/htpasswd;            }          }    #提示:默認沒有htpasswd這個命令,需要安裝httpd才有  [root@ctos3 conf]# yum install httpd -y  [root@ctos3 conf]# which htpasswd  /usr/bin/htpasswd    #創建用戶和密碼  [root@ctos3 conf]# htpasswd  -bc /application/nginx/conf/htpasswd guoke guoke123  Adding password for user guoke    [root@ctos3 conf]# chmod 400 /application/nginx/conf/htpasswd  #修改權限  [root@ctos3 conf]# chown nginx /application/nginx/conf/htpasswd    [root@ctos3 conf]# /application/nginx/sbin/nginx -t #檢查語法  nginx: the configuration file /application/nginx/conf/nginx.conf syntax is ok  nginx: configuration file /application/nginx/conf/nginx.conf test is successful

#訪問效果

 

 #參數講解:

auth_basic:設置認證提示字符串  auth_basic_user_file:用於設置認證的密碼文件