您的位置:首页 > 运维架构 > Nginx

nginx默认虚拟主机,用户认证,域名重定向,日志,静态文件不记录日志,防盗链,访问控制,php解析

2017-11-16 12:46 1336 查看
12.7-12.14 nginx默认虚拟主机,用户认证,域名重定向,日志,,静态文件不记录日志,防盗链,访问控制

默认虚拟主机

nginx默认虚拟主机的概念和apache类似,当域名定向到本主机时,如果本主机没有满足条件的域名,那么就自动用默认主机解析。

nginx除了系统默认主机外,还可以标记默认主机。

vim /usr/local/nginx/conf/nginx.conf
//删掉模块server中的内容,并在结尾的}内添加 include vhost/*.conf;
//意思是本conf路径下/vhost路径内的所有.conf文件都会加载

mkdir /usr/local/nginx/conf/vhost
cd /usr/local/nginx/conf/vhost
vim default.conf
//写入如下内容:
server
{
listen 80 default_server;  // 有这个标记的就是默认虚拟主机
server_name aaa.com;
index index.html index.htm index.php;
root /data/nginx/aaa.com;
}

/usr/local/nginx/sbin/nginx -t //检测配置是否正确
/usr/local/nginx/sbin/nginx -s reload //重载配置文件

echo "default server" > /data/nginx/aaa.com/index.html  //创建索引页

curl -x127.0.0.1:80 aaa.com  //测试
default server   //返回值

curl -x127.0.0.1:80 bbb.com  //测试一个没有定义的域名
default server   //返回值依然是默认虚拟主机


用户认证

用户认证和apache的用户认证类似。

//再创建一个新的虚拟主机
vim /usr/local/nginx/conf/vhost/test.com.conf
//写入如下内容
{
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;   //加密文件位置
}
}

[root@cent01 vhost]# mkdir /data/nginx/test.com  //创建根目录
[root@cent01 vhost]# vim /data/nginx/test.com/index.html
//输入 test.com

[root@cent01 vhost]# /usr/local/apache2.4/bin/htpasswd -c /usr/local/nginx/conf/htpasswd aming //用apache的htpasswd生成密码,如果没有安装apache,可以yum安装httpd再使用htpasswd命令
New password:
Re-type new password:

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

[root@cent01 vhost]# curl -x127.0.0.1:80 test.com  //返回401要求认证
<head><title>401 Authorization Required</title></head>

[root@cent01 vhost]# curl -uaming:1234 -x127.0.0.1:80 test.com  // 测试成功
test.com


//对特定路径做限制
location  /  ,/后面可以接子目录。例如以下表示admin子目录
location  /admin

//现在我们试验只对admin子目录做限制
vim /usr/local/nginx/conf/vhost/test.com.conf
//在location / 后添加admin

[root@cent01 vhost]# mkdir /data/nginx/test.com/admin
[root@cent01 vhost]# vim /data/nginx/test.com/admin/1.html
//输入 test.com/admin/1.html

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

[root@cent01 vhost]# curl -x127.0.0.1:80 test.com  //访问个目录不需要密码
test.com
[root@cent01 vhost]# curl  -x127.0.0.1:80 test.com/admin  //访问admin需要密码
<head><title>401 Authorization Required</title></head>

[root@cent01 vhost]# curl -uaming:1234 -x127.0.0.1:80 test.com/admin/1.html  //访问时输入用户名密码,访问成功
test.com/admin/1.html


域名重定向

在nginx中server_name可以写多个域名。

修改test.com.conf配置文件如下:
server
{
listen 80;
server_name test.com test1.com test2.com;
index index.html index.htm index.php;
root /data/nginx/test.com;
if ($host != 'test.com' ) {
rewrite  ^/(.*)$  http://test.com/$1  permanent;  //permanent相当于301永久重定向。如果写redirect代表302。
}
}

[root@cent01 vhost]# /usr/local/nginx/sbin/nginx -t
[root@cent01 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@cent01 vhost]# curl -x127.0.0.1:80 test1.com -I  //已经重定向
HTTP/1.1 301 Moved Permanently
Server: nginx/1.13.6
Date: Wed, 15 Nov 2017 03:40:02 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/[/code] 

日志与日志切割

[root@cent01 vhost]# vim /usr/local/nginx/conf/nginx.conf  //在主配置文件中查看log_format,发现log的名字叫combined_realip

编辑 test.com.conf,在server括号里添加如下内容:
access_log /tmp/1.log combined_realip; //前面是指定路径,后面是指定日志模板名

[root@cent01 vhost]# /usr/local/nginx/sbin/nginx -t
[root@cent01 vhost]# /usr/local/nginx/sbin/nginx -s reload

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

[root@cent01 vhost]# cat /tmp/1.log  //查看刚才的访问日志
127.0.0.1 - [15/Nov/2017:12:01:34 +0800] test.com "/" 200 "-" "curl/7.29.0"


//nginx没有自动切割日志的工具,我们可以自己写一个脚本
[root@cent01 vhost]# vim /usr/local/sbin/nginx_log_rotate.sh

