1. 程式人生 > >linux的nginx的安裝和使用

linux的nginx的安裝和使用

安裝,啟動nginx

1.下載原始碼包
wget -c https://nginx.org/download/nginx-1.12.0.tar.gz
2.解壓縮原始碼
tar -zxvf nginx-1.12.0.tar.gz
3.配置,編譯安裝 開啟nginx狀態監測功能
./configure --prefix=/opt/nginx1-12/ --with-http_ssl_module --with-http_stub_status_module
make && make install
4.啟動nginx,進入sbin目錄,找到nginx啟動命令
cd sbin
./nginx #啟動
./nginx -s stop #關閉
./nginx -s reload #重新載入

安裝完成後檢測服務

netstat -tunlp |grep 80
curl -I 127.0.0.1
#如果訪問不了,檢查selinux,iptables

部署一個web站點

nginx預設站點是Nginx目錄下的html資料夾,這裡可以從nginx.conf中查到

 location /{
            root   html;  #這裡是預設的站點html資料夾,也就是 /opt/nginx1-12/html/資料夾下的內容
            index  index.html index.htm; #站點首頁檔名是index.html
        }

如果要部署網站業務資料,只需要把開發好的程式全放到html目錄下即可

[[email protected]_python /tmp 11:34:52]#ls /opt/nginx1-12/html/
index.html  jssts.jpeg  lhy.mp4  man.jpg  wget-log

因此只需要通過域名/資源,即可訪問

http://www.pyyuc.cn/man.jpg

Nginx的目錄結構

[[email protected]_python /opt/nginx1-12 11:44:02]#ls
client_body_temp  conf  fastcgi_temp  html  logs  proxy_temp  sbin  scgi_temp  static  uwsgi_temp
  • conf 存放nginx所有配置檔案的目錄,主要nginx.conf
  • html 存放nginx預設站點的目錄,如index.html、error.html等
  • logs 存放nginx預設日誌的目錄,如error.log access.log
  • sbin 存放nginx主命令的目錄,sbin/nginx

Nginx主配置檔案解析

Nginx主配置檔案/etc/nginx/nginx.conf是一個純文字型別的檔案,整個配置檔案是以區塊的形式組織的。一般,每個區塊以一對大括號{}來表示開始與結束。

CoreModule核心模組

user www;                       #Nginx程序所使用的使用者
worker_processes 1;             #Nginx執行的work程序數量(建議與CPU數量一致或auto)
error_log /log/nginx/error.log  #Nginx錯誤日誌存放路徑
pid /var/run/nginx.pid          #Nginx服務執行後產生的pid程序號
events事件模組

events {            
    worker_connections  //每個worker程序支援的最大連線數
    use epool;          //事件驅動模型, epoll預設
}
http核心模組

