1. 程式人生 > 實用技巧 >linux系統nginx認證加密等模組使用

linux系統nginx認證加密等模組使用

課前回顧主要講述了nginx的安裝配置流程

剛開始遇到了一點問題,ping不同外網,但是可以ping通

閘道器、ip、和dns,解決思路:首先找到網絡卡配置檔案檢查ip、閘道器、和dns,發現都沒有問題,然後ping閘道器、ip、dns,都沒有問題,這時候就關閉一個叫做NetworkManager的管理工具包並禁止開機自啟,就OK了

安裝nginx:首先更換官方源,複製官方配置檔案,然後yum下載,安裝完成後啟動nginx

檢查埠和程序,開啟瀏覽器訪問預設用的ip訪問,前提是沒有做域名解析,注意的點是關閉虛擬機器的防火牆

配置nginx:vim /etc/nginx/nginx.conf

然後手寫server標籤

server寫法:server{

​ 監聽的埠

​ listen 80;

​ server的域名

​ server_name www.wzh.com;

​ 站點目錄和預設頁面

​ location / {

​ root /opt/wzh;

​ index index.html index.htm;

}

}

寫完後儲存退出,然後檢查語法,重新載入nginx,根據配置檔案的需求創建出站點目錄並編輯一個index.html預設頁面,接著在hosts檔案裡做ip和域名解析,然後重新訪問瀏覽器

注:如果在配置檔案的站點目錄寫的是相對路徑html的時候預設是在/usr/share/nginx/html/下面,但是一般不寫相對路徑,容易出錯

改名複製小技巧

在xxx後面加上.conf

mv xxx {,.conf}

給xxx備份

cp xxx{,.bak}

關於在server標籤配日誌:作用域越小優先順序越高

比如在全域性配置了日誌另外在location裡面又配置了,會優先選擇location裡的配置,把location裡的日誌註釋了才會選擇全域性的,在精細點如果又加入了一個/xxx的日誌,那麼訪問/xxx的時候日誌只會記錄在/xxx裡面

關於狀態碼:當訪問一個網站的時候並不只是載入頁面還有很多的比如圖片之類的東西,頁面可以顯示但是別的東西可能載入不出來也會產生404的狀態碼,所以解決的話就要找到network裡面的狀態碼看看具體的路徑,然後把這個路徑創建出來

nginx模組

學習nginx模組然後把模組加入nginx的配置檔案從而實現頁面的各種展示形式和功能

找模組的方式:在瀏覽器上的網址框裡輸入nginx.org開啟nginx的官方網站,然後點開documentation,往下拉找到Modules reference,下面就是各種模組

index模組:作用就是展示網站的預設頁面

Syntax:	index file ...;
Default:	
index index.html;
Context:	http, server, location

注:如果配置檔案裡有路徑但是沒有配置index會有403的報錯

403:找不到主頁

404:找不到頁面

** ngx_http_autoindex_module:**

# 自動獲取,把瀏覽器頁面變成可預覽的目錄,預設是off,改成on
Syntax:	autoindex on | off;
Default:	
autoindex off;
Context:	http, server, location
# 把檔案大小更精確的輸出,預設是on,改成off
Syntax:	autoindex_exact_size on | off;
Default:	
autoindex_exact_size on;
Context:	http, server, location
# 預設顯示的格式html
Syntax:	autoindex_format html | xml | json | jsonp;
Default:	
autoindex_format html;
Context:	http, server, location
This directive appeared in version 1.7.9.
# 顯示本地時間,預設是off改成on
Syntax:	autoindex_localtime on | off;
Default:	
autoindex_localtime off;
Context:	http, server, location

** ngx_http_charset_module:**字符集模組解決亂碼

Syntax:	charset charset | off;
Default:	
charset off;
Context:	http, server, location, if in location

crm學習方式:先抄官網(copy),然後執行(run),最後修改(modify)

手動配置autoindex模組