//写入如下内容:
#! /bin/bash
## 假设nginx的日志存放路径为/tmp/
d=`date -d "-1 day" +%Y%m%d`
logdir="/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`

//添加任务计划
[root@cent01 vhost]# crontab -e
0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh  //任务内容


静态文件不添加日志,设置缓存时间

//在test.com.conf中添加如下语句
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires      7d;
access_log off;
}
location ~ .*\.(js|css)$
{
expires      12h;
access_log off;
}

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

[root@cent01 vhost]#cd /data/nginx/test.com/
[root@cent01 test.com]# touch 1.js 1.jpg 1.html //添加测试文件

[root@cent01 test.com]# curl -x127.0.0.1:80 test.com/1.jpg -I  //缓存时间已生效
HTTP/1.1 200 OK
Expires: Wed, 22 Nov 2017 04:48:50 GMT
Cache-Control: max-age=604800

[root@cent01 test.com]# curl -x127.0.0.1:80 test.com/1.js -I
HTTP/1.1 200 OK
Expires: Wed, 15 Nov 2017 16:49:03 GMT
Cache-Control: max-age=43200

[root@cent01 test.com]# cat /tmp/1.log  //看不到jpg和js文件的日志
127.0.0.1 - [15/Nov/2017:12:45:09 +0800] test.com "/1.php" 404 "-" "curl/7.29.0"
127.0.0.1 - [15/Nov/2017:12:45:16 +0800] test.com "/1.html" 404 "-" "curl/7.29.0"
127.0.0.1 - [15/Nov/2017:12:47:57 +0800] test.com "/1.html" 200 "-" "curl/7.29.0"


防盗链

防盗链和静态文件不记录日志有重合。所以我们这样修改静态文件部分的语句。

[root@cent01 ~]# 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;
}
access_log off;
}

[root@cent01 ~]# /usr/local/nginx/sbin/nginx -t
[root@cent01 ~]# /usr/local/nginx/sbin/nginx -s reload

[root@cent01 ~]# curl -e "http://www.badi.com" -x127.0.0.1:80 test.com/1.jpg -I  //访问图片被阻止
HTTP/1.1 403 Forbidden

[root@cent01 ~]# curl -e "http://www.badi.com" -x127.0.0.1:80 test.com -I //访问其他页面正常
HTTP/1.1 200 OK


访问控制

//以下是对admin目录做限制,只允许一些ip可以访问
location /admin/
{
allow 192.168.27.128;
allow 127.0.0.1;
deny all;
}

[root@cent01 ~]# /usr/local/nginx/sbin/nginx -t
[root@cent01 ~]# /usr/local/nginx/sbin/nginx -s reload

[root@cent01 ~]# curl -x127.0.0.1:80 test.com/admin/1.html -I
HTTP/1.1 200 OK

//但是从ip为192.168.0.100的电脑访问,则显示403。说明限制成功。

//还可以对指定的ip做限制,如果是网段可以写成192.168.0.1/24
location /admin/
{
allow 192.168.27.128;
allow 127.0.0.1;
deny all;
}


//针对浏览器做限制
if ($http_user_agent ~* 'Spider/3.0|YoudaoBot|Tomato')
{
return 403;
}

~表示匹配
*表示不区分大小写,否则区分

[root@cent01 ~]# /usr/local/nginx/sbin/nginx -t
[root@cent01 ~]# /usr/local/nginx/sbin/nginx -s reload

[root@cent01 ~]# curl -A "Tomato" -x127.0.0.1:80 test.com/admin/1.html -I
HTTP/1.1 403 Forbidden

[root@cent01 ~]# curl -A "tomato" -x127.0.0.1:80 test.com/admin/1.html -I
HTTP/1.1 403 Forbidden


//用正则做限制
location ~ .*(upload|image)/.*\.php$
{
deny all;
}

[root@cent01 ~]# /usr/local/nginx/sbin/nginx -t
[root@cent01 ~]# /usr/local/nginx/sbin/nginx -s reload

[root@cent01 ~]# curl -x127.0.0.1:80 test.com/upload/1.php -I  //路径中带有upload的php文件禁止访问,说明成功
HTTP/1.1 403 Forbidden


添加php解析

当没有设置php解析时,浏览器不能解析php,就会把php页面当文件下载到本地。不能正常展示出效果。

//在test.com.conf文件中添加如下内容
location ~ \.php$
{
include fastcgi_params;
fastcgi_pass unix:/tmp/php-fcgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/nginx/test.com$fastcgi_script_name;
}

需要确保这里的内容和/usr/local/php-fpm/etc/php-fpm.conf 的内容一致,否则会报错。

fastcig_pass用来指定php-fpm的地址,如果php-fpm监听的是一个端口,如127.0.0.1:9000,那么这里也应该改成fastcig_pass 127.0.0.1:9000,否则会报502错误。

fastcgi_param SCRIPT_FILENAME 后面应该和网站的根目录一致,否则会报404错误。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