//公共的配置定義在http{}
http {  //http層開始
...    
    //使用Server配置網站, 每個Server{}代表一個網站(簡稱虛擬主機)
    'server' {
        listen       80;        //監聽埠, 預設80
        server_name  localhost; //提供服務的域名或主機名
        access_log host.access.log  //訪問日誌
        //控制網站訪問路徑
        'location' / {
            root   /usr/share/nginx/html;   //存放網站程式碼路徑
            index  index.html index.htm;    //伺服器返回的預設頁面檔案
        }
        //指定錯誤程式碼, 統一定義錯誤頁面, 錯誤程式碼重定向到新的Locaiton
        error_page   500 502 503 504  /50x.html;
    }
    ...
    //第二個虛擬主機配置
    'server' {
    ...
    }
    
    include /etc/nginx/conf.d/*.conf;  //包含/etc/nginx/conf.d/目錄下所有以.conf結尾的檔案

}   //http層結束

Nginx虛擬主機

 

如果每臺linux伺服器只運行了一個小網站,那麼人氣低,流量小的草根站長需要承擔高額的伺服器租賃費,也造成了硬體資源浪費。

虛擬主機就是將一臺伺服器分割成多個“虛擬伺服器”,每個站點使用各自的硬碟空間,由於省資源,省錢,眾多網站都使用虛擬主機來部署網站。

虛擬主機的概念就是在web服務裡的一個獨立的網站站點,這個站點對應獨立的域名(IP),具有獨立的程式和資源目錄,可以獨立的對外提供服務。
這個獨立的站點配置是在nginx.conf中使用server{}程式碼塊標籤來表示一個虛擬主機。
Nginx支援多個server{}標籤,即支援多個虛擬主機站點。

虛擬主機型別

基於域名的虛擬主機
通過不同的域名區分不同的虛擬主機,是企業應用最廣的虛擬主機。

基於埠的虛擬主機
通過不同的埠來區分不同的虛擬主機,一般用作企業內部網站,不對外直接提供服務的後臺,例如www.pythonav.cn:9000

基於IP的虛擬主機
通過不同的IP區分不同的虛擬主機,此類比較少見,一般業務需要多IP的常見都會在負載均衡中繫結VIP

Nginx狀態資訊(status)配置

Nginx狀態資訊(status)配置及資訊詳解
    nginx與php-fpm一樣內建了一個狀態頁,對於想了解nginx的狀態以及監控nginx非常有幫助。為了後續的zabbix監控,我們需要先了解一下nginx的狀態頁。

Nginx狀態資訊(status)介紹
    Nginx軟體在編譯時又一個with-http_stub_status_module模組,這個模組功能是記錄Nginx的基本訪問狀態資訊,讓使用者瞭解Nginx的工作狀態。
要想使用狀態模組,在編譯時必須增加--with-http_stub_status_module引數。

監測你的nginx是否安裝了status模組

[[email protected] conf]# /opt/nginx/sbin/nginx -V
nginx version: nginx/1.12.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC)
configure arguments: --prefix=/opt/nginx/ --with-http_stub_status_module

啟動status狀態功能,修改配置檔案

#在訪問ip/status的時候,進入狀態功能        
location /status {
        #開啟nginx狀態功能 stub_status on; }

平滑重啟nginx

./sbin/nginx -s reload

訪問status頁面

http://192.168.119.10/status

通過ab壓測命令檢測

-n requests #執行的請求數,即一共發起多少請求。

-c concurrency #請求併發數。

-#啟用HTTP KeepAlive功能,即在一個HTTP會話中執行多個請求。

ab -kc 1000 -n 100000 http://192.168.119.10/

 

status頁面解析

 

 

 

 

 

基於域名的多虛擬主機實戰

nginx可以自動識別使用者請求的域名,根據不同的域名請求伺服器傳輸不同的內容,只需要保證伺服器上有一個可用的ip地址,配置好dns解析服務。

/etc/hosts是linux系統中本地dns解析的配置檔案,同樣可以達到域名訪問效果

修改nginx.conf

[[email protected]_python ~ 14:33:16]#egrep -v '#|^$' /opt/nginx1-12/conf/nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  logs/access.log  main;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  www.pyyuc.cn;
        location /{
            root   html/pyyuc;
            index  index.html index.htm;
        }
    }
}

上述程式碼配置了一個www.pyyuc.cn域名的站點,虛擬主機的部分就是server{}裡的內容

建立pyyuc.cn的站點目錄和檔案

[[email protected]_python /opt/nginx1-12/html 14:36:08]#mkdir pyyuc

[[email protected]_python /opt/nginx1-12/html 14:36:18]#echo "<meta charset=utf8>我是pyyuc站點" > pyyuc/index.html
[[email protected]_python /opt/nginx1-12/html 14:37:21]#cat pyyuc/index.html
<meta charset=utf8>我是pyyuc站點

上述作用建立了一個html/pyyuc站點目錄,對應於虛擬主機配置檔案裡的root根目錄的設定html/pyyuc

然後生成一個首頁檔案index.html,內容是“我是pyyuc站點”

檢查nginx語法重新載入nginx

[[email protected]_python /opt/nginx1-12/html 14:37:28]#../sbin/nginx -t
nginx: the configuration file /opt/nginx1-12/conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx1-12/conf/nginx.conf test is successful

#平滑重啟nginx

[[email protected]_python /opt/nginx1-12/html 14:39:18]#../sbin/nginx -s reload

檢查nginx埠,程序,訪問pyyuc虛擬站點

[[email protected]_python /opt/nginx1-12/html 14:40:02]#netstat -tunlp|grep nginx
[[email protected]_python /opt/nginx1-12/html 14:40:29]#ps -ef|grep nginx

#我這裡是有dns解析,沒有的話則需要/etc/hosts解析
#成功配置了pyyuc虛擬主機站點
[[email protected]_python /opt/nginx1-12/html 14:41:37]#curl www.pyyuc.cn
<meta charset=utf8>我是pyyuc站點

配置多個域名的虛擬主機

其實就是新增一個server{}虛擬主機

egrep -v '#|^$' /opt/nginx1-12/conf/nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  logs/access.log  main;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  www.pyyuc.cn;
        location /{
            root   html/pyyuc;
            index  index.html index.htm;
        }
}
    server {
        listen       80;
        server_name  www.pythonav.cn;
        location /{
            root   html/pythonav;
            index  index.html index.htm;
        }
}
    }

建立pythonav虛擬主機站點的目錄和檔案

[[email protected]_python /opt/nginx1-12 14:47:21]#mkdir -p /opt/nginx1-12/html/pythonav
[[email protected]_python /opt/nginx1-12 14:49:33]#echo "<meta charset=utf8>我是pythonav,未成年禁止入內"> /opt/nginx1-12/html/pythonav/index.html
[[email protected]_python /opt/nginx1-12 14:50:44]#./sbin/nginx -t
nginx: the configuration file /opt/nginx1-12/conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx1-12/conf/nginx.conf test is successful
[[email protected]_python /opt/nginx1-12 14:51:32]#./sbin/nginx -s reload

大功告成,基於域名的虛擬主機實戰搞定

[[email protected]_python /opt/nginx1-12 14:52:12]#curl www.pythonav.cn
<meta charset=utf8>我是pythonav,未成年禁止入內
[[email protected]_python /opt/nginx1-12 14:52:40]#curl www.pyyuc.cn
<meta charset=utf8>我是pyyuc站點

nginx訪問日誌(access_log)

日誌功能對每個使用者訪問網站的日誌資訊記錄到指定的日誌檔案裡,開發運維人員可以分析使用者的瀏覽器行為,此功能由ngx_http_log_module模組負責,官網地址是:

http://nginx.org/en/docs/http/ngx_http_log_module.html

控制日誌的引數

log_format    記錄日誌的格式,可定義多種格式
accsss_log    指定日誌檔案的路徑以及格式

  log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  '$status $body_bytes_sent "$http_referer" '
  '"$http_user_agent" "$http_x_forwarded_for"';

對應引數解析

$remote_addr    記錄客戶端ip
$remote_user    遠端使用者,沒有就是 “-”
$time_local    對應[14/Aug/2018:18:46:52 +0800]
$request     對應請求資訊"GET /favicon.ico HTTP/1.1"
$status     狀態碼
$body_bytes_sent  571位元組 請求體的大小
$http_referer  對應“-”  由於是直接輸入瀏覽器就是 -
$http_user_agent  客戶端身份資訊
$http_x_forwarded_for  記錄客戶端的來源真實ip 97.64.34.118

日誌效果如下

66.102.6.6 - - [14/Aug/2018:18:46:52 +0800] "GET /favicon.ico HTTP/1.1" 404 571 "-"

"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.75 Safari/537.36 Google Favicon" "97.64.34.118"

 nginx.conf預設配置

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  logs/access.log  main;

日誌格式配置定義

log_format是日誌關鍵字引數,不能變
main是日誌格式指定的標籤,記錄日誌時通過main標籤選擇指定的格式。 

nginx限制網站來源IP訪問

如果哪天發現你的nginx很慢,或者檢查access.log時候,有一個some body瘋狂請求你的nginx server,那麼可以禁止這個IP訪問
限制ip或ip段訪問
禁止訪問/av/底下的資源
location /av { deny 122.71.240.254; #alias /opt/nginx1-12/html/av; allow 10.1.1.0/16; }

 

Nginx錯誤頁面優化

在網站執行過程中,可能因為頁面不存在等原因,導致網站無法正常響應請求,此時web服務會返回系統的錯誤碼,但是預設的錯誤頁面很不友好。

 

因此我們可以將404,403等頁面的錯誤資訊重定向到網站首頁或者其他指定的頁面,提升使用者訪問體驗。

server {
        listen       80;
        server_name  www.pythonav.cn;
        root html/pythonav;
        location /{
            index  index.html index.htm;
        }
      #在pythonav路徑下的40x.html錯誤頁面 error_page 400 403 404 405 /40x.html; }

40x.html

<img style='width:100%;height:100%;' src=https://pic1.zhimg.com/80/v2-77a9281a2bebc7a2ea5e02577af266a8_hd.png>

此時訪問www.pythonav.cn/asdasd錯誤頁面已經優化了

 

Nginx代理

正向代理

正向代理,也就是傳說中的代理,他的工作原理就像一個跳板(VPN),簡單的說:

我是一個使用者,我訪問不了某網站,但是我能訪問一個代理伺服器,這個代理伺服器呢,他能訪問那個我不能訪問的網站,於是我先連上代理伺服器,告訴他我需要那個無法訪問網站的內容,代理伺服器去取回來,然後返回給我。

 

反向代理

對於客戶端而言,代理伺服器就像是原始伺服器。

nginx實現負載均衡的元件

ngx_http_proxy_module    proxy代理模組,用於把請求拋給伺服器節點或者upstream伺服器池

實現一個簡單的反向代理

機器準備,兩臺伺服器

master  192.168.11.63  主負載
slave 192.168.11.64  web1

主負載均衡節點的配置檔案

worker_processes  1;
error_log  logs/error.log;
pid        logs/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  logs/access.log  main;
    sendfile        on;
    keepalive_timeout  65;
        upstream slave_pools{
    server 192.168.11.64:80 weight=1;
}
    server {
        listen       80;
        server_name  localhost;
        location / {
        proxy_pass  http://slave_pools;
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
nginx.conf

檢查語法並啟動nginx

[[email protected] 192.168.11.63 /opt/nginx1-12]$/opt/nginx1-12/sbin/nginx -t
nginx: the configuration file /opt/nginx1-12/conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx1-12/conf/nginx.conf test is successful
#啟動nginx
[[email protected] 192.168.11.63 /opt/nginx1-12]$/opt/nginx1-12/sbin/nginx
#檢查埠
[[email protected] 192.168.11.63 /opt/nginx1-12]$netstat -tunlp|grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 8921/nginx: master

此時訪問master的伺服器192.168.11.63:80地址,已經會將請求轉發給slave的80埠

除了頁面效果的展示以外,還可以通過log(access.log)檢視代理效果

master端日誌

slave端日誌

參考:https://www.cnblogs.com/pyyu/p/9468680.html

Nginx負載均衡概述

Web伺服器,直接面向用戶,往往要承載大量併發請求,單臺伺服器難以負荷,我使用多臺WEB伺服器組成叢集,前端使用Nginx負載均衡,將請求分散的打到我們的後端伺服器叢集中,
實現負載的分發。那麼會大大提升系統的吞吐率、請求效能、高容災

 

Nginx要實現負載均衡需要用到proxy_pass代理模組配置

Nginx負載均衡與Nginx代理不同地方在於

Nginx代理僅代理一臺伺服器,而Nginx負載均衡則是將客戶端請求代理轉發至一組upstream虛擬服務池

Nginx可以配置代理多臺伺服器,當一臺伺服器宕機之後,仍能保持系統可用。

 upstream配置

在nginx.conf > http 區域中

upstream django {
       server 10.0.0.10:8000;
       server 10.0.0.11:9000;
}

在nginx.conf > http 區域 >  server區域  > location配置中

新增proxy_pass

location / {
            root   html;
            index  index.html index.htm;
            proxy_pass http://django;
}

此時初步負載均衡已經完成,upstream預設按照輪訓方式負載,每個請求按時間順序逐一分配到後端節點。

upstream分配策略

weight 權重

upstream django {
       server 10.0.0.10:8000 weight=5;
       server 10.0.0.11:9000 weight=10;#這個節點訪問比率是大於8000的
}

ip_hash

每個請求按訪問ip的hash結果分配,這樣每個訪客固定訪問一個後端伺服器
upstream django {
    ip_hash; server 10.0.0.10:8000; server 10.0.0.11:9000; }

backup

在非backup機器繁忙或者宕機時,請求backup機器,因此機器預設壓力最小

upstream django {
       server 10.0.0.10:8000 weight=5;
       server 10.0.0.11:9000;
       server node.oldboy.com:8080 backup;
}

負載均衡實驗環境規劃

角色            ip                    主機名
lb01        192.168.119.10        lb01    
web01        192.168.119.11        web01
web02        192.168.119.12        web02

關閉防火牆

iptables -F
sed  -i 's/enforcing/disabled/' /etc/selinux/config

systemctl stop firewalld
systemctl disable firewalld

一、web01伺服器配置nginx,建立index.html

server {
        listen       80;
        server_name  192.168.119.11;
        location / {
        root /node;
            index  index.html index.htm;
        }
}

mkdir /node
echo 'i am web01' > /node/index.html

#啟動NGINX
./sbgin/nginx

二、web01伺服器配置nginx,建立index.html

server {
    listen       80;
    server_name  192.168.119.12;
    location / {
        root /node;
        index  index.html index.htm;
}


mkdir /node
echo 'i am web02...' > /node/index.html
#啟動nginx
./sbing/nginx

三、配置lb01伺服器的nginx負載均衡

1.檢查lb01的 nginx.conf

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    upstream node {
      server 192.168.119.11:80;
      server 192.168.119.12:80;
}
    server {
        listen       80;
        server_name 192.168.119.10;
        location / {
          proxy_pass http://node;
          include proxy_params;  #需要手動建立
        }
    }
}

2.手動建立proxy_params檔案,檔案中存放代理的請求頭相關引數

[[email protected] conf]# cat /opt/nginx/conf/proxy_params
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;

proxy_buffering on;
proxy_buffer_size 32k;
proxy_buffers 4 128k;
啟動lb01負載均衡nginx服務

./sbin/nginx

四、訪問lb01節點nginx,反覆重新整理

五、nginx負載均衡排程演算法

排程演算法      概述
輪詢        按時間順序逐一分配到不同的後端伺服器(預設)
weight       加權輪詢,weight值越大,分配到的訪問機率越高
ip_hash      每個請求按訪問IP的hash結果分配,這樣來自同一IP的固定訪問一個後端伺服器
url_hash      按照訪問URL的hash結果來分配請求,是每個URL定向到同一個後端伺服器
least_conn    最少連結數,那個機器連結數少就分發

1.輪詢(不做配置,預設輪詢)

2.weight權重(優先順序)

3.ip_hash配置,根據客戶端ip雜湊分配,不能和weight一起用

六、nginx動靜分離負載均衡

 

環境準備 

 

系統                 服務                軟體                ip地址
centos7(lb01)                負載均衡            nginx proxy        192.168.119.10
centos7(web01)                靜態資源            nginx靜態資源        192.168.119.11
centos7(web02)                動態資源            django            192.168.119.1

一、在web01機器上,配置靜態資源,圖片等

cat nginx.conf


server {
        listen       80;
        server_name  192.168.119.11;
        #定義網頁根目錄
         root /code;
        #定義了靜態資源
        index index.html;
#域名匹配,所有的png、jpg、gif請求資源,都去/root/code/images底下找
         location ~* .*\.(png|jpg|gif)$ {
                root /code/images;
        }    

#重啟nginx
./sbin/nginx
#建立目錄
mkdir -p /code/images
#準備首頁檔案
[[email protected]  /code]$cat index.html
static files...
#準備靜態檔案,圖片
[[email protected]  /code/images]$wget http://pythonav.cn/av/girlone.jpg^C
[[email protected]  /code/images]$ls
girlone.jpg

二、在web02配置動態請求,準備一個flask程式和靜態資源轉發

cat  nginx.conf

#靜態資源地址
upstream static {
server 192.168.119.11:80;
}
#flask動態請求 upstream flask { server 192.168.119.12:8080; }
server { listen 80; server_name 192.168.119.12;
      #當請求到達192.168.119.12:80/時,轉發給flask的8080應用 location / { proxy_pass http://flask; include proxy_params; }
      #當判斷資源請求是 192.168.119.12/girl.jpg時候,轉發請求給static地址池的伺服器192.168.119.11/ location ~ .*\.(png|jpg|gif)$ {         proxy_pass http://static; include proxy_params; }

準備flask應用,flask.py

from flask import Flask
app=Flask(__name__)
@app.route('/')
def hello():
    return "i am flask....from nginx"
if __name__=="__main__":
    app.run(host='0.0.0.0',port=8080)

後臺執行flask程式

python flask-web.py &

三、在負載均衡伺服器lb01上測試訪問192.168.119.10

參考:https://www.cnblogs.com/pyyu/p/10004681.html

總結:

1.yum解決編譯nginx所需的依賴包,之後你的nginx就不會報錯了
    cd /opt/
    yum install gcc patch libffi-devel python-devel  zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel openssl openssl-devel -y

2.安裝配置nginx軟體,下載原始碼
    wget -c https://nginx.org/download/nginx-1.12.0.tar.gz
3.解壓縮原始碼,編譯且安裝
    tar -zxvf nginx-1.12.0.tar.gz 
    切換原始碼目錄
    ./configure --prefix=/opt/nginx112/
    make && make install 
4.進入nginx的工作目錄
cd /opt/nginx112/

5.檢視gninx的工作目錄
[[email protected] nginx112]# ls
conf  配置檔案目錄
html  網頁根目錄,你的index.html就放在這裡,然後通過域名訪問  pythonav.cn/index.html     html/index.html 
logs    日誌
sbin    存放nginx可執行命令的


6.定製自己的nginx網站
修改/opt/nginx112/html/index.html  這是nginx網頁根檔案,清空內容寫入自己的html標籤

7.啟動nginx伺服器
/opt/nginx112/sbin/nginx    直接回車執行 

8.檢查nginx服務埠
ps -ef|grep nginx 

9.通過windows訪問nginx web服務
瀏覽器 訪問http://192.168.13.79

 

nginx
1.編譯安裝配置完成
/opt/nginx11/html/index.html   這是網頁的首頁檔案

2.    nginx.conf主配置檔案學習
######################################如下
worker_processes  4;   nginx工作程序數,根據cpu的核數定義
events {
    worker_connections  1024;    #連線數
}
#http區域塊,定義nginx的核心web功能
http {
    include(關鍵字)       mime.types(可修改的值);
    default_type  application/octet-stream;
    
    #定義日誌格式
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    #開啟訪問日誌功能的引數          
    access_log  logs/access.log  main;
    sendfile        on;
    #tcp_nopush     on;
    #keepalive_timeout  0;
    #保持長連線
    keepalive_timeout  65;
    #支援圖片 gif等等壓縮,減少網路頻寬
    gzip  on;
    
    #這個server標籤 控制著nginx的虛擬主機(web站點)
    server {
        # 定義nginx的入口埠是80埠
        listen       80;
        # 填寫域名,沒有域名就寫ip地址
        server_name  www.s15rihan.com;
        # 定義編碼
        charset utf-8;
        # location定義網頁的訪問url
        #就代表 使用者的請求 是  192.168.13.79/
        location / {
            #root引數定義網頁根目錄
            root   html;
            #定義網頁的首頁檔案,的名字的
            index  index.html index.htm;
        }
        #定義錯誤頁面,客戶端的錯誤,就會返回40x系列錯誤碼
        error_page  404  403 401 400            /404.html;
        #500系列錯誤代表後端程式碼出錯
        error_page   500 502 503 504  /50x.html;
    }
    #在另一個server{}的外面,寫入新的虛擬主機2
    server{
        listen 80;
        server_name  www.s15oumei.com;
        location /  {
        root  /opt/myserver/oumei;        #定義虛擬主機的網頁根目錄
        index  index.html;
        }
    }
}

3.準備兩個虛擬主機的網頁根目錄內容
    [[email protected] myserver]# tree /opt/myserver/
        /opt/myserver/
        ├── oumei
        │   └── index.html        寫入自己的內容
        └── rihan
            └── index.html        寫入自己的內容 
        
4.修改windows本地的測試域名  C:\Windows\System32\drivers\etc\hosts檔案
寫入如下內容

192.168.13.79 www.s15rihan.com  

192.168.13.79 www.s15oumei.com  

    因為我們沒有www.s15oumei.com 也沒有  www.s15rihan.com ,因此要在本地搞一個測試域名,
    不想改dns的話,就去阿里雲去買一個域名~~~~~~~~~~~~~~~~~~~~~~
    
5.然後在瀏覽器測試訪問 兩個不同的 web站點

www.s15rihan.com  

www.s15oumei.com   

nginx的訪問日誌功能
1.開啟nginx.conf中的日誌引數

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    #開啟訪問日誌功能的引數          
    access_log  logs/access.log  main;
    
    
2.檢查access.log的日誌資訊

tail -f  access.log 



nginx的拒絕訪問功能
1.在nginx.conf中,新增引數
在server{}虛擬主機標籤中,找到location 然後新增引數

        #當趙一寧訪問  192.168.13.79/  的時候 
        location / {
            #拒絕引數是 deny 
            #deny 寫你想拒絕的IP地址
            #deny還支援拒絕一整個網站
            deny  192.168.13.33;
            root   /opt/myserver/rihan;
            index  index.html;
        }


nginx的錯誤頁面優化
1.修改nginx.conf 中的配置引數
這個s1540x.html存在 虛擬主機定義的網頁根目錄下
  error_page  404              /s1540x.html;
 

vpn就是正向代理
中國的使用者,在自己機器上,使用了一個vpn的ip地址,然後通過這個vpn的IP地址和外接通訊

nginx的反向代理功能(自帶了反向代理的功能,天生的二道販子)
1.實驗環境準備
準備2個伺服器,都安裝好nginx軟體
    nginx1        192.168.13.79   作為web伺服器 (理解為火車票售票點)

    nginx2        192.168.13.24    作為反向代理伺服器        (黃牛)
    
    使用者   通過瀏覽器去訪問   黃牛 (代理)
    瀏覽器 訪問  192.168.13.24    >        192.168.13.79

2.在反向代理伺服器中新增配置


nginx負載均衡

叢集的概念:一堆伺服器做一件事


1.實驗準備
準備三臺計算機 

nginx1      192.168.13.121   作為nginx負載均衡器                只要我訪問這個負載均衡器,檢視頁面的結果,到底是來自於

nginx2      192.168.13.24    web服務,提供一個頁面        

nginx3         192.168.13.79  web服務,提供一個頁面 



2.先配置兩個nginx  web頁面  
    192.168.13.24  準備一個   index.html  寫入  你好,我是192.168.13.24機器
    192.168.13.79    準備一個    index.html 寫入        老了老弟,我是192.168.13.79
    
    然後啟動兩個nginx web 服務
    
3.準備一個nginx負載均衡器  192.168.13.121機器上,修改nginx.conf 
寫入如下內容 
            定義一個負載均衡池,負載均衡的演算法有
            排程演算法      概述
            輪詢        按時間順序逐一分配到不同的後端伺服器(預設)
            weight       加權輪詢,weight值越大,分配到的訪問機率越高
            ip_hash      每個請求按訪問IP的hash結果分配,這樣來自同一IP的固定訪問一個後端伺服器
            url_hash      按照訪問URL的hash結果來分配請求,是每個URL定向到同一個後端伺服器
            least_conn    最少連結數,那個機器連結數少就分發

            1.輪詢(不做配置,預設輪詢)

            2.weight權重(優先順序)

            3.ip_hash配置,根據客戶端ip雜湊分配,不能和weight一起用

upstream s15webserver  {
ip_hash;
server 192.168.13.79 ;
server 192.168.13.24 ;
}
 
然後在虛擬主機中新增 反向代理配置,將使用者的請求,直接轉發給 負載均衡池中的伺服器

server {
        listen       80;
        #當我的請求來自於 192.168.13.121時,走這>個虛擬主機
        server_name  192.168.13.121;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        #核心配置,就在這,一條proxy_psss引數即可
        location / {
          proxy_pass http://s15webserver;
            #root   html;
            #index  index.html index.htm;
        }

}

4.啟動負載均衡器的 nginx服務 

5.在客戶端windows中測試訪問,負載均衡器  192.168.13.121 ,檢視請求分發的結果