server {

        listen 80;
        server_name www.wzh.com;
        charset "utf-8,gbk";
        access_log /var/log/nginx/wzh_access.log main;

        location / {
                root /opt/wzh;
                autoindex on;
                autoindex_exact_size off;
                autoindex_localtime off;
                autoindex_format html;
}
}
# 配置完成後首先檢測語法O不OK,然後重新載入nginx,最後再滿足配置檔案的需求,建立相對應的目錄做域名解析,然後再在創建出的站點目錄下面建立幾個檔案,最後訪問瀏覽器
**檢測語法**
nginx -t
**重新載入**
systemctl reload nginx
**建立目錄**
mkdir /opt/wzh
**做域名解析**
在hosts檔案裡輸入10.0.0.7空格www.wzh.com儲存退出
**建立檔案**
cd /opt/wzh/
touch www.html
[root@web01 wzh]# touch zzz.html
[root@web01 wzh]# touch hahah.html
[root@web01 wzh]# touch hahah.txt
touch ggg.html
**檔案裡面寫內容**
[root@web01 wzh]# echo 我 >hahah.txt 
[root@web01 wzh]# echo 叫 >hahah.html 
[root@web01 wzh]# echo 王 >ggg.html 
[root@web01 wzh]# echo 張 >zzz.html 
[root@web01 wzh]# echo 行 >www.html
**跟阿里雲時間同步**
[root@web01 wzh]# yum install -y ntpdate
[root@web01 wzh]# ntpdate time1.aliyun.com
**瀏覽器訪問**

注:**站點目錄下面不能建立index.html的檔案,如果建立了那麼瀏覽器就是預設找這個頁面

模組監控和模組認證

alias:重新定義站點目錄,比如我在配置檔案裡想定義多個站點目錄的時候如果用root就會產生報錯,這時候改成alias就OK了

nginx的狀態監控

# 模組名:ngx_http_stub_status_module記錄nginx的狀態
Syntax:	stub_status;
Default:	—
Context:	server, location
#寫法
location = /basic_status {
    stub_status;
}
#如果使用原始碼安裝的nginx必須加上--with-http_stup_status_module這個選項才可以使用這個模組

認證模組

# 模組名: ngx_http_auth_basic_module
# 寫法
location / {
    auth_basic           "closed site";
    auth_basic_user_file conf/htpasswd;
}
# 預設是關閉的,改成on
Syntax:	auth_basic string | off;
Default:	
auth_basic off;
Context:	http, server, location, limit_except
# 想使用這個模組要安裝httpd-tools,因為這個服務是apache提供的
安裝完成會有一個htppasswd命令
這個命令的選項-b和-c
-b:是互動密碼
-c:是免互動在命令列輸入
-n:可以新增多個使用者和-c不能同時使用

練習題

使用五個模組配置server站點**

環境準備

一臺新虛擬機器ip10.0.0.7

首先關閉防火牆

systemctl stop firewallld

禁止開機自啟

systemctl disable firewalld

關閉selinux

sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux

然後修改nginx 的源開啟nginx.org官網。找到download,然後點進去檢視穩定版本,點stable and mainline,找到RHEL/CentOS的配置檔案和內容,在虛擬機器裡面編輯這個配置檔案,把內容貼上進去,儲存退出,然後下載nginx,下載完成後開啟nginx,並加入開機自啟,然後檢測埠和程序,沒問題就去瀏覽器訪問

然後就是修改nginx的配置檔案

編輯nginx的配置檔案

[root@web01 conf.d]# vim /etc/nginx/conf.d/www.wzh.conf 
server {
	
	listen 80;
	server_name www.wzh.com;
	charset "utf-8";
	access_log /var/log/nginx/wzh_access.log main;

	location / {
		root /opt/wzh;
		index index.html index.htm;
}

	location /download {
		alias /opt/down;		
		autoindex on;
		autoindex_exact_size off;
		autoindex_localtime on;
		#auth_basic           "wzh";
    		#auth_basic_user_file /etc/nfinx/pass/download.pass;
}


	location = /ztjk {
	    stub_status;
}
}
# 配置檔案編輯完成後,檢測語法nginx -t,
語法沒問題就重新載入nginx使用systemctl reload nginx,
載入完成後根據配置檔案內容建立對應的站點目錄和檔案,做域名解析,
然後下載httpd-tools的工具包,
會有一個htpasswd的命令,
使用這個命令的-b和-c選項生成密碼然後瀏覽器訪問