linux學習筆記-第二十四課-LNMP-Nginx進階配置(三)

來源:互聯網
上載者:User

標籤:linux   server   local   使用者   認證   


一、使用者認證

    使用者認證功能是利用Apache的工具htpasswd產生的密鑰,所以需要安裝Apache的這個工具即可,我們用yum來安裝就可以。

[[email protected] ~]# yum install -y httpd-tools[[email protected] ~]# htpasswd -cm /usr/local/nginx/conf/.htpasswd mydiscuzNew password:Re-type new password:Adding password for user mydiscuz[[email protected] ~]# cat /usr/local/nginx/conf/.htpasswdmydiscuz:$apr1$ejPLa15T$kuyykf8at2I77oogZ0kUz1

    修改配置,主要是修改server模組

location / {    auth_basic "Closed";    auth_basic_user_file .htpasswd; <== 這裡要注意加密檔案的路徑}

二、靜態檔案快取

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$    {     expires      30d;     access_log off;     }location ~ .*\.(js|css)?$    {     expires      12h;     access_log off;     }


三、防盜鏈


location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip|doc|pdf|gz|bz2|jpeg|bmp|xls)$ {                   valid_referers none blocked server_names  *.taobao.com *.baidu.com *.google.com *.google.cn *.soso.com ;                 if ($invalid_referer) {                        return 403;#                       rewrite ^/ http://www.example.com/nophoto.gif;                        }                }


四、網域名稱重新導向

 if ($host != ‘bbs.a.com‘ ){        rewrite ^/(.*)$ http://bbs.a.com/$1 permanent;    }


五 、日誌切割

     因為Nginx沒有自動切割日誌功能,所以需要手動編輯指令碼

     編寫指令碼:vim  /usr/local/sbin/logrotate.sh  //加入

#! /bin/bashdatedir=`date +%Y%m%d`/bin/mkdir  /home/logs/$datedir >/dev/null 2>&1/bin/mv /home/logs/*.log /home/logs/$datedir/bin/kill -HUP `cat /var/run/nginx.pid`


六、設定日誌記錄的內容

    日誌格式化
    log_format main ‘$remote_addr - $remote_user [$time_local] $request ‘
                    ‘"$status" $body_bytes_sent "$http_referer" ‘
                    ‘"$http_user_agent" "$http_x_forwarded_for"‘;

    log_format main1 ‘$proxy_add_x_forwarded_for - $remote_user [$time_local] ‘
                     ‘"$request" $status $body_bytes_sent ‘
                     ‘"$http_referer" "$http_user_agent"‘;  //此日誌格式為,ip不僅記錄代理的ip還記錄遠程用戶端真實IP。

七、存取控制

    限制只讓某個ip訪問
    allow          219.232.244.234;
    deny           all;

禁止某個IP或者IP段訪問網站的設定方法

首先建立下面的設定檔放在nginx的conf目錄下面,命名為deny.ip   
cat  deny.ip
deny 192.168.1.11;
deny 192.168.1.123;
deny 10.0.1.0/24;

在nginx的設定檔nginx.conf中加入:
include deny.ip;

重啟一下nginx的服務:/usr/local/nginx/sbin/nginx  reload 就可以生效了。

deny.ip 的格式中也可以用deny all;
如果你想實現這樣的應用,除了幾個IP外,其他全部拒絕,
那需要你在deny.ip 中這樣寫
allow 1.1.1.1;
allow 1.1.1.2;
deny all;

有時候會根據目錄來限制php解析:
location ~ .*(diy|template|attachments|forumdata|attachment|image)/.*\.php$
{
        deny all;
}


八、使用 user_agent 控制用戶端訪問
location /
{
    if ($http_user_agent ~ ‘bingbot/2.0|MJ12bot/v1.4.2|Spider/3.0|YoudaoBot|Tomato|Gecko/20100315‘){
            return 403;
    }
}

九、Nginx代理

    server {
            listen 80;
            server_name aaa.com;

            location / {
                proxy_pass      http://2.2.2.2/;
                proxy_set_header Host   $host;
                proxy_set_header X-Real-IP      $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            }
#            access_log  /home/logs/aaa_access.log combined;
        }

本文出自 “Topspeed_King” 部落格,請務必保留此出處http://mylinuxlife.blog.51cto.com/4706737/1654758

linux學習筆記-第二十四課-LNMP-Nginx進階配置(三)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.