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

1.nginx的概述

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

 

#特色html

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

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

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

 

#nginx的企業應用nginx

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

 

#nginx的應用場景web

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

 

#參數介紹bash

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.訪問日誌

#nginx軟件會把用戶訪問網站的日誌信息記錄到指定的日誌文件裏,給網站提供者參考
#官網地址:http://nginx.org/en/docs/http/ngx_http_log_module.html
#默認參數配置
 #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:用於設置認證的密碼文件
相關文章
相關標籤/搜索