centos7.4 源码搭建LNMP___Nginx配置——1:虚拟主机、用户认证、域名重定向、访问日志、静态文件

学习笔记

1、默认虚拟主机

修改配置文件

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

在最后倒数第二行添加 " include vhost/*.conf; "  (把vhost下面的所有以.conf结尾的文件都会加载,这样就可以把所有虚拟主机配置文件放到vhost目录下)

 

创建vhost,编辑配置文件

mkdir /usr/local/nginx/conf/vhost
cd /usr/local/nginx/conf/vhost/
vim default.conf
#添加以下代码
server
{
    listen 80 default_server;
    server_name cheese.com;
    index index.html index.htm index.php;
    root /data/nginx/default;
}

检验是否有错误

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

重启

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

创建索引页

echo " test.com " > /data/nginx/test.com/index.html

访问

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

 

 

2、用户认证

创建一个新的虚拟主机

cd /usr/local/nginx/conf/vhost/
vim default.conf
#添加以下内容
server
{   listen 80;
           server_name test.com;
           index index.html index.htm index.php;
           root /data/nginx/test.com;

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

auth_basic打开认证,auth_basic_user_file指定用户密码文件

借助httpd的htpasswd:

yum install -y httpd
htpasswd -c /usr/local/nginx/conf/htpasswd admin

检验是否有错误,重启

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

用curl命令验证

mkdir /data/nginx/test.com
echo " test.com " > /data/nginx/test.com/index.html
curl -I -x127.0.0.1:80 test.com

出现401说明需要验证,用浏览器就可以进行认证

 

对目录做用户认证,在location 后面加上路径就行

vim /usr/local/nginx/conf/vhost/test.com.conf
server
{   listen 80;
           server_name test.com;
           index index.html index.htm index.php;
           root /data/nginx/test.com;

           location  /admin/
           {
               auth_basic              "Auth";
               auth_basic_user_file   /usr/local/nginx/conf/htpasswd;
           }
}
echo "directory auth" > /data/nginx/test.com/admin/index.html

 

3、域名重定向

编辑配置文件

vim /usr/local/nginx/conf/vhost/test.com.conf
server
{   listen 80;
           server_name test.com test1.com;
           index index.html index.htm index.php;
           root /data/nginx/test.com;


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

server_name 后面可以跟多个域名,permanent 为永久重定向,相当于httpd的R=301

 

检测、重启、测试

/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload
curl -x127.0.0.1:80  test1.com/123.txt -I

结果

 

4、Nginx访问日志

编辑配置文件

vim /usr/local/nginx/conf/vhost/test.com.conf

添加access_log /tmp/test.log combined_realip;         

server
{   listen 80;
           server_name test.com test1.com;
           index index.html index.htm index.php;
           root /data/nginx/test.com;


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

           access_log /tmp/test.log combined_realip;
}

access_log来指定日志的存储位置,最后面的是指定日志的格式名字

 

验证、重启、验证

/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload
curl -x127.0.0.1:80 test.com/test

查看

cat /tmp/test.log

 

切割Nginx日志需借助系统的切割工具或自定义脚本

vim /usr/local/sbin/nginx_log_rotate.sh
#添加以下内容
#!/bin/bash
d=`date -d "-1 day" +%Y%m%d`
logdir="/tmp"                                            #logdir是nginx日志存放的路径为/tmp
nginx_pid="/usr/local/nginx/logs/nginx.pid"
cd $logdir
for log in `ls *.log`
do
    mv $log $log-$d
done
/bin/kill -HUP `cat $nginx_pid`

增加任务计划

/bin/bash /usr/local/sbin/nginx_log_rotate.sh

 

5、配置静态文件不记录日志并添加过期时间

编辑配置文件

vim /usr/local/nginx/conf/vhost/test.com.conf
server
{   listen 80;
           server_name test.com test1.com;
           index index.html index.htm index.php;
           root /data/nginx/test.com;

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


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

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

            location ~ .*\.(js|css)$
           {
                 expires      12h;
                 access_log off;
           }
           access_log /tmp/test.log combined_realip;
}

location ~ 可以指定对应的静态文件,expires配置过期时间,access_log off 就可以不记录访问日志了

 

测试(创建js、jpg、jss文件)

/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload
echo "ok is ok" > /data/nginx/test.com/01.js
echo "good is good" > /data/nginx/test.com/02.jpg
touch /data/nginx/test.com/01.jss

验证

curl -I -x127.0.0.1:80 test.com/01.js
curl -I -x127.0.0.1:80 test.com/02.jpg
curl -I -x127.0.0.1:80 test.com/01.jss

  

可看到Cache-control对应的时间大小,也可以看下访问日志

cat /tmp/test.log

  

可以看出只有jss,没有js和jpg

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值