2018-07-04笔记(LNMP配置)

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

猜你喜欢

转载自blog.51cto.com/13736286/2137086
今日推荐