在Nginx中也有默认虚拟主机,跟httpd相似,第一个被Nginx加载的虚拟主机就是默认主机,但和httpd不相同的地方是,它还有一个配置用来标记默认虚拟主机,也就是说,若是没有这个标记,第一个虚拟主机为默认虚拟主机。php
设置nginx默认虚拟主机css
vim /usr/local/nginx/conf/nginx.conf
html
删除如下标红的地方:nginx
添加如下内容:shell
include vhost/*.conf;
vim
建立虚拟主机文件浏览器
建立vohost文件缓存
mkdir /usr/local/nginx/conf/vhost
bash
vim test.conf
服务器
添加如下内容:
server { #default_server 表示就是一个虚拟主机 listen 80 default_server; server_name wxy.com; index index.html index.htm index.php; root /data/wwwroot/wxy.com; }
建立默认目录
mkdir -p /data/wwwroot/wxy.com/
新建index.html
vim /data/wwwroot/wxy.com/index.html
添加如下内容:
this is test
检查配置文件
./nginx -t
从新加载
./nginx -s reload
测试
curl -x 127.0.0.1:80 wxy.com
修改虚拟主机conf配置文件
添加如下内容:
location / { auth_basic "Auth"; auth_basic_user_file /usr/local/nginx/conf/htpasswd; }
建立用户
使用httpd来建立,若是没法使用则须要安装(yum install httpd
)
htpasswd -c /usr/local/nginx/conf/htpasswd wxy
测试
检查是否有错误
/usr/local/nginx/sbin/nginx -t
从新加载
/usr/local/nginx/sbin/nginx -s reload
测试:
curl -uwxy:123 -x127.0.0.1:80 test.com -I
对某个目录进行认证
修改虚拟主机配置文件
把location /改成location /admin
location /admin/
正常访问
curl -x 127.0.0.1:80 test.com
添加admin目录,并建立index.html
mkdir /data/wwwroot/wxy.com/admin/
echo "test admin " > /data/wwwroot/wxy.com/admin/index.html
测试admin目录访问
针对某个文件进行认证
修改虚拟主机文件
~ php:表示匹配php文件
测试
正常访问
访问php类型
修改虚拟主机文件
修改内容以下:
server { listen 80; server_name test.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; } }
permanent:永久跳转,301
redirect:临时跳转,302
测试
检查
从新加载
../../sbin/nginx -s reload
nginx日志配置
nginx日志在nginx.conf文件中配置
nginx日志变量意义
名称 | 解释 |
---|---|
$remote_addr | 客户端ip(公网ip) |
$http_x_forwarded_for | 代理服务器的ip |
$time_local | 服务器本地时间 |
$host | 访问主机名(域名) |
$request_uri | 访问的url地址 |
$status | 状态码 |
$http_referer | referer |
$http_user_agent | user_agent |
在虚拟主机中配置日志格式
在虚拟主机配置文件中添加如下内容:
access_log /tmp/wxy.log combined_realip;
/tmp/wxy.log是日志路径
combined_realip是日志格式,在nginx.conf中log_format后面自定义
访问并查看日志
nginx没有本身的切割工具,咱们使用脚原本实现
vim /usr/local/sbin/nginx_logrotate.sh
脚本内容:
#! /bin/bash #定义前一天日期 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 #从新加载nginx,不然不会生成新的日志文件。相似(nginx -s relod) /bin/kill -HUP `cat $nginx_pid`
测试运行脚本
sh -x /usr/local/sbin/nginx_logrotate.sh
ll /tmp/wxy.log*
建立定时任务
添加一个天天0点执行此脚本的定时任务
crontab -e
0 0 * * * /usr/local/sbin/nginx_logrotate.sh
清理日志
如清理30天之前的日志
find /tmp/ -type f -name "*.log-" -mtime +30 |xargs rm
静态文件不记录日志和浏览器缓存时间
虚拟主机配置文件location~能够指定对应的静态文件,expires配置过时时间,而access_log 配置为off就能够不记录访问日志了
修改配置虚拟主机文件
修改内容以下:
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 7d; access_log off; } location ~ .*\.(js|css)$ { expires 12h; access_log off; }
expires 7d:表示过时时间7天
expires 12:表示过时时间12小时
测试
模拟gif和js文件
echo "gif gif gig" > /data/wwwroot/wxy.com/1.gif
echo "js js js " >/data/wwwroot/wxy.com/2.js
检查nginx配置文件,并从新加载
../../sbin/nginx -t
../../sbin/nginx -s reload
开始测试
curl -x 127.0.0.1:80 wxy.com/1.gif -I
curl -x 127.0.0.1:80 wxy.com/2.js -I
查看日志并无生成gif和js的日志
修改虚拟主机配置文件
修改如下内容
valid_referers none blocked server_names *.wxy.com; if ($invalid_referer) { return 403; }
测试
无效refer返回403
curl -e "http://www.qq.com/1.jpg" -x 127.0.0.1:80 wxy.com/2.jpg
正常的
curl -e "wxy.com/1.jpg" -x 127.0.0.1:80 wxy.com/1.gif
设置ip白名单
1.修改nginx虚拟主机配置文件
添加如下内容
location /admin/ { allow 127.0.0.1; allow 47.106.84.56; deny all; }
测试
使用127.0.0.1访问正常
curl -x 127.0.0.1:80 wxy.com/admin/ -I
在47.106.84.56上访问正常
在其它机器访问403
针对文件进行控制
在上传目录upload和image,禁止.php的文件
修改虚拟配置文件
添加如下内容:
location ~ .*(upload|image)/.*\.php$ { deny all; }
测试
在upload目录建立1.txt和1.php文件,可以访问1.txt,不可以访问1.php
1.txt 能够访问,1.php访问403
根据user-agent进行限制
1.修改虚拟主机配置文件
添加如下内容
if ($http_user_agent ~* 'Spider/3.0|YoudaoBot|Tomato') { return 403; }
使用user_agent设置的三个用户访问都报403
使用其它用户访问正常
代理分为:正向代理、反向代理。正向通常不使用,主要仍是使用反向代理。反向代理(Reverse Proxy)方式是指以代理服务器来接受Internet上的链接请求,而后将请求转发给内部网络上的服务器;并将从服务器上获得的结果返回给Internet上请求链接的客户端,此时代理服务器对外就表现为一个服务器。
新建配置文件
vim /usr/local/nginx/conf/vhost/proxy.conf
添加如下内容:
server { listen 80; server_name ask.apelearn.com; location / { proxy_pass http://47.104.7.242/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
测试
检查配置文件
../../sbin/nginx -t
从新加载配置文件
../../sbin/nginx -s reload
curl测试
curl -x 127.0.0.1:80 ask.apelearn.com -I
测试robots
curl ask.apelearn.com/robots.txt