Nginx:綜合架構網站服務 -- 2 -- nginx網站搭建配置及訪問


綜合架構網站服務

命令總覽:

[root@web01 html]# nginx -h 		-- 查看nginx幫助選項
[root@web01 7]# nginx -V    		-- 查看已配置信息,是編譯安裝是./config配置的內容
[root@web01 html]# nginx -t 		-- 對配置語法進行檢查

# nginx -T 能查看nginx的所有配置信息
[root@web01 html]# nginx -T 		-- 對配置語法進行檢查,並且將所有配置信息顯示到屏幕上
[root@web01 html]# nginx -s reload 	-- -s參數指定需要執行的動作stop, quit, reopen, reload
									--(建議和systemctl reload nginx只用一種方式,不要混用)
[root@web01 password]# htpasswd -bc htpasswd oldboy 123456 -- 創建訪問認證密碼文件
[root@web01 password]# curl www.oldboy.com -u oldboy   ## 指定用戶,輸入密碼
[root@web01 password]# curl www.oldboy.com -u oldboy:123456 ## 指定用戶及密碼

00. 內容說明:

  1. nginx的安裝部署回顧
  2. nginx服務的企業應用 (模塊功能)
  3. nginx在企業中虛擬主機的訪問方式、原理及配置
  4. nginx部分總結

01. 上文回顧:

鏈接:https://www.cnblogs.com/moox/p/12689800.html

  1. nginx服務一些特點介紹
    a 支持高並發能力比較強 消耗資源少,如淘寶在Nginx基礎上開發了tengine
    b 軟件功能比較多樣 (支持web,負載均衡,緩存服務)
    c 支持系統平台比較廣泛
    可以制作yum倉庫
    1. 收集倉庫中的軟件 --- yum.conf
    2. 客戶端訪問到制作yum倉庫 --- nginx (可以部署在linux windows)
  2. nginx軟件部署安裝
    a yum安裝方式
    b 編譯安裝軟件
  3. nginx目錄結構
  4. nginx配置文件參數說明

02. nginx服務的企業應用

