ngin 模塊及模板

nginx碎碎念

server {
        listen 80;
        server_name www.abc.com;
        access_log /var/log/nginx/www.abc.com.log;

        location / {
        root    /dir;
        index /a.txt;
        access_log /var/log/nginx/www.abc.com.dir.log;
        }
}

nginx的源:有官網上的源,有阿里雲上的源,github上面的源
下載nginx的方式,wget,yum,windows下載,不一樣的下載方式的安裝方法不同,相關文件的位置不同,相關文件的內容不同

nginx配置文件能夠簡單歸納爲:
	核心層,事件層,http層
	http層包括:server層,location層
yum下載的官網nginx能夠include,阿里雲nginx不能夠include

location / 能夠認爲是隱藏的
root和index在某些模塊中能夠省略
通常來講root和alias必需要指定一個,不知道就會報錯(403),index和autoindex也必須指定一個,不指定就報錯(403)
root可使用alias代替(使用alias將少一層目錄)
index可使用autoindex代替(區別是直接顯示一個文件內容,顯示文件或目錄的層級結構)
root裏面的 / 是根,別的地方的根是站點目錄
index後面的文件名能夠是任意的,html文件可使用絕對路徑,也可使用相對路徑

nginx日誌老是做用於最小的做用域
access.log	該文件記錄着nginx正常的訪問日誌和報錯日誌
error.log	該文件只記錄着nginx報錯日誌


/etc/nginx/conf.d/default.conf
	該文件記錄着nginx默認的信息,不過優先級低(默認的站點目錄,HTML頁面,error_page,location)
/usr/share/nginx/html
	這個目錄是安裝nginx自動生成目錄,這個目錄和使用root相對路徑(/50.html,html/50html)有關
/usr/share/nginx/html/50x.html 
	nginx默認的50x 的反饋頁面	
/usr/share/nginx/html/index.html
	下載好的nginx默認的html頁面,和IP的訪問好玩localhost的訪問有關
/etc/logrotate.d/
	centos7中默認的日誌切割文件,能夠自動切割nginx,mysql,yum的日誌,天週年默認不等
	
一個server表明一個網站,一個location表明一個頁面的跳轉
一個nginx搭載的網站裏面有目錄,各類後綴的文件,html頁面
經過nginx不一樣的模塊能夠給nginx增長不一樣的功能,好比:顯示目錄的層級結構,文件的大小,狀態時間,設置網站的密碼,限制訪問,限制併發訪問

401	:認證失敗

nginx模塊

index模塊

可使用html的語法編寫html,顯示美麗的html頁面css

1.手寫server語句
vim /etc/nginx/conf.d/syy1.conf 
server {
        listen 80;
        server_name www.abc.com;

        location / {
        root    /dir;
        index index.html;
        }
}

2.檢查和檢測,重載
nginx -t
nginx -s reload

3.建立站點目錄
mkdir /dir

4.建立html文件,並寫入內容

5.windows上域名解析

6.瀏覽器訪問
F5	alt+F5	F12+network+cache	換別的瀏覽器

autoindex模塊

1.手寫server語句
vim /etc/nginx/conf.d/syy1.conf 
server {
        listen 80;
        server_name www.abc.com;

        location / {
        root    /dir;   
        autoindex on;
        }
}

2.檢查和檢測
nginx -t
nginx -s reload

3.建立站點目錄
mkdir /dir

4.移除站點目錄下的html,htm文件

5.windows上域名解析

6.瀏覽器訪問
F5	alt+F5	F12+network+cache	換別的瀏覽器

添加字符集

charset utf-8,gbk;

autoindex_exact_size模塊

on :顯示文件的字節html

off :顯示文件的經常使用單位node

只顯示文件的大小,不顯示目錄的大小,過小的文件不會顯示經常使用的單位mysql

1.手寫server語句
vim /etc/nginx/conf.d/syy1.conf 
server {
        listen 80;
        server_name www.abc.com;
        charset utf-8,gbk;
        autoindex_exact_size off;
        
        location / {
        root    /dir;   
        autoindex on;
        }
}

