0516第二十一次課:LNMP 2

LNMP 2

1、nginx默認虛擬主機

在Nginx中也有默認虛擬主機,跟httpd相似,第一個被Nginx加載的虛擬主機就是默認主機,但和httpd不相同的地方是,它還有一個配置用來標記默認虛擬主機,也就是說,若是沒有這個標記,第一個虛擬主機爲默認虛擬主機。php

  1. 設置nginx默認虛擬主機css

    vim /usr/local/nginx/conf/nginx.confhtml

    刪除如下標紅的地方:nginx

    添加如下內容:shell

    include vhost/*.conf;vim

  2. 建立虛擬主機文件瀏覽器

    建立vohost文件緩存

    mkdir /usr/local/nginx/conf/vhostbash

    vim test.conf服務器

    添加如下內容:

    server
    {
    #default_server 表示就是一個虛擬主機
     listen 80 default_server;
     server_name wxy.com;
     index index.html index.htm index.php;
     root /data/wwwroot/wxy.com;
    }

    建立默認目錄

    mkdir -p /data/wwwroot/wxy.com/

    新建index.html

    vim /data/wwwroot/wxy.com/index.html

    添加如下內容:

    this is test

    檢查配置文件

    ./nginx -t

    從新加載

    ./nginx -s reload

    測試

    curl -x 127.0.0.1:80 wxy.com

二.nginx用戶認證

  1. 修改虛擬主機conf配置文件

    添加如下內容:

location /
   {
   auth_basic "Auth";
   auth_basic_user_file /usr/local/nginx/conf/htpasswd;
   }
  1. 建立用戶

    使用httpd來建立,若是沒法使用則須要安裝(yum install httpd)

    htpasswd -c /usr/local/nginx/conf/htpasswd wxy

  2. 測試

    檢查是否有錯誤

    /usr/local/nginx/sbin/nginx -t

    從新加載

    /usr/local/nginx/sbin/nginx -s reload

    測試:

    curl -uwxy:123 -x127.0.0.1:80 test.com -I

  3. 對某個目錄進行認證

    修改虛擬主機配置文件

    把location /改成location /admin

    location /admin/

    正常訪問

    curl -x 127.0.0.1:80 test.com

    添加admin目錄,並建立index.html

    mkdir /data/wwwroot/wxy.com/admin/

    echo "test admin " > /data/wwwroot/wxy.com/admin/index.html

    測試admin目錄訪問

  4. 針對某個文件進行認證

    修改虛擬主機文件

    ~ php:表示匹配php文件

    測試

    正常訪問

    訪問php類型

3、nginx域名重定向

  1. 修改虛擬主機文件

    修改內容以下:

    server
    	{    
    		listen 80;
    		server_name test.com test2.com test3.com;
    		index index.html index.htm index.php;
    		root /data/wwwroot/test.com;
    		if ($host != 'test.com') {
    		rewrite ^/(.*)$ http://test.com/$1 permanent; 
    		}
    	}

    permanent:永久跳轉,301

    redirect:臨時跳轉,302

  2. 測試

    檢查

    從新加載

    ../../sbin/nginx -s reload

4、nginx日誌

  • nginx日誌
  1. nginx日誌配置

    nginx日誌在nginx.conf文件中配置

    nginx日誌變量意義

名稱 解釋
$remote_addr 客戶端ip(公網ip)
$http_x_forwarded_for 代理服務器的ip
$time_local 服務器本地時間
$host 訪問主機名(域名)
$request_uri 訪問的url地址
$status 狀態碼
$http_referer referer
$http_user_agent user_agent
  1. 在虛擬主機中配置日誌格式

    在虛擬主機配置文件中添加如下內容:

    access_log /tmp/wxy.log combined_realip;

    /tmp/wxy.log是日誌路徑

    combined_realip是日誌格式,在nginx.conf中log_format後面自定義

  2. 訪問並查看日誌

  • nginx日誌切割
  1. nginx沒有本身的切割工具,咱們使用腳原本實現

    vim /usr/local/sbin/nginx_logrotate.sh

    腳本內容:

    #! /bin/bash
    #定義前一天日期
    d=`date -d "-1 day" +%Y%m%d`
    #日誌路徑
    logdir="/tmp/"
    nginx_pid="/usr/local/nginx/logs/nginx.pid"
    cd $logdir
    for log in `ls *.log`
    do
    mv $log $log-$d 
    done
    #從新加載nginx,不然不會生成新的日誌文件。相似(nginx -s relod)
    /bin/kill -HUP `cat $nginx_pid`
  2. 測試運行腳本

    sh -x /usr/local/sbin/nginx_logrotate.sh

    ll /tmp/wxy.log*

  3. 建立定時任務

    添加一個天天0點執行此腳本的定時任務

    crontab -e

    0 0 * * * /usr/local/sbin/nginx_logrotate.sh

  4. 清理日誌

    如清理30天之前的日誌

    find /tmp/ -type f -name "*.log-" -mtime +30 |xargs rm

  • 靜態文件不記錄日誌和瀏覽器緩存時間

    虛擬主機配置文件location~能夠指定對應的靜態文件,expires配置過時時間,而access_log 配置爲off就能夠不記錄訪問日誌了

  1. 修改配置虛擬主機文件

    修改內容以下:

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

    expires 7d:表示過時時間7天

    expires 12:表示過時時間12小時

  2. 測試

    模擬gif和js文件

    echo "gif gif gig" > /data/wwwroot/wxy.com/1.gif

    echo "js js js " >/data/wwwroot/wxy.com/2.js

    檢查nginx配置文件,並從新加載

    ../../sbin/nginx -t

    ../../sbin/nginx -s reload

    開始測試

    curl -x 127.0.0.1:80 wxy.com/1.gif -I

    curl -x 127.0.0.1:80 wxy.com/2.js -I

    查看日誌並無生成gif和js的日誌

  • nginx防盜鏈
  1. 修改虛擬主機配置文件

    修改如下內容

    valid_referers none blocked server_names *.wxy.com;
        if ($invalid_referer)
        {
        return 403;
        }

  2. 測試

    無效refer返回403

    curl -e "http://www.qq.com/1.jpg" -x 127.0.0.1:80 wxy.com/2.jpg

    正常的

    curl -e "wxy.com/1.jpg" -x 127.0.0.1:80 wxy.com/1.gif

5、nginx訪問控制

  • 設置ip白名單

    1.修改nginx虛擬主機配置文件

    添加如下內容

    location /admin/
     {
       allow 127.0.0.1;
       allow 47.106.84.56;
       deny all;
     }
  1. 測試

    使用127.0.0.1訪問正常

    curl -x 127.0.0.1:80 wxy.com/admin/ -I

    在47.106.84.56上訪問正常

    在其它機器訪問403

  • 針對文件進行控制

    在上傳目錄upload和image,禁止.php的文件

  1. 修改虛擬配置文件

    添加如下內容:

    location ~ .*(upload|image)/.*\.php$
    {
      deny all;
    }
  2. 測試

    在upload目錄建立1.txt和1.php文件,可以訪問1.txt,不可以訪問1.php

    1.txt 能夠訪問,1.php訪問403

  • 根據user-agent進行限制

    1.修改虛擬主機配置文件

    添加如下內容

    if ($http_user_agent ~* 'Spider/3.0|YoudaoBot|Tomato')
       {
         return 403;
       }
    1. 測試

    使用user_agent設置的三個用戶訪問都報403

    使用其它用戶訪問正常

6、nginx代理

代理分爲:正向代理、反向代理。正向通常不使用,主要仍是使用反向代理。反向代理(Reverse Proxy)方式是指以代理服務器來接受Internet上的鏈接請求,而後將請求轉發給內部網絡上的服務器;並將從服務器上獲得的結果返回給Internet上請求鏈接的客戶端,此時代理服務器對外就表現爲一個服務器。

  • 配置反向代理
  1. 新建配置文件

    vim /usr/local/nginx/conf/vhost/proxy.conf

    添加如下內容:

server
{
   listen 80;
   server_name ask.apelearn.com;
   location /
   {
       proxy_pass http://47.104.7.242/;
       proxy_set_header Host $host;
       proxy_set_header X-Real-IP $remote_addr;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
   }
}
  1. 測試

    檢查配置文件

    ../../sbin/nginx -t

    從新加載配置文件

    ../../sbin/nginx -s reload

    curl測試

    curl -x 127.0.0.1:80 ask.apelearn.com -I

    測試robots

    curl ask.apelearn.com/robots.txt

相關文章
相關標籤/搜索