Nginx常用模塊


Nginx虛擬主機

1.虛擬主機方式

  • 基於多IP的方式
  • 基於多端口的方式
  • 基於多域名的方式

2.基於多IP的方式

[root@web02 conf.d]# cat game.conf 
server {
    listen 80;
    server_name 192.168.15.8;
    location / {
		root /opt/Super_Mary;
        index index.html;
    }
}
server {
    listen 80;
    server_name 172.16.1.8;
    location / {
        root /opt/xaingqi;
        index index.html;
    }
}    # 在網頁端輸入192.168.15.8進入的是超級瑪麗,輸入172.16.1.8進入的是象棋

3.基於多端口的方式

[root@web02 conf.d]# cat game1.conf 
server {
        listen 80;
        server_name game001.com;
        location / {
        root /opt/Super_Mary;
        index index.html;
        }
}
server {
        listen 81;
        server_name game001.com;
        location / {
        root /opt/xiangqi;
        index index.html;
        }
}  # 在網頁端輸入game001.com:80進入的是超級瑪麗,輸入game001.com:81進入的是象棋(要在hosts修改域名)

4.基於多域名的方式

[root@web02 conf.d]# cat game2.conf 
server {
     listen 80;
     server_name game001.com;
     location / {
     root /opt/Super_Mary;
     index index.html;
  }
}
server {
     listen 80;
     server_name game002.com;
     location / {
     root /opt/xiangqi;
     index index html;
  }
}  # 在網頁端輸入game001.com進入的是超級瑪麗,輸入game002.com進入的是象棋(要在hosts修改域名)

Nginx日志

1.排查報錯原因

systemctl status nginx.service -l
cat /var/log/nginx/error.log  # 兩個都是查看錯誤日志 能解決百分之90的問題

2.日志參數

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         # 記錄通用的本地時間
$request            # 記錄請求的方法以及請求的http協議
$status             # 記錄請求狀態碼(用於定位錯誤信息)
$body_bytes_sent    # 發送給客戶端的資源字節數,不包括響應頭的大小
$http_referer       # 記錄從哪個頁面鏈接訪問過來的
$http_user_agent    # 記錄客戶端瀏覽器相關信息
$http_x_forwarded_for # 真實的客戶端IP(在反向代理中生效)
# 注:如果Nginx位於負載均衡器,nginx反向代理之后, web服務器無法直接獲取到客 戶端真實的IP地址。
# $remote_addr獲取的是反向代理的IP地址。 反向代理服務器在轉發請求的http頭信息中,    

3.網站的訪問來源

eg:

  1. 修改配置文件(本機ip為172.16.1.8,由vpn啟動,vpn的ip是172.16.1.81)
[root@web02 conf.d]# cat game2.conf 
server {
     listen 80;
     server_name game001.com;
     location / {
     root /opt/Super_Mary;
     index index.html;
  }
}
server {
     listen 80;
     server_name game002.com;
     location / {
     root /opt/xiangqi;
     index index.html;
  }
}  
  1. 域名解析
192.168.15.8    game001.com
172.16.1.8      game002.com
  1. 依次訪問后查看日志
192.168.15.1 - - [04/Jan/2022:15:36:07 +0800] "GET /sounds/stomp.mp3 HTTP/1.1" 404 555 "http://game001.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36" "-"  # 本機的windowip訪問
172.16.1.81 - - [04/Jan/2022:15:34:51 +0800] "GET /img/stype_1/b_m.png HTTP/1.1" 200 2271 "http://game002.com/" "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko" "-"  # vpn的ip訪問
  1. 總結

從公網訪問的話ip是本機window系統的ip

而從內網訪問的話因為是經由vpn訪問,所以訪問ip是vpn的ip

Nginx常用模塊

1.Nginx訪問控制模塊

  • ngx_http_access_module

語法

#允許訪問的語法
Syntax: allow address | all;
Default:    —
Context:    http, server, location, limit_except
 
#拒絕訪問的語法
Syntax: deny address | all;
Default:    —
Context:    http, server, location, limit_except
 
#如果配置允許,則也要配置拒絕;配置拒絕可以單獨配置

# 特別注意!!!   匹配規則是從上往下的,上面的符合就不匹配下面的,不符合就依次往下匹配。

案例

1.允許192.168.15.1訪問,不允許其他IP訪問

[root@web02 conf.d]# vim xiangqi.conf
server {
        listen 80;
        server_name game002.com;
        allow  192.168.15.1;  # 允許192.168.15.1訪問
        deny all;  # 不允許其他IP訪問
        location / {
        root /opt/xiangqi;
        index index.html;
    }
}

image

image

2.允許192.168.15.0這個網段訪問,不允許其他網段訪問

[root@web02 conf.d]# vim xiangqi.conf
server {
        listen 80;
        server_name game002.com;
        allow 192.168.15.0/24;  # 允許192.168.15.0這個網段訪問
        deny all;  # 不允許其他網段訪問
        location / {
        root /opt/xiangqi;
        index index.html;
    }
}
# 知識補充 curl -H'Host:game.com' -I 192.168.15.8 從linux訪問網站。

3.只允許通過VPN來訪問

[root@web02 conf.d]# vim xiangqi.conf
server {
        listen 80;
        server_name game002.com;
        allow 117.16.1.81;  # 允許通過VPN來訪問
        deny all;  # 其他拒絕
        location / {
        root /opt/xiangqi;
        index index.html;
    }
}

image
image

2.Nginx訪問認證模塊

  • ngx_http_auth_basic_module

語法

