綜合架構(gòu)之網(wǎng)站服務(wù)-nginx

1.logrotate程序進(jìn)行日志切割原理說明

  1. nginx服務(wù)擴(kuò)展配置文件 server區(qū)塊
  2. nginx服務(wù)如何搭建網(wǎng)站
  3. nginx服務(wù)多個(gè)虛擬主機(jī) 多個(gè)server配置信息
  4. nginx服務(wù)三種訪問方式 基于域名 基于端口(訪問原理) 基于地址(監(jiān)聽地址)
  5. nginx服務(wù)實(shí)現(xiàn)共享存儲(chǔ) autoindex --- 顯示站點(diǎn)目錄結(jié)構(gòu)
  6. nginx服務(wù)安全訪問控制 基于訪問源地址(deny/allow) 基于認(rèn)證訪問(ngx_http_auth_basic_module)

logrotate程序進(jìn)行日志切割原理

cat /etc/logrotate.conf
# see "man logrotate" for details
# rotate log files weekly
weekly #默認(rèn)按周進(jìn)行切割日志

# keep 4 weeks worth of backlogs
rotate 4 #保留最新4周的數(shù)據(jù)

# create new (empty) log files after rotating old ones
create #切割日志之后創(chuàng)建新的日志文件

# use date as a suffix of the rotated file
dateext #生成的日志帶時(shí)間信息。

# uncomment this if you want your log files compressed
#compress #切割的日志是否壓縮

# RPM packages drop log rotation information into this directory
include /etc/logrotate.d #調(diào)用一個(gè)目錄下的日志文件

# no packages own wtmp and btmp -- we'll rotate them here
/var/log/wtmp { #具體得哪個(gè)文件進(jìn)行切割(局部配置)
    monthly
    create 0664 root utmp 切割完日志的權(quán)限
    minsize 1M        最小尺寸,如果不超過1M就不進(jìn)行切割
    rotate 1              保存一份
}

/var/log/btmp {
    missingok
    monthly
    create 0600 root utmp
    rotate 1
}

# system-specific logs may be also be configured here.

加載目錄下的日志文件

[root@web01 logrotate.d]# ll 
total 20
-rw-r--r--. 1 root root  91 Apr 11  2018 bootlog
-rw-r--r--  1 root root 351 Apr 23 22:34 nginx
-rw-r--r--. 1 root root 224 Oct 30  2018 syslog
-rw-r--r--. 1 root root 100 Oct 31  2018 wpa_supplicant
-rw-r--r--. 1 root root 103 Nov  5  2018 yum