2.檢查和檢測
nginx -t
nginx -s reload

3.建立站點目錄
mkdir /dir

4.移除站點目錄下的html文件

5.windows上域名解析

6.瀏覽器訪問
F5	alt+F5	F12+network+cache	換別的瀏覽器

autoindex_localtime on

on :顯示格林時間nginx

off :顯示當地時區時間git

這個時間最終顯示文件的狀態時間github

1.手寫server語句
vim /etc/nginx/conf.d/syy1.conf 
server {
        listen 80;
        server_name www.abc.com;
        charset utf-8,gbk;
        autoindex_exact_size off;
        autoindex_localtime off;
        
        location / {
        root    /dir;   
        autoindex on;
        }
}

2.檢查和檢測
nginx -t
nginx -s reload

3.建立站點目錄
mkdir /dir

4.移除站點目錄下的html文件

5.windows上域名解析

6.瀏覽器訪問
F5	alt+F5	F12+network+cache	換別的瀏覽器

ngx_http_stub_status_module模塊

最簡單的模塊,站點目錄都不用配...web

這個網站可使用allow和denysql

1.手寫server語句
vim /etc/nginx/conf.d/syy1.conf 
server {
        listen 80;
        server_name www.abc.com;

        location = /basic_status {
        stub_status;
        }
}

2.檢查和檢測
nginx -t
nginx -s reload

3.瀏覽器訪問
http://www.abc.com/basic_status

F5	alt+F5	F12+network+cache	換別的瀏覽器

#能夠修改location
        location = /zt {
        stub_status;
        }

#可使用allow和deny(先寫allow再寫deny)
        location = /zt {
        stub_status;
        allow 10.0.0.0/24;
        deny all;
        }

ngx_http_auth_basic_module模塊

該模塊放在server裏或者location均可以,可是不能單獨放在一個location裏面vim

能夠給整個網站設置密碼,也能夠給指定的location設置密碼

該模塊放在哪就是給哪一個模塊設置密碼,重複設置密碼無心義

這個模塊不能設置免密登陸

1.手寫server語句
vim /etc/nginx/conf.d/syy1.conf 
server {
        listen 80;
        server_name www.abc.com;

	   location / {
        root    /dir;   
        autoindex on;
        
        auth_basic           "closed site";
        auth_basic_user_file /dir/htpasswd;
        }

        location = /zt {
        stub_status;
        }
}

