Nginx默认虚拟主机,Nginx用户认证,Nginx重定向,Nginx访问日志,Nginx防盗链,Nginx访问控制,Nginx解析PHP

nginx默认虚拟主机

设置Nginx默认虚拟主机,其实默认就设置了。在Nginx的配置文件中,直接添加server就是,一般的,你有几个网站就设置几个server。还有另一种设置方式,在配置文件中不要去设置server,直接重新写一个虚拟主机配置文件(vhost/*.conf).,在配置文件中加上include vhost/ *.conf

编辑配置文件,把Nginx配置文件中server段删去,添加一段:

include vhost/*.conf;

这里写图片描述

这里写图片描述

在/usr/local/nginx/conf/目录下,创建一个目录(vhost),并在目录下创建一个新文件。这个vhost就类似于Apache中的虚拟配置文件。

[root@shuai-01 conf]# mkdir vhost
[root@shuai-01 conf]# cd vhost/
[root@shuai-01 vhost]# ls
[root@shuai-01 vhost]# vim aaa.com.conf

这里写图片描述

server
{
    listen 80 default_server;  // 有这个标记的就是默认虚拟主机
    server_name aaa.com;
    index index.html index.htm index.php;
    root /data/wwwroot/default;
}

创建/data/wwwroot/default,并在defualt目录下编写index.html文件:

[root@shuai-01 vhost]# mkdir /data/wwwroot/default
[root@shuai-01 vhost]# cd /data/wwwroot/default/
[root@shuai-01 default]# vim index.html

echo “This is a default site.”>/data/wwwroot/default/index.html

检测一下配置文件语法

[root@shuai-01 default]# /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@shuai-01 default]# /usr/local/nginx/sbin/nginx -s reload

一般的,在服务器跑动的时候,都选择重新加载配置文件,而不是重启服务(/etc/init.d/nginx restart),重启服务会短暂关闭然后在启动。

测试默认主机:

[root@shuai-01 default]# curl localhost
this is a default site

默认虚拟主机就是只要你解析过来是这个IP,不管什么域名,都会访问到默认虚拟主机。

[root@shuai-01 default]# curl -x127.0.0.1:80 bbb.com
this is a default site
[root@shuai-01 default]# curl -x127.0.0.1:80 abadsj.com
this is a default site

Nginx用户认证

做用户认证就是为了安全,在做httpd的用户认证时就已经说到过,可以将两篇帖子结合起来看。
http://blog.csdn.net/aoli_shuai/article/details/78854280

用户认证步骤:

  1. 确认机器安装过htpasswd命令(httpd的)
  2. 没安装htpaswdd安装后,在server中加上配置location,那些站点,目录,页面需要用户认证,密码文件的存放
  3. 通过命令为用户设置密码 /usr/local/apache2.4/bin/htpasswd -c /usr/local/nginx/conf/htpasswd shuai

创建一个虚拟主机(test.com.conf):

[root@shuai-01 wwwroot]# cd /usr/local/nginx/conf/vhost/
[root@shuai-01 vhost]# ls
aaa.com.conf
[root@shuai-01 vhost]# vim test.com.conf

写入:

server
{
    listen 80;
    server_name test.com;
    index index.html index.htm index.php;
    root /data/wwwroot/test.com;
    
    location  /
     {
        auth_basic              "Auth";
        auth_basic_user_file   /usr/local/nginx/conf/htpasswd;//用户名密码文件
     }
}

生成密码的工具是htpasswd,这个工具在Apache用户认证时就安装过了,没安装的就用yum install -y httpd 安装上。

为shuai用户做用户认证:

[root@shuai-01 vhost]# /usr/local/apache2.4/bin/htpasswd  -c /usr/local/nginx/conf/htpasswd shuai
New password: 
Re-type new password: 
Adding password for user shuai

为aoli用户做认证:

[root@shuai-01 vhost]# /usr/local/apache2.4/bin/htpasswd  /usr/local/nginx/conf/htpasswd aoli
New password: 
Re-type new password: 
Adding password for user aoli
[root@shuai-01 vhost]# cat /usr/local/nginx/conf/htpasswd 
shuai:$apr1$V0AiFAbc$CSttuCLed5mA1AMi1mKdw/
aoli:$apr1$OX2YLAuw$z1XF5XGLO/Z15Qw5dFo0V0

检查配置文件语法并重新加载配置文件:

[root@shuai-01 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@shuai-01 vhost]# /usr/local/nginx/sbin/nginx -s reload

测试:

[root@shuai-01 vhost]# curl -x127.0.0.1:80 test.com
<html>
<head><title>401 Authorization Required</title></head>
<body bgcolor="white">
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx/1.12.2</center>
</body>
</html>

出现401,需要用户认证。

[root@shuai-01 vhost]# curl -ushuai:123456 -x127.0.0.1:80 test.com
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.12.2</center>
</body>
</html>

出现404,没有文件。
写一个index.html文件。

[root@shuai-01 vhost]# mkdir /data/wwwroot/test.com
[root@shuai-01 vhost]# echo "test.com" > /data/wwwroot/test.com/index.html
[root@shuai-01 vhost]# curl -ushuai:123456 -x127.0.0.1:80 test.com
test.com

这个用户认证时针对整个站点,只针对某个特定目录的用户认证。针对admin目录。

修改虚拟配置文件:

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

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

这里写图片描述

检查配置文件语法并重新加载配置文件:

[root@shuai-01 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@shuai-01 vhost]# /usr/local/nginx/sbin/nginx -s reload

测试:

访问test.com

[root@shuai-01 vhost]# curl -x127.0.0.1:80 test.com
test.com

访问test.com/admin

[root@shuai-01 vhost]# curl -x127.0.0.1:80 test.com/admin
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.12.2</center>
</body>
</html>

针对某个.php文件

配置文件写成

location ~ admin.php

nginx域名重定向

域名重定向是主机设置多个域名,将访问的请求不是主域名的定向到主域名去

域名重定向步骤

  1. server_name中设置多个域名
  2. 配置文件中加上规则

更改虚拟配置文件

[root@shuai-01 vhost]# vim test.com.conf

server
{
    listen 80;
    server_name test.com test1.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;
    } 
    location  /admin/           
     {  
        auth_basic              "Auth";
        auth_basic_user_file   /usr/local/nginx/conf/htpasswd;
     }
}

这里写图片描述

这里多个域名都可以写到server_name 后面,不像httpd,有server_alias .
这里的permanent表示301跳转。
redirect 表示302

检查配置文件语法并重新加载配置文件:

	[root@shuai-01 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@shuai-01 vhost]# /usr/local/nginx/sbin/nginx -s reload

测试:

[root@shuai-01 vhost]# curl -x127.0.0.1:80 test2.com/index.html -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.2
Date: Mon, 08 Jan 2018 11:16:30 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/index.html

[root@shuai-01 vhost]# curl -x127.0.0.1:80 test3.com/index.html -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.2
Date: Mon, 08 Jan 2018 11:16:42 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/index.html

[root@shuai-01 vhost]# curl -x127.0.0.1:80 test4.com/index.html -I
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Mon, 08 Jan 2018 11:16:49 GMT
Content-Type: text/html
Content-Length: 23
Last-Modified: Mon, 08 Jan 2018 07:52:14 GMT
Connection: keep-alive
ETag: "5a53232e-17"
Accept-Ranges: bytes

测试test2.com test3.com 都是301重定向,test4.com 时,访问就是默认虚拟主机。

nginx rewrite四种flag http://www.netingcn.com/nginx-rewrite-flag.html

Nginx的访问日志

Nginx的日志格式是在Nginx的主配置文件中(/usr/local/nginx/conf/nginx.conf)

[root@shuai-01 vhost]# vim /usr/local/nginx/conf/nginx.conf

这里写图片描述

可以将日志格式名称改一下,改为shaui

这里写图片描述

Nginx日志字段的含义

这里写图片描述

在主配置文件中定义日志的格式,在虚拟主机配置文件中定义日志路径。

打开虚拟主机配置文件

[root@shuai-01 vhost]# vim test.com.conf 

access_log /tmp/test.com.log shuai;

这里写图片描述
注意,Nginx配置文件写完一行要加“;”,不然就是错误。

检查配置文件语法并重新加载配置文件

[root@shuai-01 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@shuai-01 vhost]# /usr/local/nginx/sbin/nginx -s reload

检测:

[root@shuai-01 vhost]# curl -x127.0.0.1:80 test2.com/index.html -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.2
Date: Mon, 08 Jan 2018 12:41:20 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/index.html

[root@shuai-01 vhost]# curl -x127.0.0.1:80 test3.com/index.html -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.2
Date: Mon, 08 Jan 2018 12:41:26 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/index.html

[root@shuai-01 vhost]# cat /tmp/test.com.log 
127.0.0.1 - [08/Jan/2018:20:41:20 +0800] test2.com "/index.html" 301 "-" "curl/7.29.0"
127.0.0.1 - [08/Jan/2018:20:41:26 +0800] test3.com "/index.html" 301 "-" "curl/7.29.0"

Nginx日志切割

nginx由于没有自带的日志切割工具,在切割日志时,需要借助于系统带的日志切割工具,或者是自己写一个日志切割脚本。

自己写一个日志切割脚本。脚本统一保存/usr/local/sbin/
先自定义一个脚本:

[root@shuai-01 vhost]# vim /usr/local/sbin/nginx_logrotate.sh


#! /bin/bash
## 假设nginx的日志存放路径为/tmp/
d=`date -d "-1 day" +%Y%m%d` 
#定义切割时间(切割一天前的日志)
logdir="/tmp/"
#此处指定要切割的日志路径(该路径来自虚拟主机配置文件)
nginx_pid="/usr/local/nginx/logs/nginx.pid"
#调用pid的目的是执行命令:/bin/kill -HUP `cat $nginx_pid`
#该命令等价于命令:nginx -s reload(重新加载文件),确保与虚拟主机配置文件变更保持同步
#该地址来自nginx配置文件
cd $logdir
for log in `ls *.log`
do
    mv $log $log-$d
done
#此处使用通配进行循环,并改名字(切割是每天产生的日志重命名)
/bin/kill -HUP `cat $nginx_pid`
#执行此命令进行重载生成新的日志文件来记录新的日志

执行脚本:

[root@shuai-01 vhost]# sh -x /usr/local/sbin/nginx_logrotate.sh 
++ date -d '-1 day' +%Y%m%d
+ d=20180108
+ logdir=/tmp/
+ nginx_pid=/usr/local/nginx/logs/nginx.pid
+ cd /tmp/
++ ls test.com.log
+ for log in '`ls *.log`'
+ mv test.com.log test.com.log-20180108
++ cat /usr/local/nginx/logs/nginx.pid
+ /bin/kill -HUP 1513

-x : 作用是显示脚本执行过程
注意:
这只是对日志进行了切割,对日志进行删除需要结合任务计划cron使用。切割也得配合cron使用。

切割之后,将大于一个月的日志文件删除:

[root@shuaiaoli test.log]# find /usr/local/nginx/logs/test.log/ -type f -name *.log-* -mtime +30 |xargs rm 

静态文件不记录日志和过期时间

在配置文件中加上配置:

打开配置文件:

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



[root@shuai-01 vhost]# vim test.com.conf

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
 #匹配文件类型
{
      expires      7d;
      #过期时间为7天
      access_log off;  
      #不记录该类型文件的访问日志     
}   
location ~ .*\.(js|css)$
{
      expires      12h;
      #过期时间为12小时
      access_log off;
      #不记录该类型文件的访问日志
}

这里写图片描述

检查配置文件语法并重新加载配置文件:

[root@shuai-01 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@shuai-01 vhost]# /usr/local/nginx/sbin/nginx -s reload

测试:

[root@shuai-01 test.com]# curl -x127.0.0.1:80 test.com/1.gif
shjdkjhkasb
[root@shuai-01 test.com]# curl -x127.0.0.1:80 test.com/2.js
ajkfdchb
[root@shuai-01 test.com]# curl -x127.0.0.1:80 test.com/index.html
test.com
[root@shuai-01 test.com]# cat /tmp/test.com.log
127.0.0.1 - [09/Jan/2018:00:39:45 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"

Nginx防盗链

Nginx防盗链也是使用location板块,和不记录静态文件和过期时间写在一起。

防盗链的步骤:

  1. 设置referer白名单
  2. 将不是白名单的referer全部拒绝

打开虚拟主机配置文件

[root@shuai-01 ~]# vim /usr/local/nginx/conf/vhost/test.com.conf 
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;
}
#不是白名单的referer ,返回403
access_log off;
}

这里写图片描述

注意:location ~* ^.+.这里匹配到的后面的内容是不区分大小写。

测查配置文件语法并重新加载:

[root@shuai-01 ~]# /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@shuai-01 ~]# /usr/local/nginx/sbin/nginx -s reload

测试:

[root@shuai-01 ~]# curl -e "http://www.qq.com/1.txt" -x127.0.0.1:80 -I test.com/1.gif
HTTP/1.1 403 Forbidden
Server: nginx/1.12.2
Date: Tue, 09 Jan 2018 11:00:19 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive

[root@shuai-01 ~]# curl -e "http://www.test.com/1.txt" -x127.0.0.1:80 -I test.com/1.gif
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Tue, 09 Jan 2018 11:01:15 GMT
Content-Type: image/gif
Content-Length: 12
Last-Modified: Mon, 08 Jan 2018 16:38:47 GMT
Connection: keep-alive
ETag: "5a539e97-c"
Expires: Tue, 16 Jan 2018 11:01:15 GMT
Cache-Control: max-age=604800
Accept-Ranges: bytes

Nginx的访问控制

关于做防盗链和访问控制的原因我在httpd做访问控制和防盗链时已经说得比较清楚了。
http://blog.csdn.net/aoli_shuai/article/details/78895746
访问控制的步骤:

  1. 设置IP白名单

需求:访问/admin/目录,只允许那几个IP进行访问。

打开虚拟主机配置文件

location /admin/
{
    allow 127.0.0.1;
    allow 192.168.176.135;
    deny all;
}

这里写图片描述
这里Nginx设置访问控制和Apache是有很大的不同,关于这个allow,deny。Apache是有一个顺序的,Nginx没有。

测查配置文件语法并重新加载:

[root@shuai-01 ~]# /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@shuai-01 ~]# /usr/local/nginx/sbin/nginx -s reload

检测:

[root@shuai-01 ~]# curl -e "http://www.test.com/1.txt" -x127.0.0.1:80 -I test.com/admin/
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Tue, 09 Jan 2018 11:23:23 GMT
Content-Type: text/html
Content-Length: 5
Last-Modified: Tue, 09 Jan 2018 11:23:20 GMT
Connection: keep-alive
ETag: "5a54a628-5"
Accept-Ranges: bytes

[root@shuai-01 ~]# curl -x192.168.176.135:80 test.com/admin/ -I
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Tue, 09 Jan 2018 11:25:02 GMT
Content-Type: text/html
Content-Length: 5
Last-Modified: Tue, 09 Jan 2018 11:23:20 GMT
Connection: keep-alive
ETag: "5a54a628-5"
Accept-Ranges: bytes

针对PHP解析做的访问控制(不让用户传上来的PHP文件做解析)

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

针对user_agent进行限制:

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

return 403 和deny all 效果是一样的。

Nginx解析PHP的相关配置

核心配置:

location ~ \.php$
    {
        include fastcgi_params;
        fastcgi_pass unix:/tmp/php-fcgi.sock;
        # fastcgi_pass 127.0.0.1:9000; 如果php-fpm配置监听配置的是IP就写这个
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com$fastcgi_script_name;
    }

Nginx要想解析PHP就要将这段核心配置写入配置文件中去。

fastcgi_pass 用来指定php-fpm监听的地址或者socket
如果是用的sock那么一定要放开php配置中的listen.mode=666(sock的权限位一定要有写的权限)
unix:/tmp/php-fcgi.sock这里的sock文件是php-fpm.conf中定义的
cat /usr/local/php-fpm/etc/php-fpm.conf配置文件中写什么就定义什么
如果php监听的是ip和端口,nginx中的配置文件就要改成
fastcgi_pass 127.0.0.1:9000;
fastcgi_param 中的路径也需要跟上面对应起来

Nginx502问题出现及解决方法:

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值