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

学习笔记

1、默认虚拟主机

修改配置文件

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

 

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

https://img-blog.csdnimg.cn/20190416130300820.png

 

创建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

结果

https://img-blog.csdnimg.cn/20190416161621307.png

 

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

  https://img-blog.csdnimg.cn/20190417131843715.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3V1aWNvbg==,size_16,color_FFFFFF,t_70

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

cat /tmp/test.log

 

  https://img-blog.csdnimg.cn/20190417132119820.png

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

发布了18 篇原创文章 · 获赞 0 · 访问量 297

猜你喜欢

转载自blog.csdn.net/m0_46400538/article/details/104495152
今日推荐