国产成人精品无码青草_亚洲国产美女精品久久久久∴_欧美人与鲁交大毛片免费_国产果冻豆传媒麻婆精东

15158846557 在線咨詢 在線咨詢
15158846557 在線咨詢
所在位置: 首頁 > 營銷資訊 > 網(wǎng)站運營 > 第九篇:nginx模塊擴展

第九篇:nginx模塊擴展

時間:2023-07-01 22:12:02 | 來源:網(wǎng)站運營

時間:2023-07-01 22:12:02 來源:網(wǎng)站運營

第九篇:nginx模塊擴展:

一、nginx回顧

1.安裝

1.epol源安裝2.官方源安裝3.源碼包安裝 1)下載 2)解壓 3)生成 4)編譯 5)安裝

2.nginx配置文件

[root@web01 ~]# cat /etc/nginx/nginx.conf##################核心模塊###############user www;worker_processes 1;error_log /var/log/nginx/error.log warn;pid /var/run/nginx.pid;#################事件驅(qū)動模塊###########events { worker_connections 1024;}################http內(nèi)核模塊############http { include /etc/nginx/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 /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #tcp_nodelay on; #gzip on; include /etc/nginx/conf.d/*.conf; server { listen 80; server_name localhost; charset utf8; #access_log /var/log/nginx/host.access.log main; location / { root /usr/share/nginx/html; index index.html index.htm; } location /download { root /usr/share/nginx/html; index index.html index.htm; } }}

二、Nginx虛擬主機

1.虛擬主機方式

1.基于多IP的方式2.基于多端口的方式3.基于多域名的方式

2.基于多IP的方式

0)網(wǎng)卡添加子IP

[root@web01 ~]# ifconfig eth0:1 10.0.0.3/24[root@web03 ~]# ip addr add 10.0.0.91/24 dev eth0

1)第一個配置文件

[root@web03 /etc/nginx/conf.d]# vim addr1.conf server { listen 10.0.0.9:80; location / { root /code/addr1; index index.html; }}

2)第二個配置文件

[root@web03 /etc/nginx/conf.d]# vim addr2.conf server { listen 10.0.0.91:80; location / { root /code/addr2; index index.html; }}

3)根據(jù)配置文件配置環(huán)境

[root@web03 /etc/nginx/conf.d]# mkdir /code[root@web03 /etc/nginx/conf.d]# mkdir /code/addr1[root@web03 /etc/nginx/conf.d]# mkdir /code/addr2[root@web03 /etc/nginx/conf.d]# echo "9" >/code/addr1/index.html[root@web03 /etc/nginx/conf.d]# echo "91" >/code/addr2/index.html

4)檢查配置

[root@web01 ~]# nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful

5)重啟訪問

[root@web01 ~]# systemctl restart nginxhttp://10.0.0.7/http://10.0.0.3/

3.基于多端口的方式

1)第一個配置文件

[root@web01 ~]# cat /etc/nginx/conf.d/game.confserver { listen 80; server_name localhost; location / { root /code/tuixiangzi; index index.html; }}

2)第二個配置文件

[root@web01 ~]# cat /etc/nginx/conf.d/gametwo.confserver { listen 81; server_name localhost; location / { root /code/tank; index index.html; }}

3)檢查配置

[root@web01 ~]# nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful

4)重啟訪問

[root@web01 ~]# systemctl restart nginxhttp://10.0.0.7/http://10.0.0.7:81/

4.基于多域名的方式

1)第一個配置文件

[root@web01 ~]# cat /etc/nginx/conf.d/game.confserver { listen 80; server_name www.tuixiangzi.com; location / { root /code/tuixiangzi; index index.html; }}

2)第二個配置文件

[root@web01 ~]# cat /etc/nginx/conf.d/gametwo.confserver { listen 80; server_name www.tank.com; location / { root /code/tank; index index.html; }}

3)檢查配置