#開啟的登錄認證
Syntax: auth_basic string | off;
Default:    auth_basic off;  # 默認
Context:    http, server, location, limit_except  # 可使用的地方
 
#指定登錄用的用戶名密碼文件
Syntax: auth_basic_user_file file;
Default:    —
Context:    http, server, location, limit_except

案例

1、安裝httpd-tools
[root@web01 ~]# yum install httpd-tools -y

2、生成用戶名密碼文件
[root@web01 ~]# htpasswd -c /etc/nginx/auth zonghan
New password: 
Re-type new password: 
Adding password for user zonghan

3、將文件路徑加入Nginx配置
[root@web01 ~]# vim /etc/nginx/conf.d/game2.conf
    auth_basic "Welcome To Login";
    auth_basic_user_file /etc/nginx/auth;

4、重啟Nginx
[root@web01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 ~]# systemctl restart nginx

image

3.目錄索引模塊

  • ngx_http_autoindex_module

語法

Syntax: autoindex on | off;
Default:    autoindex off;  # 默認關閉
Context:    http, server, location  # 適用地方
# ngx_http_autoindex_module模塊處理以斜杠字符('/')結尾的請求,並生成目錄列表。
# 當ngx_http_index_module模塊找不到索引文件時,通常會將請求傳遞給ngx_http_autoindex_module模塊。
展示目錄索引
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
autoindex_format html | xml | json | jsonp;

案例

顯示目錄

[root@web02 conf.d]# vim index.conf
server {
        listen 80;
        server_name 192.168.15.8;
        autoindex on;  # 展示目錄索引
        location / {
        root /opt;
   }
}

image

文件大小四舍五入

[root@web02 conf.d]# vim index.conf
server {
        listen 80;
        server_name 192.168.15.8;
        autoindex on;
        autoindex_exact_size off;
        location / {
        root /opt;
   }
}

image

顯示本地時間

[root@web02 conf.d]# vim index.conf
server {
        listen 80;
        server_name 192.168.15.8;
        autoindex on;
        autoindex_exact_size off;
        autoindex_localtime on;
        location / {
        root /opt;
   }
}

image

顯示目錄的格式

[root@web02 conf.d]# vim index.conf
server {
        listen 80;
        server_name 192.168.15.8;
        autoindex on;
        autoindex_exact_size off;
        autoindex_localtime on;
        autoindex_format json;
        location / {
        root /opt;
   }
}  # 有html、xml、json、jsonp格式,默認是html

image

4.控制速率的模塊模塊

  • ngx_http_limit_req_module

語法

#設置空間的語法
Syntax: limit_req_zone key zone=name:size rate=rate [sync];
	 eg:limit_req_zone $remote_addr zone=one:10m rate=1r/s;
	    聲明連接池       變量          名稱  連接池的大小  速率
Default:    —
Context:    http

limit_req_zone          #設置空間的模塊
key                     #空間存儲的內容
zone                    #指定空間
=name                   #空間的名字
:size                   #空間的大小
rate=rate [sync];       #讀寫速率

#調用的語法
Syntax: limit_req zone=name [burst=number] [nodelay | delay=number];
	 eg:limit_req zone=one burst=5;
Default:    —
Context:    http, server, location

limit_req               #調用控件模塊
zone=name               #指定空間=空間的名字
[burst=number]          #允許多請求幾次
[nodelay | delay=number]; #延時

# 特別注意!!!  定義空間語法的時候   如果要作用於location要寫在server內,調用時寫在location,要作用於server要寫在http內,調用時寫在server,要作用於http要寫在全局呢內,調用時寫在http內

案例

要求每秒只能有一個訪問

[root@web02 conf.d]# cat supermary.conf 
limit_req_zone $remote_addr zone=one:10m rate=1r/s;
server {
    listen 80;
    server_name 192.168.15.8;
    limit_req zone=one burst=5;  # 連續刷新5次就503
    location / {
        root /opt/Super_Marie;
	index index.html;
    }
}

image

5.控制訪問連接數模塊

  • ngx_http_limit_conn_module

語法

#設置限制的空間
Syntax: limit_conn_zone key zone=name:size;
     eg:limit_conn_zone $remote_addr zone=addr:10m;
Default:    —
Context:    http
 
limit_conn_zone     #設置空間的模塊
key                 #指定空間存儲的內容
zone                #指定空間
=name               #空間名字
:size;              #空間的大小
 
#調用限制的空間
Syntax: limit_conn zone number;
Default:    —
Context:    http, server, location
 
limit_conn          #調用空間的模塊
zone                #空間的名字
number;             #指定可以同時連接的次數

案例

要求1個ip只有1個連接

[root@web02 conf.d]# cat supermary.conf 
limit_conn_zone $remote_addr zone=addr:10m;
server {
    listen 80;
    server_name 192.168.15.7;
    limit_conn addr 1;  # 調用
    location / {
        root /opt/Super_Mary;
	index index.html;
    }
}

壓力測試

1、安裝ab測試命令
	yum install httpd-tools -y 

	2、ab 參數
		-n : 總共需要訪問多少次
		-c : 每次訪問多少個
eg: ab -n 100000 -c 200 http://192.168.15.8/

6.Nginx狀態監控模塊

  • ngx_http_stub_status_module

語法

Syntax: stub_status;
Default:    —
Context:    server, location

案例

監控Nginx運行狀態

[root@web02 conf.d]# cat game5.conf 
server {
    listen 80;
    server_name 192.168.15.7;
    location / {
        stub_status;
    }
}

image


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM