1. 程式人生 > >2018-07-04筆記(LNMP配置)

2018-07-04筆記(LNMP配置)

mov 地址 日誌功能 emp return 靜態 rep then 代理

12.7 默認虛擬主機

默認虛擬主機是在定義虛擬主機時的第一個虛擬主機,或者是在定義server的時候加上default_server。
1、修改nginx的主配置文件nginx.conf,把原有的server段配置刪除,並在http段配置裏面加入一行include vhost/*.conf,這個目錄vhost要創建在nginx的conf目錄下

[root@lnmp ~]# vi /usr/local/nginx/conf/nginx.conf
[root@lnmp ~]# cat !$
cat /usr/local/nginx/conf/nginx.conf
user nobody nobody;
worker_processes 2;
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;

events
{
    use epoll;
    worker_connections 6000;
}

http
{
    include mime.types;
    default_type application/octet-stream;
    server_names_hash_bucket_size 3526;
    server_names_hash_max_size 4096;
    log_format combined_realip ‘$remote_addr $http_x_forwarded_for [$time_local]‘
    ‘ $host "$request_uri" $status‘
    ‘ "$http_referer" "$http_user_agent"‘;
    sendfile on;
    tcp_nopush on;
    keepalive_timeout 30;
    client_header_timeout 3m;
    client_body_timeout 3m;
    send_timeout 3m;
    connection_pool_size 256;
    client_header_buffer_size 1k;
    large_client_header_buffers 8 4k;
    request_pool_size 4k;
    output_buffers 4 32k;
    postpone_output 1460;
    client_max_body_size 10m;
    client_body_buffer_size 256k;
    client_body_temp_path /usr/local/nginx/client_body_temp;
    proxy_temp_path /usr/local/nginx/proxy_temp;
    fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
    fastcgi_intercept_errors on;
    tcp_nodelay on;
    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 8k;
    gzip_comp_level 5;
    gzip_http_version 1.1;
    gzip_types text/plain application/x-javascript text/css text/htm 
    application/xml;
    include vhost/*.conf;                        #增加這一行,把原有server配置刪除    
}

註意nginx配置文件中,一條配置必須以分號 ; 結束,否則都視為一條配置
2、創建vhost目錄,並在vhost目錄創建虛擬主機配置文件

[root@lnmp ~]# mkdir /usr/local/nginx/conf/vhost
[root@lnmp ~]# cd /usr/local/nginx/conf/vhost/
[root@lnmp vhost]# vim aaa.com.conf      #這裏註意名字一定要是什麽.conf,因為主配置文件定義了*.conf
[root@lnmp vhost]# cat aaa.com.conf             
server
{
    listen 80;
    server_name www.aaa.com default_server;     
    index index.html index.htm index.php;
    root /data/wwwroot/www.aaa.com/;
}
server
{
    listen 80;
    server_name www.test.com test.com ;
    index index.html index.htm index.php;
    root /data/wwwroot/www.test.com/;
}

說明:

(1) 一個server段配置就是一個虛擬主機,可以在一個配置文件中寫多個虛擬主機,也可以給每一個虛擬主機創建單獨的配置文件
(2)當server_name後面帶有default_server字段時,就代表這個虛擬主機就是默認虛擬主機,否則第一個虛擬主機為默認虛擬主機

3、檢測配置文件是否有錯並平滑重啟nginx

[root@lnmp vhost]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@lnmp vhost]# /usr/local/nginx/sbin/nginx -s reload

4、創建網站根目錄,並建立index.html索引頁

[root@lnmp vhost]# mkdir -p /data/wwwroot/www.aaa.com
[root@lnmp vhost]# mkdir -p /data/wwwroot/www.test.com
[root@lnmp vhost]# ll /data/wwwroot/www.aaa.com/index.html /data/wwwroot/www.test.com/index.html 
-rw-r--r-- 1 root root 0 7月   6 10:35 /data/wwwroot/wwww.aaa.com/index.html
-rw-r--r-- 1 root root 0 7月   6 10:35 /data/wwwroot/wwww.test.com/index.html
#寫入內容
[root@lnmp vhost]# echo "This is default server aaa" > /data/wwwroot/www.aaa.com/index.html
[root@lnmp vhost]# echo "This is test server test" > /data/wwwroot/www.test.com/index.html

5、用curl測試訪問

#測試www.aaa.com
[root@lnmp wwwroot]# curl -x127.0.0.1:80 www.aaa.com
This is default server aaa
#測試www.test.com
[root@lnmp wwwroot]# curl -x127.0.0.1:80 www.test.com
This is test server test
#測試默認虛擬主機
[root@lnmp wwwroot]# curl -x192.168.66.132:80 bbb.com
This is default server aaa
[root@lnmp wwwroot]# curl -x192.168.66.132:80 ccc.com
This is default server aaa

12.8 Nginx用戶認證

1、添加配置內容
在server_name 是www.test.com的虛擬主機配置下面添加以下的配置

location  /
{
    auth_basic             "Auth";
    auth_basic_user_file   /usr/local/nginx/conf/htpasswd;
}

#查看添加配置後的文件內容

[root@lnmp ~]# vim /usr/local/nginx/conf/vhost/aaa.com.conf 
[root@lnmp ~]# cat !$
cat /usr/local/nginx/conf/vhost/aaa.com.conf
server
{
    listen 80;
    server_name www.aaa.com default_server;     
    index index.html index.htm index.php;
    root /data/wwwroot/www.aaa.com/;
}
server
{
    listen 80;
    server_name www.test.com test.com ;
    index index.html index.htm index.php;
    root /data/wwwroot/www.test.com/;
location  /
{
    auth_basic             "Auth";
    auth_basic_user_file   /usr/local/nginx/conf/htpasswd;
}
}

##註意auth_basic_user_file 的路徑一定要填正確,否則報403錯誤

2、生成用於驗證的用戶和密碼 ,這裏需要用到apache的htpasswd命令
如果沒有這個命令可以yum安裝httpd-tools可以了

[root@lnmp ~]# yum install -y httpd-tools
[root@lnmp ~]# htpasswd -c /usr/local/nginx/conf/htpasswd test
New password: 
Re-type new password: 
Adding password for user testuser

# htpasswd命令的-c選項,只有在第一次創建用戶認證的密碼文件時需要使用,
# 如果再次添加用戶和密碼時使用了-c選項,則會覆蓋掉之前的所有內容

3、檢查語法錯誤,重載配置文件

[root@lnmp ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@lnmp ~]# nginx -s reload

4、測試是否生效
curl 測試訪問www.test.com

[root@lnmp wwwroot]# curl -x192.168.66.132:80 www.test.com -I
HTTP/1.1 401 Unauthorized
Server: nginx/1.12.1
Date: Fri, 06 Jul 2018 03:11:23 GMT
Content-Type: text/html
Content-Length: 195
Connection: keep-alive
WWW-Authenticate: Basic realm="Auth"

curl -u 加上用戶名密碼測試

[root@lnmp wwwroot]# curl -utest:test -x192.168.66.132:80 www.test.com
This is test server test

說明:這裏的用戶認證針對的是整個網站,也可以針對網站的某個目錄,或者某個文件進行用戶驗證,把

location  /                            
#把上面location後面的/替換成目錄或者文件
#例如:location  /admin/ 針對admin目錄,location ~ ^.*/admin.php針對admin.php文件
{
    auth_basic             "Auth";
    auth_basic_user_file   /usr/local/nginx/conf/htpasswd;
}