[root@web01 ~]# nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful

4)重啟

[root@web01 ~]# systemctl restart nginx

5)配置本地hosts訪問

#修改windows的hosts文件C:/Windows/System32/drivers/etc/hosts10.0.0.7 www.tuixiangzi.com www.tank.com#訪問http://www.tuixiangzi.com/http://www.tank.com/

總結(jié):

? 在nginx的虛擬主機類型中,基于域名的虛擬主機應用最為廣泛。

1,基于端口和IP的虛擬主機類型,用戶體驗不好。2,基于IP類型的虛擬主機,如在公網(wǎng)環(huán)境下使用,會產(chǎn)生額外費用。3,基于域名的虛擬主機,一次付費,用戶體驗較好。不管使用哪種虛擬主機,最終使用的都是本地主機現(xiàn)有資源。

5.日志配置

1)第一個配置

[root@web01 ~]# cat /etc/nginx/conf.d/game.conf server { listen 80; server_name www.tuixiangzi.com; access_log /var/log/nginx/www.tuixiangzi.com.log main; #訪問日志以main格式存放到目標地址 location / { root /code/tuixiangzi; index index.html; }}

2)第二個配置

[root@web01 ~]# cat /etc/nginx/conf.d/gametwo.conf server { listen 80; server_name www.tank.com; access_log /var/log/nginx/www.tank.com.log main; location / { root /code/tank; index index.html; }}

3)重啟訪問測試

[root@web01 ~]# nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful[root@web01 ~]# systemctl restart nginx[root@web01 ~]# ll /var/log/nginx/total 136-rw-r-----. 1 nginx adm 103660 Nov 27 09:31 access.log-rw-r-----. 1 nginx adm 21972 Nov 27 09:36 error.log-rw-r--r--. 1 root root 666 Nov 27 09:36 www.tank.com.log-rw-r--r--. 1 root root 190 Nov 27 09:36 www.tuixiangzi.com.log總結(jié):nginx運行優(yōu)先遵循server內(nèi)配置,再遵循h(huán)ttp..。所以日志可以分類儲存

三、nginx日志

Nginx有非常靈活的日志記錄模式,每個級別的配置可以有各自獨立的訪問日志。日志格式通過log_format命令定義格式

1.log_format語法

Syntax: log_format name [escape=default|json|none] string ...;Default: log_format combined "...";Context: http

2.默認日志格式

log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; 10.0.0.1 - - [27/Nov/2020:09:36:08 +0800] "GET /images/tank.ico HTTP/1.1" 200 25214 "http://www.tank.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36" "-"10.0.0.1 - - [2020-11-27T10:13:58+08:00] "GET /images/modewin/help0.png HTTP/1.1" 200 27947 "http://www.tank.com/css/tank.css" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36" "-"

3.日志常用變量

$remote_addr # 記錄客戶端IP地址$remote_user # 記錄客戶端用戶名$time_local # 記錄通用的本地時間$time_iso8601 # 記錄ISO8601標準格式下的本地時間$request # 記錄請求的方法以及請求的http協(xié)議$status # 記錄請求狀態(tài)碼(用于定位錯誤信息)$body_bytes_sent # 發(fā)送給客戶端的資源字節(jié)數(shù),不包括響應頭的大小$bytes_sent # 發(fā)送給客戶端的總字節(jié)數(shù)$msec # 日志寫入時間。單位為秒,精度是毫秒。$http_referer # 記錄從哪個頁面鏈接訪問過來的$http_user_agent # 記錄客戶端瀏覽器相關(guān)信息$http_x_forwarded_for #記錄經(jīng)過的所有服務器的IP地址$X-Real-IP #記錄起始的客戶端IP地址和上一層客戶端的IP地址$request_length # 請求的長度(包括請求行, 請求頭和請求正文)。$request_time # 請求花費的時間,單位為秒,精度毫秒# 注:如果Nginx位于負載均衡器,nginx反向代理之后, web服務器無法直接獲取到客 戶端真實的IP地址。# $remote_addr獲取的是反向代理的IP地址。 反向代理服務器在轉(zhuǎn)發(fā)請求的http頭信息中,# 增加X-Forwarded-For信息,用來記錄客戶端IP地址和客戶端請求的服務器地址。