2.檢查和檢測(#只是檢查server語句語法而已,站點目錄不存在的話都不會被檢測出來)
nginx -t
nginx -s reload

3.建立站點目錄
mkdir /dir

寫在哪就是給誰設置密碼,能夠給網站,location,指定的html設置密碼
htpasswd -b -c /dir/htpasswd syy 123

4.移除站點目錄下的html文件

5.windows上域名解析

6.瀏覽器訪問
F5	alt+F5	F12+network+cache	換別的瀏覽器

ngx_http_access_module

該模塊放在server裏或者location均可以,可是不能單獨放在一個location裏面

容許某一網段的的IP訪問,拒絕剩下的全部用戶的訪問

basic和access 是兩個模塊,互不相關

當心allow和deny的順序

vim /etc/nginx/conf.d/syy1.conf +13
server {
        listen 80;
        server_name www.abc.com;

        location / {
        root    /dir;
        autoindex on;

        #auth_basic           "closed site";
        #auth_basic_user_file /dir/htpasswd;

        allow 10.0.0.0/24;
        deny  all;
}

        location = /zt {
        stub_status;
        }
}
vim /etc/nginx/conf.d/syy1.conf +13
server {
        listen 80;
        server_name www.abc.com;

        location / {
        root    /dir;
        autoindex on;

        #auth_basic           "closed site";
        #auth_basic_user_file /dir/htpasswd;
}

        location = /zt {
        stub_status;
        allow 10.0.0.0/24;
        deny all;
        }
}

#使用curl命令取出指定內容
curl http://syy:123@www.syy1.com/zt

不能這樣分開寫

Y5XxMQ.md.png

ngx_http_limit_conn_module模塊

鏈接限制,限制同時最高500個鏈接(只對公網IP限制)

超過500個鏈接後,後續所有顯示403

1.主配置文件
vim /etc/nginx/nginx.conf  
limit_conn_zone $binary_remote_addr zone=perip:10m; 
limit_conn_zone $server_name zone=perserver:10m;

2.從配置文件
vim /etc/nginx/conf.d/syy1.conf 
server {
        listen 80;
        server_name www.syy1.com;

        limit_conn perip 3;
        limit_conn perserver 3;

        location / {
                root /code/syy1;
                #index index.html;
                autoindex on;

                }
        }

3.nginx -s reload

4.瀏覽器 訪問該server中的任意一個location

使用ab命令對某一個網站( / 或者某一個頁面)進行壓測

-c	:併發用戶數
-n	:發送請求數(總數)

#域名必須加/

ab -c100 -n 1000 http://www.syy1.com/

ngx_http_limit_req_module 模塊

請求限制,限制併發請求

併發請求返回狀態碼503

1.主配置文件設置限制
vim /etc/nginx/nginx.conf 
limit_req_zone $binary_remote_addr zone=perip:10m rate=1r/s;
limit_req_zone $server_name zone=perserver:10m rate=10r/s;

2.從配置文件調用
vim /etc/nginx/conf.d/syy1.conf +5
server {
        listen 80;
        server_name www.syy1.com;

    limit_req zone=perip burst=1 nodelay;
    limit_req zone=perserver burst=1;
    
        location / {
                root /code/syy1;
                #index index.html;
                autoindex on;
        }
}

3.瀏覽器測試手速
503 Service Temporarily Unavailable (503服務暫時不可用)(服務器過載)

自定義反饋狀態碼

自定義狀態碼範圍(400-599),只有某些模塊能夠自定義狀態碼

1.自定義網站 單個IP'併發'請求形成的服務器過載 反饋的'狀態碼'
vim /etc/nginx/conf.d/syy1.conf +5
server {
        listen 80;
        server_name www.syy1.com;

    limit_req zone=perip burst=1 nodelay;
    limit_req zone=perserver burst=1;
    limit_req_status 412;

        location / {
                root /code/syy1;
                #index index.html;
                autoindex on;
        }
}

2.瀏覽器測試手速
412 Precondition Failed

3.查看日誌記錄的狀態碼
[root@web01 ~]# tailf /var/log/nginx/access.log
10.0.0.1 - syy [19/May/2020:01:02:32 +0800] "GET / HTTP/1.1" 412 575 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36" "-"

自定義狀態碼的 HTML頁面

1.自定義網站 單個IP'併發'請求形成的服務器過載 反饋的'狀態碼'
vim /etc/nginx/conf.d/syy1.conf +5
server {
        listen 80;
        server_name www.syy1.com;

    limit_req zone=perip burst=1 nodelay;
    limit_req zone=perserver burst=1;
    limit_req_status 412;
    error_page 412 /syy1.html;

        location / {
                root /code/syy1;
                #index index.html;
                autoindex on;

                auth_basic           "aaa";
                auth_basic_user_file /code/htpasswd;
        }
}

2.編輯412 HTML頁面
vim /code/syy1/syy1.html 
<!DOCTYPE html>
<html>
    <head>
        <meta charset="=utf-8" />
        <title>標題</title>
    </head>
    <body>
        <center>
            <p>
                412 Precondition Failed
            </p>
        </center>
        <img src="https://images.cnblogs.com/cnblogs_com/syy1757528181/1767642/t_20051810211013300809_980x1200_0.jpg" alt="看什麼看" width="1200" height="1000">

    </body>
</html>

3.重載nginx

4.瀏覽器測試手速

nginx初步優化

0.0>配置官方yum源,安裝官方nginx
0.1> 啓動,加入開機自啓動

1.主配置文件
vim /etc/nginx/nginx.conf  
limit_conn_zone $binary_remote_addr zone=perip:10m; 
limit_conn_zone $server_name zone=perserver:10m;

limit_req_zone $binary_remote_addr zone=perip2:10m rate=1r/s;
limit_req_zone $server_name zone=perserver2:10m rate=10r/s;

2.手寫server語句
vim /etc/nginx/conf.d/syy1.conf 
server {
        listen 80;
        server_name www.abc.com;
        
        access_log /var/log/nginx/www.abc.com.access.log main;
    	error_log /var/log/nginx/www.abc.com.error.log;
        
        #字符集
        charset utf-8,gbk;
        #顯示經常使用單位
        autoindex_exact_size off;
        #顯示當地時區時間
        autoindex_localtime off;
        #設置網站全站密碼
        auth_basic           "closed site";
        auth_basic_user_file /dir/htpasswd;
        #訪問限制
        deny 10.0.0.1;
        allow 10.0.0.0/24;
        deny  all;
        
        #設置狀態(zt)location,加入訪問控制
        location = /zt {
        stub_status;
        allow 10.0.0.0/24;
        deny all;
        }  
        
        #限制公網IP鏈接數
        limit_conn perip 3;
        limit_conn perserver 3;
        #限制併發請求
        limit_req zone=perip2 burst=1 nodelay;
        limit_req zone=perserver2 burst=1;
        
        #默認訪問域名返回的html頁面
        location / {
        root	/dir;
        index index.html;
        
        #自定義併發請求的反饋的狀態碼
        limit_req_status 412;
        error_page 412 /412.html;
             
        }
        #自定義站點目錄
        location /abc {
        root    /dir;   
        autoindex on;
        }
        
        #匹配大小寫  .*\.(svn|git|cvs)  通通拒絕  好比 tt.svn  .git .cvs
		location ~ .*\.(svn|git|cvs) {
		deny all;
        }

		# 忽略大小寫匹配   .*\.htm|html|xml|shtml   ->緩存 expires
		location ~* \.(htm|html|xml|shtml)$ {
		expires 600;    #秒
         }
		location  ~* \.(js|css)$ {
	    expires 30d;      #天
        }
		location ~* \.(mp3|htc|gif|ico|png|swf|jpg|jpeg|bmp)$ {
         etag off;         #關閉校驗
        }
        
        #只要出現錯誤則跳轉對應的錯誤頁面/dir/*.html 下面的文件
		error_page  400 = /dir/400.html;
		error_page  404 = /dir/404.html;
		error_page  500 = /dir/500.html;
		error_page  502 = /dir/502.html;
}

3.檢查
nginx -t
nginx -s reload

4.建立站點目錄
mkdir /dir/abc -p

5.建立html頁面,加入HTML元素
vim /dir/abc/index.html

6.編輯412 HTML頁面,400,404,500,502(#不作這些html頁面的話,默認反饋)
vim /dir/412.html 

7.basic模塊寫在哪就是給誰設置密碼,能夠給網站,location,指定的html設置密碼
htpasswd -b -c /dir/htpasswd syy 123

8.移除 想要顯示下載目錄的location中的 站點目錄下的html文件
rm -rf /dir/abc/*.htm*

9.windows上域名解析
10.0.0.7 www.abc.com

10.瀏覽器訪問,#測試網站(密碼,訪問限制,狀態模塊,文件單位,顯示時間,併發請求,還有他的html頁面,還有默認的index.html頁面,相關的html頁面,查看目錄結構,驗證location的模糊匹配)
F5	alt+F5	F12+network+cache	換別的瀏覽器

11.查看日誌記錄的狀態碼
[root@web01 ~]# tailf /var/log/nginx/www.abc.com.access.log
相關文章
相關標籤/搜索