1. 利用nginx服務搭建一個網站(www)

  1. 編寫虛擬主機配置文件/etc/nginx/conf.d/*.conf
  2. 需要獲取開發人員編寫的網站代碼Index.html
  3. 重啟nginx服務(平滑重啟) systemctl reload nginx 或 nginx -s reload
  4. 編寫DNS配置信息-- 模擬域名解析windows../hosts
  5. 進行測試訪問

1.1 : 編寫虛擬主機配置文件

cd /etc/nginx/conf.d/
vim www.conf
server {
    listen        80;  				# 監聽的端口
    server_name   www.oldboy.com;	 # 主機的域名 
    location  /oldboy {				# 匹配的區域
    root  /usr/share/nginx/html;	# 站點目錄
    index oldboy.html;				# 指定首頁文件
    }
}

1.2 : 獲取開發人員網站代碼

上傳一張圖片moox.jpg到站點目錄下/usr/share/nginx/html

<html>
  <meta charset="utf-8">
  <head>
    <title>Nginx head part</title>
  </head>
  <body>
   Nginx 網站服務搭建 body部分
    <table border=1>
      <tr> <td>01</td> <td>oldboy</td> </tr>
      <tr> <td>02</td> <td>oldgirl</td> </tr>
      <tr> <td>03</td> <td>olddog</td> </tr>
    </table>
    <a href="http://cnblos.com/moox">
      <img src="moox.jpg" />
    </a>
   </body>
</html>

1.3 : 重啟nginx服務
(平滑重啟) 兩種方法: -- 選其中一種,不要混用

systemctl reload nginx (yum安裝直接平滑重啟即可)
nginx -s reload (編譯安裝無nginx服務,-s指定執行動作)

nginx命令參數(nginx -h)

Options:
  -?,-h         : this help
  -v            : show version and exit
  -V            : show version and configure options then exit
  -t            : test configuration and exit 				
  -- nginx -t 檢查配置語法
  -T            : test configuration, dump it and exit		
  -- nginx -T -- 對配置語法進行檢查,並且將所有配置信息顯示到屏幕上
  -q            : suppress non-error messages during configuration testing
  -s signal     : send signal to a master process: stop, quit, reopen, reload  
  -- nginx -s reload 指定執行動作
  -p prefix     : set prefix path (default: /etc/nginx/)
  -c filename   : set configuration file (default: /etc/nginx/nginx.conf)
  -g directives : set global directives out of configuration file

1.4 : 配置DNS域名解析

真實域名: 在阿里雲上進行DNS解析記錄配置
模擬域名: 在windows主機的hosts文件中進行配置即可
C:\Windows\System32\drivers\etc\hosts
改動頻率比較大,可以將hosts快捷方式到桌面

10.0.0.7 www.moox.com 

1.5 : 進行測試訪問

瀏覽器中: 
http://www.moox.com/    -- 請求的是/ ,默認進入 default.conf中location / 指定的首頁
http://www.moox.com/moox.html    -- 進入 conf.d/www.conf 中location /moox 指定的首頁moox.html

linux:
mkdir /usr/share/nginx/html/moox -p
mv /usr/share/nginx/html/moox.* /usr/share/nginx/html/moox/
systemctl reload nginx

清除緩存重新訪問測試:
http://www.moox.com/moox -- 進入 conf.d/www.conf 中location /moox 指定的首頁moox.html

2. 部署搭建網站常見錯誤

  1. 網站服務配置文件編寫不正確

    • 404 錯誤
      • 解決方法一: 修改nginx配置文件---location
      • 解決方法二: 在站點目錄中創建相應目錄或文件數據信息
    • 403 錯誤
      • 解決方法一: 不要禁止訪問
      • 解決方法二: 因為沒有首頁文件
    • 500 Internal Server Error
      • 1.內部程序代碼編寫有問題
      • 2.程序服務中文件權限不正確
  2. DNS信息配置不正確

  3. nginx配置文件修改, 一定要重啟服務;
    站點目錄中代碼文件信息調整,不需要重啟服務

3. 利用nginx服務搭建一個多網站(www bbs blog)

3.1 創建多個虛擬主機配置文件
3.2 創建站點目錄和目錄中首頁文件
3.3 編寫hosts解析文件(Windows)
3.4 進行訪問測試
3.5. 企業中虛擬主機訪問方式

3.1 創建多個虛擬主機配置文件

## /etc/nginx/conf.d/bbs.conf
server {
   listen        80;
   server_name   bbs.oldboy.com;
   location  / {
     root  /html/bbs;
     index index.html;
   }
}
## /etc/nginx/conf.d/blog.conf
server {
   listen        80;
   server_name   blog.oldboy.com;
   location  / {
     root  /html/blog;
     index index.html;
   }
}
## /etc/nginx/conf.d/www.conf
server {
   listen        80;
   server_name   www.oldboy.com;
   location  / {
     root  /html/www;
     index index.html;
   }
}
[root@web01 nginx]# systemctl reload nginx

3.2 創建站點目錄和目錄中首頁文件

[root@web01 conf.d]# mkdir /html/{www,bbs,blog} -p
[root@web01 conf.d]# for name in {www,bbs,blog};do echo "10.0.0.7 $name.moox.com" >/html/$name/index.html  ;done
[root@web01 conf.d]# for name in {www,bbs,blog};do cat /html/$name/index.html  ;done
10.0.0.7 www.moox.com
10.0.0.7 bbs.moox.com
10.0.0.7 blog.moox.com

3.3 編寫hosts解析文件(Windows)
10.0.0.7 www.oldboy.com bbs.oldboy.com blog.oldboy.com

3.4 進行訪問測試

  • 利用windows進行瀏覽器訪問測試,需要清除緩存。Google瀏覽器可使用無痕模式,不用每次都清緩存
  • 利用linux進行命令訪問測試
cat /etc/hosts
172.16.1.7      web01 www.moox.com bbs.moox.com blog.moox.com 
[root@web01 conf.d]# curl www.oldboy.com
10.0.0.7 www.oldboy.com
[root@web01 conf.d]# curl bbs.oldboy.com
10.0.0.7 bbs.oldboy.com
[root@web01 conf.d]# curl blog.oldboy.com
10.0.0.7 blog.oldboy.com
  • 有多個網站時,使用ip訪問可以指定最先的訪問次序
cat /etc/nginx/nginx.conf
## ...
include /etc/nginx/conf.d/blog.conf; --在包含所有*.conf的前面指定訪問順序
include /etc/nginx/conf.d/*.conf;

03. 企業中虛擬主機的訪問

1 企業中虛擬主機的訪問方式
2 企業中網站的頁面訪問原理
3 企業中網站的安全訪問配置

1. 企業中虛擬主機的訪問方式

  • a 基於域名的方式進行訪問:

  • b 基於地址的方式進行訪問: (只能用指定地址訪問) --- 負載均衡+高可用服務

    server {
    listen        10.0.0.7:80;  ## 指定IP及端口,重啟nginx服務
    server_name   www.oldboy.com;
    location  / {
    	root  /html/www;
    	index index.html;
    	}	
    }
    ## PS: 服務配置文件中涉及到地址修改,必須重啟nginx服務,不能平滑重啟**
    
  • c 基於端口的方式進行訪問:

    ## zabbix服務(apache:80)  + web服務(nginx:80) --> 同一台主機
    server {
    listen        8080;
    server_name   www.oldboy.com;
    location  / {
    	root  /html/www;
    	index index.html;
    	}
    }
    

2. 企業中網站的頁面訪問原理

  1. 將域名進行解析 www.oldboy.com --- 10.0.0.7
  2. 建立TCP的連接(四層協議)
    10.0.0.7 目標端口 8080 -- 但域名訪問,tcp端口默認為80
  3. 根據應用層HTTP協議發出請求
    請求報文: hosts: www.oldboy.com
    -- 找到www.oldboy.com ,但端口不是80,放棄;轉而找滿足8080端口的
  4. 沒有相同域名的server主機,會找滿足端口要求的第一個主機
    顯示主機的網站頁面 bbs.oldboy.com --顯然不是要找的www.oldboy.com

3. 企業中網站的安全訪問配置

a 根據用戶訪問的地址進行控制

10.0.0.0/24 www.oldboy.com/AV/ 不能訪問 deny
172.16.1.0/24 www.oldboy.com/AV/ 可以訪問 allow

  • nginx訪問模塊: ngx_http_access_module

    • 舉例配置:
    location / {
      deny  192.168.1.1;
      allow 192.168.1.0/24;
      allow 10.1.1.0/16;
      allow 2001:0db8::/32;
      deny  all;
    }
    
    • 指令用法
    Syntax:	deny address | CIDR | unix: | all;
    Default:	—
    Context:	http, server, location, limit_except    --- 能夠使用deny的區域
    
    • 第一個歷程: 編寫配置文件
        [root@web01 conf.d]# cat www.conf 
        server{
            listen	80;
            server_name www.moox.com;
            location / {
                root  /html/www;
                index index.html index.htm;
            }
            location /AV {
                deny 10.0.0.0/24;
                allow 172.16.1.0/24;
            }
        }      
    - 補充: 
        location外面的信息, 全局配置信息
        location里面的信息, 局部配置信息
    
b 根據用戶訪問進行認證

訪問時有彈出框,如輸入密碼認證等
nginx認證模塊: ngx_http_auth_basic_module
編寫虛擬主機配置文件 /etc/nginix/conf.d/www.conf

  • nginx認證模塊: ngx_http_auth_basic_module

    • 舉例配置:
    location / {
           auth_basic           "closed site";    --- 開啟認證功能,引號中的內容是提示信息,但需要有auth_basic
           auth_basic_user_file conf/htpasswd;    --- 加載用戶密碼文件,密碼文件可以指定
    }
    
    • 第一個歷程: 編寫虛擬主機配置文件
    server {
    	listen        80;
    	server_name   www.oldboy.com;
    	location / {
             root  /html/www;
             index index.html;
             auth_basic      "oldboy-sz-01";
             auth_basic_user_file password/htpasswd;  --- 相對於/etc/nginx目錄的相對位置
    	}
    }
    
    • 第二個歷程: 創建密碼文件(文件中密碼信息必須是密文的)

    htpasswd 創建一個有密文信息的密碼文件
    htpasswd命令參數說明見文末04.知識總結部分(5)

    mkdir /etc/nginx/password/ -p
    htpasswd -bc ./password/htpasswd oldboy  123456  -- 免交互式創建加密密碼文件
    # 命令  -bc:免交互創建   指定文件名	   用戶名    密碼
    # 此時瀏覽器訪問需要輸入用戶名和密碼才能訪問
    
    • 修改密碼文件權限:
    [root@web01 conf.d]# ll ./htpasswd 
    -rw-r--r-- 1 root root 45 Apr 18 16:27 ../password/htpasswd
    [root@web01 conf.d]# chmod 600 ./htpasswd 
    [root@web01 password]# ll
    -rw------- 1 root root 45 Apr 18 16:27 htpasswd  
    
    ## 此時使用的文件權限為root,訪問時報500 Internal Server Error
    ## 一般原因: 500 Internal Server Error
    1. 內部程序代碼編寫有問題
    2. 程序服務中文件權限不正確
    
    ## 需要將密碼文件權限修改為nginx的worker管理的用戶www
    [root@web01 password]# ps -ef|grep nginx
    root       1570      1  0 14:43 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
    www        2260   1570  0 16:34 ?        00:00:00 nginx: worker process
    root       2299   1576  0 16:52 pts/0    00:00:00 grep --color=auto nginx
    [root@web01 password]# chown www.www htpasswd 
    [root@web01 password]# ll
    -rw------- 1 www www 45 Apr 18 16:27 htpasswd
    
c curl命令參數:
  參數 -u, --user USER[:PASSWORD]  Server user and password
[root@web01 password]# curl www.oldboy.com -u oldboy   ## 指定用戶
Enter host password for user 'oldboy':
10.0.0.7 www.oldboy.com
[root@web01 password]# curl www.oldboy.com -u oldboy:123456 ## 指定用戶及密碼
10.0.0.7 www.oldboy.com

04. 知識總結:

  1. 如何搭建一個網站(服務器 域名 網頁代碼 數據庫支持)
  2. 如何搭建多個網站
  3. 如何訪問網站(3種 域名 地址 端口)
  4. 網站服務安全配置
    根據用戶訪問域名地址進行控制
    根據用戶訪問信息進行認證控制

附:htpasswd命令參數說明

mkdir /etc/nginx/password/ -p
htpasswd -c ./password/htpasswd oldboy    -- 交互式創建加密密碼文件
# 命令  -c:創建   指定文件名			用戶名
或
htpasswd -bc ./password/htpasswd oldboy  123456  -- 免交互式創建加密密碼文件
# 命令  -bc:免交互創建   指定文件名	   用戶名    密碼
或
htpasswd -bpc      ./password/htpasswd oldboy  123456  -- 免交互式創建明文不加密密碼文件
# 命令  -bpc:免交互創建不加密   指定文件名	   用戶名    密碼

htpasswd -D htpasswd oldboy   --- 刪除指定用戶oldboy密碼文件htpasswd,參數-D -刪除

# htpasswd命令參數說明:
-c  Create a new file.  *****
	--創建一個密碼文件
-n  Don't update file; display results on stdout.
	--不會更新文件; 顯示文件內容信息
-b  Use the password from the command line rather than prompting for it. *****
	--免交互方式輸入用戶密碼信息
-i  Read password from stdin without verification (for script usage).
	--讀取密碼采用標准輸入方式,並不做檢查 ???
-m  Force MD5 encryption of the password (default).
	--md5的加密算法
-B  Force bcrypt encryption of the password (very secure).
	--使用bcrypt對密碼進行加密  
-C  Set the computing time used for the bcrypt algorithm (higher is more secure but slower, default: 5, valid: 4 to 31).
	--使用bcrypt algorithm對密碼進行加密
-d  Force CRYPT encryption of the password (8 chars max, insecure).
	--密碼加密方式
-s  Force SHA encryption of the password (insecure).
	--加密方式
-p  Do not encrypt the password (plaintext, insecure).
	--不進行加密
-D  Delete the specified user.
	--刪除指定用戶
-v  Verify password for the specified user.


免責聲明!

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



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