4.nginx日志切割

[root@web01 ~]# vim /etc/logrotate.d/nginx #指定要切割的日志/var/log/nginx/*.log { daily #每天切割日志 missingok #忽略日志丟失 rotate 52 #日志保留時間 52天 compress #日志壓縮 delaycompress #延時壓縮 not if empty #不切割空日志 create 640 nginx adm #切割好的日志權(quán)限 sharedscripts #開始執(zhí)行腳本 postrotate #標注腳本內(nèi)容 if [ -f /var/run/nginx.pid ]; then#判斷nginx啟動 kill -USR1 `cat /var/run/nginx.pid` #重新生成一個access.log fi endscript #腳本執(zhí)行完畢}

四、nginx常用模塊

1.目錄索引模塊

# ngx_http_autoindex_modulengx_http_autoindex_module模塊處理以斜杠字符('/')結(jié)尾的請求,并生成目錄列表。當ngx_http_index_module模塊找不到索引文件時,通常會將請求傳遞給ngx_http_autoindex_module模塊。

1)語法

Syntax: autoindex on | off;Default: autoindex off;Context: http, server, location

2)配置

[root@web01 ~]# vim /etc/nginx/conf.d/www.autoindex.com.conf server { listen 80; server_name www.autoindex.com; charset utf8; location / { root /code/autoindex; autoindex on; }}

3)訪問網(wǎng)站正常,加download跳轉(zhuǎn)目錄頁面

[root@web01 ~]# vim /etc/nginx/conf.d/www.autoindex.com.conf server { listen 80; server_name www.autoindex.com; charset utf8; location / { root /code/autoindex; index index.html; } location /download { root /code/autoindex; autoindex on; }}#創(chuàng)建站點目錄[root@web01 ~]# mkdir /code/autoindex/download -p[root@web01 ~]# echo "測試autoindex模塊" > /code/autoindex/index.html#訪問http://www.autoindex.com/ 為主站http://www.autoindex.com/download/ 為下載文件的目錄

4)常用優(yōu)化參數(shù)

#顯示文件字節(jié)大小,默認是顯示字節(jié)大小,配置為off之后,顯示具體大小 M/G/KSyntax: autoindex_exact_size on | off;Default: autoindex_exact_size on;Context: http, server, location#顯示文件的修改的具體時間,默認顯示的時間與真實時間相差8小時,所以配置 onSyntax: autoindex_localtime on | off;Default: autoindex_localtime off;Context: http, server, location

5)完整配置

[root@web01 ~]# cat /etc/nginx/conf.d/www.autoindex.com.conf server { listen 80; server_name www.autoindex.com; charset utf8; location / { root /code/autoindex; index index.html; } location /download { root /code/autoindex; autoindex on; autoindex_exact_size off; autoindex_localtime on; }}

2.Nginx訪問控制模塊

#ngx_http_access_module

1)語法

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

2)配置訪問控制示例

1>拒絕指定的IP,其他全部允許

[root@web01 ~]# vim /etc/nginx/conf.d/www.autoindex.com.conf server { listen 80; server_name www.autoindex.com; charset utf8; location / { root /code/autoindex; index index.html; } location /download { root /code/autoindex; autoindex on; autoindex_exact_size off; autoindex_localtime on; deny 10.0.0.1; allow all; }}

2>只允許指定IP能訪問, 其它全部拒絕

