Nginx學習筆記-默認網站

Nginx配置文件詳細請諮詢百度

默認網站

當Nginx配置文件中有且只有一個server時,該server被nginx認爲時默認網站,所有發給nginx服務得訪問請求,都會默認給該server

默認配置主要爲server字段

server {
        ##監聽端口
        listen       80;
        ##域名
        server_name  localhost;
        ##web根目錄定義
        location / {
            root   html;
            index  index.html index.htm;
        }
        ##50x錯誤響應碼返回頁面
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

目錄訪問控制

場景模擬:只允許本機訪問某目錄,拒絕其他所有訪問信息

location /a {
        #定義允許訪問的主機
        allow 127.0.0.1;
        #定義拒絕訪問的主機
        deny all;
        #return指定拒絕訪問主機的返回頁
        #return 404;
        return http://www.jd.com;
        }

登陸認證
參數配置auth_basic,默認爲關閉

語法:auth_basic string | off;

        auth_basic_user_file file_path;

實例:

location /b {
        auth_basic "登陸驗證";
        auth_basic_user_file /etc/nginx/htpasswd;
        }

htpasswd配置,需要安裝httpd-tools包

語法:htpasswd [-cimBdpsDv] [-C cost] passwordfile username

[root@localhost ~]# mkdir /etc/nginx
[root@localhost ~]# htpasswd -m /etc/nginx/htpasswd admin

日誌管理

Nginx訪問⽇日誌主要有兩個參數控制

1) log_format  #⽤用來定義記錄⽇日誌的格式(可以定義多種⽇日誌格式,取不不同名字即可)

log_format  log_name  string

2) access_log  #⽤用來指定⽇日⾄至⽂文件的路路徑及使⽤用的何種⽇日誌格式記錄⽇日誌

access_log  logs/access.log  main;

示例:

自定義日誌格式

log_format test_01 '[$time_local] $remote_addr "$request" $status';

定義使用的日誌格式

access_log  logs/host.access.log  test_01;

log_format格式變量:

    $remote_addr  #記錄訪問⽹網站的客戶端地址

    $remote_user  #遠程客戶端⽤用戶名

    $time_local  #記錄訪問時間與時區

    $request  #⽤用戶的http請求起始⾏行行信息

    $status  #http狀態碼,記錄請求返回的狀態碼,例例如:200、301、404等

    $body_bytes_sent  #服務器器發送給客戶端的響應body字節數

    $http_referer  #記錄此次請求是從哪個連接訪問過來的,可以根據該參數進⾏行行防盜鏈設置。

    $http_user_agent  #記錄客戶端訪問信息,例例如:瀏覽器器、⼿手機客戶端等

    $http_x_forwarded_for  #當前端有代理理服務器器時,設置web節點記錄客戶端地址的配置,此參數⽣生效的前提是代理理服務器器也要進⾏行行相關的x_forwarded_for設置

防盜鏈

定義哪些訪問內容不允許通過第三方訪問

場景:各種格式的圖片不允許通過第三方訪問

示例一,圖片有固定目錄:

指定圖片目錄,定義允許訪問的方式,如果不是以上方式,返回錯誤頁面

location /images {
            valid_referers none blocked localhost;
            if ($invalid_referer) {
                return 403;
            }
        }

示例二,沒有固定目錄:

指定圖片格式,所有訪問網站中這些格式的圖片,如果不是通過允許的方式訪問,返回錯誤頁面

location ~* \.(png|jpg|gif|bmp)$ {
            valid_referers none blocked localhost;
            if ($invalid_referer) {
                return 403;
            } 
        }

完整場景模擬

場景:發佈一個內部網站,要求:

對圖片目錄做防盜鏈設置

用戶只允許某個網段的IP訪問

需要登陸驗證

配置方法:

    server {
        listen       80;
        server_name  localhost;
        ##指定字符集
        charset utf-8;
        #access_log  logs/host.access.log  main;
        location / {
            root   html;
            index  index.html index.htm;
        }
        
        location /a {
            auth_basic "登陸驗證";
            auth_basic_user_file /etc/nginx/htpasswd;
            allow 10.16.0.0/24;
            deny all;
        }
        
        #location /images {
        location ~* \.(png|jpg|gif|bmp)$ {
            valid_referers none blocked localhost;
            if ($invalid_referer) {
                return 403;
            } 
        }

    配置完成後,測試配置是否正常。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章