11月26日学习笔记
钉钉、微博极速扩容黑科技,点击观看阿里云弹性计算年度发布会!>>>
12.6 Nginx安装
12.7 默认虚拟主机
12.8 Nginx用户认证
12.9 Nginx域名重定向
Nginx安装
cd /usr/local/src
wget http://nginx.org/download/nginx-1.12.1.tar.gz
cd /usr/local/src/nginx-1.12.1
tar zxf nginx-1.12.1.tar.gz
./configure --prefix=/usr/local/nginx
make && make install
vim /etc/init.d/nginx //编辑启动脚本,复制如下内容(参考https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/etc_init.d_nginx )
chmod 755 /etc/init.d/nginx
chkconfig --add nginx
chkconfig nginx on
cd /usr/local/nginx/conf/;
mv nginx.conf nginx.conf.bak
vim nginx.conf //,编辑配置文件,写入如下内容(参考https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/nginx.conf) /usr/local/nginx/sbin/nginx -t
/etc/init.d/nginx start netstat -lntp |grep 80
测试是否解析php文件
创建测试文件:
vim /usr/local/nginx/html/2.php
内容如下:
<?php echo "测试php是否解析"; ?>
测试:
[root@localhost nginx]# curl localhost/2.php 测试php是否解析[root@localhost nginx]#
显示成这样,才说明php解析正确。
只是扩充: http://www.aminglinux.com/bbs/thread-5442-1-1.html
Nginx默认虚拟主机
vim /usr/local/nginx/conf/nginx.conf //增加,把 application/xml下面的内容删除,再添加下面include这一条。
include vhost/*.conf
mkdir /usr/local/nginx/conf/vhost
cd !$; vim default.conf //加入如下内容
server
{
listen 80 default_server; // 有这个标记的就是默认虚拟主机
server_name aaa.com;
index index.html index.htm index.php;
root /data/wwwroot/default;
}
mkdir -p /data/wwwroot/default/
echo “This is a default site.”>/data/wwwroot/default/index.html
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload //重新加载nginx
curl localhost curl -x127.0.0.1:80 123.com // 由于设置了默认虚拟主机,所以返回值跟域名无关。
Nginx用户认证
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/wwwroot/test.com;
location / //这里可以自定义我们要进行用户认证的文件或目录,比如location /admin/ 或者 location ~ admin.php(匹配)
{
auth_basic "Auth";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}
}
yum install -y httpd //需要安装httpd才能使用htpasswd这个命令,之前我们在讲apache的时候就已经安装过
htpasswd -c /usr/local/nginx/conf/htpasswd aming -t && -s reload //测试配置并重新加载
mkdir /data/wwwroot/test.com
echo “test.com”>/data/wwwroot/test.com/index.html
curl -x127.0.0.1:80 test.com -I//状态码为401说明需要验证
curl -uaming:passwd 访问状态码变为200 编辑windows的hosts文件,然后在浏览器中访问test.com会有输入用户、密码的弹窗 针对目录的用户认证
// curl -uaming:12345 -x127.0.0.1:80 test.com/admin 此时我们就是对admin的用户认证进行访问。
location /admin/
{
auth_basic "Auth";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}
Nginx域名重定向
Nginx的server_name后面支持写多个域名,而apache只能写一个,apache的域名重定向要用Alias来定义。
更改test.com.conf server
{
listen 80;
server_name test.com test1.com test2.com;
index index.html index.htm index.php;
root /data/wwwroot/test.com;
if ($host != 'test.com' )
{
rewrite ^/(.*)$ http://test.com/$1 permanent; 1ff8 // $1表示跳转到第一行,permanent表示301。
}
}
server_name后面支持写多个域名,这里要和httpd的做一个对比 permanent为永久重定向,状态码为301,如果写redirect则为302
- 前端学习笔记(四) - 盒子模型及其相关
- 学习 Unity 3D游戏开发日常笔记(C# 第四周)
- 学习笔记(08):Python数据清洗实战-数据筛选
- 学习笔记(57):零基础搞定Python数据分析与挖掘-常用的正则符号(一)
- MySql学习笔记1
- 雾山的Anrdoid学习笔记---CheckBox,RadioGroup&RadioButton .
- QML学习笔记
- 学习css笔记---——字体章节(2014年11月2日)
- 学习笔记-DOM知识点
- MFC学习笔记1——MFC概述
- 算法导论学习笔记(1)---堆排序
- android系统学习笔记二
- XQuery 学习笔记(三)回到定义(上)
- C语言学习笔记(8)
- Python:GUI之tkinter学习笔记1控件的介绍及使用
- Android之Fragment+ViewPager实现点击+滑动界面切换学习笔记
- Jquery学习笔记之如何引用jquery库
- python学习笔记(12)--爬虫下载煎蛋网图片
- Spring学习笔记之缓存数据
- 学习笔记2:Struts2 +Spring JDBC