12.9 Nginx域名重定向

1、這裏用到rewrite模塊,修改aaa.com.conf配置文件,增加下面兩行

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

#查看修改後的配置

[root@lnmp wwwroot]# vi /usr/local/nginx/conf/vhost/aaa.com.conf 
[root@lnmp wwwroot]# cat !$
cat /usr/local/nginx/conf/vhost/aaa.com.conf
#server
#{
#   listen 80;
#    server_name www.aaa.com default_server;     
#    index index.html index.htm index.php;
#    root /data/wwwroot/www.aaa.com/;
#}
server
{
    listen 80;
    server_name www.test.com test.com ;
    index index.html index.htm index.php;
    root /data/wwwroot/www.test.com/;
    if ($host != ‘www.test.com‘ ) {
        rewrite  ^/(.*)$  http://test.com/$1  permanent;
                }
location  /
{
    auth_basic             "Auth";
    auth_basic_user_file   /usr/local/nginx/conf/htpasswd;
}
}

說明:

#在nginx配置文件中可以使用if判斷
#當 $host 不是www.test.com的時候,將域名重定向到http://www.test.com
#$host就是訪問請求的域名,也就是server_name
# ^/(.)$ 這個可以匹配出域名後面的URI地址,
# $1 就是調用前面(.
)匹配到的URI,類似sed 這樣就可以進行更精確的重定向了。
# permanent 表示永久重定向 狀態碼為 301
#redirect 表示臨時重定向,狀態碼302

2、關於URI
在nginx中有幾個關於uri的變量,包括$uri $request_uri $document_uri $args,
下面看一下他們的區別 :
假如訪問的URL為:http://www.test.com/index.php?a=1&b=2

$uri==/index.php
$request_uri==/index.php?a=1&b=2
$document_uri==/index.php
$args==?a=1&b=2

