下面我们接着对nginx进行基本的配置
1、nginx的用户认证
需求:对管理中心登入界面进行二次验证,网址为http://www.test.com/admin.php,即对访问admin.php文件进行验证。
(1)用户认证需要用到htpasswd工具来生成密码文件,这个工具是Apache的,所以我们要先安装Apache。
[iyunv@tpp ~]# yum install -y httpd
[iyunv@tpp ~]# which htpasswd //查看htpasswd路径
/usr/bin/htpasswd
接着创建tpp用户,并生成一个密码文件
[iyunv@tpp ~]# /usr/bin/htpasswd -c /usr/local/nginx/conf/.htpasswd tpp
(2)配置虚拟主机文件
[iyunv@tpp ~]# vim /usr/local/nginx/conf/vhosts/test.conf //添加如下代码
location ~ .*admin\.php$ {
auth_basic "testlinux auth";
auth_basic_user_file /usr/local/nginx/conf/.htpasswd;
include fastcgi_params;
fastcgi_pass unix:/tmp/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;
}
如下图:
我们重新加载下nginx
[iyunv@tpp ~]# /etc/init.d/nginx reload
注:如果只是对目录,这个目录必须是网站的根目录下的目录,例:/data/www/abc/
location ~ /abc/ { auth_basic "testlinux auth"; auth_basic_user_file /usr/local/nginx/conf/.htpasswd; }
(3)验证
我们再次访问管理中心时候,就会提示要进行认证。如下图:
验证成功后,我们就可以进入管理中心的登入页面了。
注:如果没有跳出验证界面,可能就是我们的浏览器有缓存,我们清理下就行了。或者你也可以用curl来进行测试:
[iyunv@tpp ~]# curl -x127.0.0.1:80 -utpp:123456 www.test.com/admin.php
当出现了网页代码就表示成功了,如下图:
2、nginx域名跳转
实现:找到本机 C:\Windows\System32\drivers\etc下的hosts文件,修改下面内容后保存退出。 192.168.0.104 www.tpp.com www.aaa.com www.bbb.com
配置虚拟主机文件
[iyunv@tpp ~]# vim /usr/local/nginx/conf/vhosts/test.conf
server_name www.test.com www.aaa.com www.bbb.com;
if ($host != 'www.test.com')
{
rewrite ^/(.*)$ http://www.test.com/$1 permanent;
}
如图:
保存退出后,重新加载nginx
[iyunv@tpp ~]# /etc/init.d/nginx reload
测试下:
[iyunv@tpp ~]# curl -x127.0.0.1:80 www.aaa.com -I
HTTP/1.1 301 Moved Permanently //301状态
Server: nginx/1.4.4
Date: Mon, 07 Sep 2015 21:08:28 GMT
Content-Type: text/html
Content-Length: 184
Connection: keep-alive
Location: http://www.test.com/ //成功跳转
下面我们在浏览器输入www.aaa.com或者[url]www.bbb.com[/url]时,会自动跳转到www.test.com去。
3、nginx不记录指定文件类型日志
日志
|