[root@web01 logrotate.d]# cat syslog 
/var/log/cron
/var/log/maillog
/var/log/messages
/var/log/secure
/var/log/spooler
{
    missingok    #固定格式
    sharedscripts #固定格式
    postrotate #固定格式
    /bin/kill -HUP `cat /var/run/syslogd.pid 2#系統(tǒng)日志服務(wù)> /dev/null` 2#沒有這個(gè)進(jìn)程追加到空> /dev/null || true
    endscript #把日志做了切割日志,原有的日志還在調(diào)用,沒有生效,所以重啟新生成的日志才會(huì)被調(diào)用。

nginx服務(wù)擴(kuò)展配置文件

mv /etc/nginx/conf.d/default.conf   /etc/nginx/conf.d/default.conf.bak #避免主配置文件加載默認(rèn)配置文件,影響www配置文件。
/etc/nginx/conf.d/default.conf
egrep -v "#|^$" default.conf >www.conf #將#號(hào)空格重新追加到一個(gè)新的配置文件當(dāng)中。
server {                               --- 可以配置網(wǎng)站信息  每個(gè)網(wǎng)站==server==每個(gè)虛擬主機(jī)
        listen       80;                   --- 網(wǎng)站服務(wù)監(jiān)聽端口
        server_name  www.oldboy.com;       --- 定義網(wǎng)站主機(jī)域名
        location / {  ???
            root   /html/www;              --- 指定站點(diǎn)目錄(存放網(wǎng)站所有資源)
            index  oldboy.jpg index.htm;   --- 首頁文件#注:訪問www.oldboy的時(shí)候會(huì)默認(rèn)訪問第一個(gè)配置的首頁文件,如果第一個(gè)丟失或刪除會(huì)繼續(xù)向下匹配。匹配的首頁文件都刪除或丟失的時(shí)候會(huì)報(bào)403的錯(cuò)誤,首頁文件不存在。
        }
        error_page   404 500 502 503 504  /oldboy.jpg;   --- 錯(cuò)誤頁面優(yōu)雅顯示#也可以配置百度的優(yōu)雅界面,后面的都不要
        location = /oldboy,jpg { 匹配URI的信息
            root   /usr/share/nginx/html; #去哪找URI的信息
        }
    }

注:更新站點(diǎn)下的目錄內(nèi)容可直接加載。


在站點(diǎn)目錄存放index.html就可以訪問頁面內(nèi)容


啟動(dòng)nginx出現(xiàn)報(bào)錯(cuò)

[root@web01 conf.d]# systemctl restart nginx 
Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.

檢查語法是否正確

[root@web01 conf.d]# nginx -t www.conf 
nginx: invalid option: "www.conf"

nginx配置文件規(guī)范:
01. 區(qū)域模塊信息 必須有一對(duì)花括號(hào)
02. 指令信息后面必須有分號(hào)
03. 相應(yīng)指令信息必須放置在正確區(qū)塊中

nginx服務(wù)配置多個(gè)虛擬主機(jī)

  1. 多個(gè)虛擬主機(jī)配置
    第一個(gè)歷程: 編寫多個(gè)虛擬主機(jī)配置文件
    /etc/nginx/conf.d/
    [root@web01 conf.d]# cat *.conf
    server {
        listen       80;
        server_name  bbs.oldboy.com;
        location / {
            root   /html/bbs;
            index  index.html index.htm;
        }
        error_page  404 500 502 503 504  https://www.baidu.com/search/error.html;
    }
    server {
        listen       80;
        server_name  blog.oldboy.com;
        location / {
            root   /html/blog;
            index  index.html index.htm;
        }
        error_page  404 500 502 503 504  https://www.baidu.com/search/error.html;
    }
    server {
        listen       80;
        server_name  www.oldboy.com;
        location / {
            root   /html/www;
            index  index.html index.htm;
        }
        error_page  404 500 502 503 504  https://www.baidu.com/search/error.html;
    }

第二個(gè)歷程: 創(chuàng)建站點(diǎn)目錄和首頁文件

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

第三個(gè)歷程: 重啟nginx服務(wù)

    nginx配置文件規(guī)范:
    01. 區(qū)域模塊信息 必須有一對(duì)花括號(hào)
    02. 指令信息后面必須有分號(hào)
    03. 相應(yīng)指令信息必須放置在正確區(qū)塊中
   
    nginx -t    --- 檢查配置文件語法命令
    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /etc/nginx/nginx.conf test is successful

    systemctl restart/reload nginx

第四個(gè)歷程: 進(jìn)行網(wǎng)站頁面訪問測試
配置DNS解析信息

10.0.0.7 www.oldboy.com bbs.oldboy.com blog.oldboy.com

網(wǎng)站服務(wù)訪問方式

  • a 基于域名信息訪問
  • b 基于端口信息訪問
    修改server虛擬主機(jī)配置文件
server {
          listen       8080;#修改了端口
          server_name  www.oldboy.com;
          location / {
              root   /html/www;
              index  index.html index.htm;
          }
          error_page  404 500 502 503 504  https://www.baidu.com/search/error.html;
      }

訪問方式
www.oldboy.com:8080

如果輸入www.oldboy.com則訪問的是bbs.oldboy.com信息的內(nèi)容

原理

用戶訪問網(wǎng)站的原理

傳輸層封裝的時(shí)候不加8080的端口號(hào)會(huì)封裝80端口去站點(diǎn)尋找首頁文件的時(shí)候會(huì)根據(jù)字母排序的html進(jìn)行訪問。

  • c 基于地址信息訪問
    準(zhǔn)備工作: 主配置文件
    include /etc/nginx/conf.d/www.conf;
       listen       10.0.0.7:80;
        server_name  www.oldboy.com;
        location / {
            root   /html/www;
            index  index.html index.htm;
        }
       PS: nginx配置文件中只要涉及到IP地址修改,必須重啟nginx服務(wù),不能平滑重啟。

利用nginx服務(wù)實(shí)現(xiàn)FTP存儲(chǔ)服務(wù)器

第一個(gè)歷程: 編寫配置文件  www.conf 
    server {
         listen            80;
         server_name  www.oldboy.com;
         location / {
             root   /html/www;
             index  index.html index.htm;
             autoindex  on;    --- 開啟網(wǎng)站站點(diǎn)目錄信息顯示功能
             charset utf-8;    --- 設(shè)置中文字符集信息,避免頁面中文出現(xiàn)亂碼
         }
         error_page  404 500 502 503 504  https://www.baidu.com/search/error.html;
     }
    
    systemctl restart nginx
    第二個(gè)歷程: 創(chuàng)建站點(diǎn)目錄, 將指定的首頁文件刪除
    rm index.html -f
    第三個(gè)歷程: 修改媒體資源類型文件
    sed -r '/jpg\;$|gif\;$|txt\;$/s@(.*)@#\1@g' /etc/nginx/mime.types
    systemctl restart nginx

mine.type識(shí)別的文件類型注釋或刪除就可以下載了。

對(duì)網(wǎng)站頁面信息進(jìn)行訪問控制

allow/deny: 
location: location /VIP專區(qū)/ 

server {
    listen            80;
    server_name  www.oldboy.com;
    location / {
        root   /html/www;
        index  index.html index.htm;
        autoindex  on;
        charset utf-8;
    }
    location /VIP專區(qū)/ {     #--- 匹配uri操作,禁止訪問URI信息
        root   /html/www;
        deny   10.0.0.1;     --- 進(jìn)行訪問控制
        allow  all;
    }
    error_page 403 404 500 502 503 504  /error.html;
    location = /error.html {
        root /html/www;
    }
}

根據(jù)用戶登錄密碼進(jìn)行控制

auth_basic --- 開啟登錄認(rèn)證功能
auth_basic_user_file --- 指定加載的密碼文件
http://nginx.org/en/docs/http/ngx_http_auth_basic_module.html

第一個(gè)歷程: 修改配置文件

   root@web01 VIP專區(qū)]# cat /etc/nginx/conf.d/www.conf 
    server {
        listen            80;
        server_name  www.oldboy.com;
        location / {
            root   /html/www;
            index  index.html index.htm;
            autoindex  on;
            charset utf-8;
        }
        location /VIP專區(qū)/ { 
            root   /html/www;
            autoindex on;
            charset utf-8;
            auth_basic  oldboy62;
            auth_basic_user_file  /etc/password.txt; #可以相對(duì)路徑也可以使用絕對(duì)路徑
        }
        error_page 403 404 500 502 503 504  /error.html;
        location = /error.html {   #每個(gè)location是局部配置
            root /html/www;
        }
    }

第二個(gè)歷程: 生成密碼文件

    yum install -y httpd-tools
    htpasswd -bc /etc/password.txt oldgirl oldboy123   #---第一次創(chuàng)建
    htpasswd -b  /etc/password.txt oldboy oldboy123    #---添加新的用戶
    -b  免交互輸入密碼
    -c  創(chuàng)建密碼文件,但是是交互的形式

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容