##一般uri和document_uri是相同的
3、測試效果,測試的時候先關掉默認虛擬主機,不然其他域名會解析到默認虛擬主機上去
curl測試訪問

[root@lnmp wwwroot]# curl -x192.168.66.132:80 abc.com -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.1
Date: Fri, 06 Jul 2018 03:40:41 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/

[root@lnmp wwwroot]# curl -x192.168.66.132:80 ccc.com -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.1
Date: Fri, 06 Jul 2018 03:40:47 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/

12.10 Nginx訪問日誌

Nginx默認的日誌格式

[root@lnmp conf]# grep -A2 ‘log_format‘ nginx.conf
    log_format combined_realip ‘$remote_addr $http_x_forwarded_for [$time_local]‘
    ‘ $host "$request_uri" $status‘
    ‘ "$http_referer" "$http_user_agent"‘;

#上面grep篩選出來的內容就是是日誌文件的格式,
#也可以自行調整各個變量的位置
#他們分別代表的含義如下:

log_format #定義日誌格式的函數
combined_realip #定義日誌格式名稱,可隨意設定
$remote_addr #客戶端的公網IP
$http_x_forwarded_for #代理服務器的IP
$time_local #服務器本地時間
$host #訪問主機名(域名,網址)
$request_uri #訪問的URI地址
$status #狀態碼
$http_referer #referer
$http_user_agent #user_agent

除了在主配置文件nginx.conf裏定義日誌格式外,還需要在虛擬主機配置文件中增加配置行

access_log logs/test_access.log combined_realip;   

查看修改後的配置

[root@lnmp conf]# cat !$
cat vhost/aaa.com.conf
#server
#{
#    listen 80;
#    server_name www.aaa.com default_server;     
#    index index.html index.htm index.php;
#    root /data/wwwroot/www.aaa.com/;
#}
server
{
    listen 80;
    server_name www.test.com test.com ;
    index index.html index.htm index.php;
    root /data/wwwroot/www.test.com/;
    if ($host != ‘www.test.com‘ )
       { 
        rewrite  ^/(.*)$  http://test.com/$1  permanent;
}
access_log logs/test_access.log combined_realip;
}

##access_log #定義訪問日誌功能
##logs/test_access.log #定義訪問日誌文件的存放路徑
##combined_realip #指定nginx.conf文件中定義的日誌格式名稱

2、檢查語法錯誤,重載配置文件

[root@lnmp conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@lnmp conf]# nginx -s reload

12.11 Nginx日誌切割

nginx沒有自帶的日誌切割工具 ,可以通過自定義腳本配合任務計劃實現日誌切割
腳本內容如下,一般腳本都放在/usr/local/sbin/目錄下

[root@lnmp conf]# vim /usr/local/sbin/nginx_log_rotate.sh
[root@lnmp conf]# cat /usr/local/sbin/nginx_log_rotate.sh
#! /bin/bash
\## 假設nginx的日誌存放路徑為/usr/local/nginx/logs/
d=`date -d "-1 day" +%Y%m%d` 
logdir="/usr/local/nginx/logs"
nginx_pid="/usr/local/nginx/logs/nginx.pid"
cd $logdir
for log in `ls *.log`
do
    mv $log $log-$d
    find -mtime +30 -type f -name "*.log" -exec rm -f {} \;
done
/bin/kill -HUP `cat $nginx_pid`

編寫完日誌切割腳本後,使用crontab -e添加一條任務計劃
每天0點0分執行這個日誌切割腳本

[root@lnmp conf]# crontab -l
0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh

12.12 靜態文件不記錄日誌和過期時間

1、在虛擬主機配置中添加以下內容

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

##添加完檢測語法,重載配置。

配置說明:

location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$
#表示location匹配URI以gif、jpg、png、bmp、swf結尾的訪問請求

#當有匹配到相關的訪問請求時,
expires 7d;
#設定文件緩存到瀏覽器的過期時間7天。
#Nd=N天,
#Nh=N小時
#Nm=N分鐘
#Ns=N秒

access_log off;
    #關閉日誌記錄 

12.13 Nginx防盜鏈

在配置文件中添加以下內容

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

防盜鏈可以和靜態文件結合起來配置。
需要修改前面添加的靜態文件的配置、
修改後aaa.com.conf文件的內容如下:
[root@lnmp conf]# cat !$

cat vhost/test.com.conf
#server
#{
#   listen 80;
#    server_name www.default.com default_server;
#    index index.html index.htm index.php;
#    root /data/wwwroot/www.default.com/;
}
server
{
    listen 80;
    server_name www.test.com test.com abc.com;
    index index.html index.htm index.php;
    root /data/wwwroot/www.test.com/;
    if ($host != ‘www.test.com‘) {
        rewrite ^/(.*)$ http://www.test.com/$1 permanent; 
    }
    location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip|doc|pdf|gz|bz2|jpeg|bmp|xls)$
    {
        expires 7d;
        valid_referers none blocked server_names  *.test.com ;
        if ($invalid_referer) {
        return 403;
        }
        access_log off;
    }

    location ~ .*\.(js|css)$
        {
              expires      12h;
              access_log off;
        }
    location ~ ^.*/admin.html
    {
    auth_basic              "Auth";
    auth_basic_user_file    /usr/local/nginx/conf/htpasswd;
    }
    access_log logs/www_test_com.log combined_realip;
}

防盜鏈配置解釋

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

    #valid_referers 配置referer白名單
    #none 代表沒有referer
    #blocked 代表有referer但是被防火墻或者是代理給去除了
    #server_names 代表這個虛擬主機的所有server_name
    #*.test.com  #這個可以是正則匹配的字段, 或者指定的域名

#當訪問請求不包含在白名單裏面時:
#invalid_referer的值為 1 ,就會執行if語句,
#當訪問請求包含在白名單裏面時,
#invalid_referer的值為 0 就不會執行 if 語句

12.14 Nginx訪問控制

在配置中增加下面的內容

    location /
    {
        allow 127.0.0.1;
        deny all;
    }

配置完檢查語法,重載配置

[root@lnmp conf]# nginx  -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@lnmp conf]# nginx  -s reload

配置說明:
allow 127.0.0.1; #規則,允許ip 127.0.0.1訪問,這裏的ip就是訪問日誌裏的$remote_addr
deny all; #規則,拒絕所有

#也可以配置為allow all;然後deny某些ip
#匹配規則是從上往下匹配,當匹配到一個規則就不再往下匹配了
2、匹配正則

location ~ .*(abc|image)/.*\.php$
{
        deny all;
}

3、根據user_agent限制

if ($http_user_agent ~ ‘Spider/3.0|YoudaoBot|Tomato‘)
{
    return 403;
}

#如此可以拒絕所有user_agent為Spider/3.0、YoudaoBot、Tomato的訪問請求
#deny all和return 403效果一樣

12.15 Nginx解析php相關配置

配置方式
在虛擬主機中添加下面的內容

    location ~ \.php$
        {
            include fastcgi_params;
            fastcgi_pass unix:/tmp/php-fcgi.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com$fastcgi_script_name;
        }

配置解釋:

location ~ .php$ #location匹配所有document_uri以 .php 結尾的訪問請求
{
include fastcgi_params;
#引用fastcgi_params常量文件

    fastcgi_pass unix:/tmp/php-fcgi.sock;  
        #指定PHP的sock文件路徑,
        #如果php-fpm.conf配置listen是ip:port,這裏也需要配置為相同的ip:port
        #這裏配置錯誤會出現502報錯

    fastcgi_index index.php;   
        #指定php的索引頁

    fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com$fastcgi_script_name; 
        #指定PHP程序的請求路徑,  $ 符號前面的路徑需要和虛擬主機的root路徑相同
        #這個路徑有問題會出現404報錯
}

測試效果

創建測試的php文件

[root@lnmp conf]# echo -e "<?php\necho ‘hello‘;" > /data/wwwroot/www.test.com/test.php

重載nginx配置文件

[root@lnmp conf]# curl -x192.168.66.132:80 www.test.com/test.php
hello

如果SCRIPT_FILENAME填寫有誤就會輸出File not found,
獲取頭部信息狀態碼為404

[root@lnmp conf]# curl -x192.168.66.132:80 www.test.com/test.php
File not found.
[root@lnmp conf]# curl -I -x192.168.66.132:80 www.test.com/test.php
HTTP/1.1 404 Not Found

如果fastcgi_pass 填寫有誤會出現502報錯

[root@server-lnmp conf]# curl -I -x192.168.66.132:80 www.test.com/test.php
HTTP/1.1 502 Bad Gateway

12.16 Nginx代理

在配置文件添加

[root@lnmp conf]# vim /usr/local/nginx/conf/vhost/proxy.conf 
[root@lnmp conf]# cat /usr/local/nginx/conf/vhost/proxy.conf
server
{
    listen 80;
    server_name www.aaa.com;         #這裏寫代理服務器的域名
 location /
    {
        proxy_pass      http://192.168.66.131/;  #這裏的IP寫web服務的ip
        proxy_set_header Host   $host;          #設定header信息的Host變量
        proxy_set_header X-Real-IP      $remote_addr;  #設定header信息的remote_addr變量
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #設定header信息的X-Forwarded-For變量
    }
}

檢查語法錯誤,重載配置文件

[root@lnmp conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@lnmp conf]# nginx -s reload

2018-07-04筆記(LNMP配置)