[root@web01 ~]# vim /etc/nginx/conf.d/www.autoindex.com.conf server { listen 80; server_name www.autoindex.com; charset utf8; location / { root /code/autoindex; index index.html; } location /download { root /code/autoindex; autoindex on; autoindex_exact_size off; autoindex_localtime on; allow 10.0.0.1; #如果使用all,一定放在最后面 deny all; }}

3>只允許10.0.0.1訪問,拒絕該網(wǎng)段其他IP

[root@web01 ~]# vim /etc/nginx/conf.d/www.autoindex.com.conf server { listen 80; server_name www.autoindex.com; charset utf8; location / { root /code/autoindex; index index.html; } location /download { root /code/autoindex; autoindex on; autoindex_exact_size off; autoindex_localtime on; allow 10.0.0.1; #如果使用all,一定放在最后面 deny 10.0.0.0/24; }}

3.Nginx訪問認證模塊

# ngx_http_auth_basic_module

1)語法

#開啟的登錄認證,沒有卵用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

2)創(chuàng)建密碼文件

#創(chuàng)建密碼文件需要用到 htpasswd[root@web01 ~]# htpasswd -c /etc/nginx/auth_basic lhdNew password: Re-type new password: Adding password for user lhd#添加一個登錄用戶[root@web01 ~]# htpasswd /etc/nginx/auth_basic egonNew password: Re-type new password: Adding password for user egon#密碼文件內(nèi)容[root@web01 ~]# cat /etc/nginx/auth_basiclhd:$apr1$A7d4BWYe$HzlIA7pjdMHBDJPuLBkvd/egon:$apr1$psp0M3A5$601t7Am1BG3uINvuBVbFV0

3)配置訪問登錄

[root@web01 ~]# vim /etc/nginx/conf.d/www.autoindex.com.conf server { listen 80; server_name www.autoindex.com; charset utf8; access_log /var/log/nginx/www.autoindex.com.log main; location / { root /code/autoindex; index index.html; } location /download { root /code/autoindex; autoindex on; autoindex_exact_size off; autoindex_localtime on; auth_basic "性感荷官在線發(fā)牌?。?!"; auth_basic_user_file /etc/nginx/auth_basic; }}

4.Nginx狀態(tài)監(jiān)控模塊

# ngx_http_stub_status_modulengx_http_stub_status_module模塊提供對nginx基本狀態(tài)信息的訪問。默認情況下不構(gòu)建此模塊,應使用--with-http_stub_status_module配置參數(shù)啟用它

1)語法

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

2)配置

[root@web01 ~]# vim /etc/nginx/conf.d/www.autoindex.com.conf server { listen 80; server_name www.autoindex.com; charset utf8; access_log /var/log/nginx/www.autoindex.com.log main; location / { root /code/autoindex; index index.html; } location /download { root /code/autoindex; autoindex on; autoindex_exact_size off; autoindex_localtime on; auth_basic "性感荷官在線發(fā)牌!??!"; auth_basic_user_file /etc/nginx/auth_basic; } location = /basic_status { stub_status; }}

3)訪問

#訪問 http://www.autoindex.com/basic_status#nginx七種狀態(tài)Active connections: 2 server accepts handled requests 2 2 2 Reading: 0 Writing: 1 Waiting: 1Active connections #活躍的連接數(shù)accepts #TCP連接總數(shù)handled #成功的TCP連接數(shù)requests #成功的請求數(shù)Reading #讀取的請求頭Writing #響應Waiting #等待的請求數(shù),開啟了keepalive# 注意, 一次TCP的連接,可以發(fā)起多次http的請求, 如下參數(shù)可配置進行驗證keepalive_timeout 0; # 類似于關(guān)閉長連接keepalive_timeout 65; # 65s沒有活動則斷開連接

關(guān)鍵詞:擴展

74
73
25
news

版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。

為了最佳展示效果,本站不支持IE9及以下版本的瀏覽器,建議您使用谷歌Chrome瀏覽器。 點擊下載Chrome瀏覽器
關(guān)閉