Linux centosVMware Nginx安装、 默认虚拟主机、Nginx用户认证、Nginx域名重定向
一、 Nginx安装
cd /usr/local/src
wget http://nginx.org/download/nginx-1.12.1.tar.gz
版本在http://nginx.org/下载
tar zxvf nginx-1.12.1.tar.gz
进入配置文件
cd nginx-1.12.1
./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/
ls
配置文件conf,不用系统配置文件,使用自己的配置文件
mv nginx.conf nginx.conf.1 拷贝系统配置文件,并移动到自己的文件下边
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 启动nginx
/etc/init.d/nginx start netstat -lntp |grep 80
测试是否成功
nginx欢迎页面
在index.html文件下面定义过
vim nginx.conf里边有定义过,即默认虚拟主机页面
访问任意网页都会跳转到主机页面
支持nginx解析地方
自己编写一个网页测试
[root@davery conf]# vim /usr/local/nginx/html/1.php
测试
二、 默认虚拟主机
vim /usr/local/nginx/conf/nginx.conf //增加
include vhost/*.conf; 一定要有;
编辑host相关配置
mkdir /usr/local/nginx/conf/vhost
vim aaa.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/
把以前的server注释掉,并添加如上
编辑wwwroot其他域名网页
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
重启
或者重新加载
或者
[root@davery default]# /usr/local/nginx/sbin/nginx -s reload
检测一下
curl localhost
curl -x127.0.0.1:80 aaa.conf
随便访问都默认是者个域名,都指向这个虚拟主机
[root@davery default]# cd /usr/local/nginx/conf/
理解: 需要两个地方都要创建才能访问成功
vim /usr/local/nginx/conf/vhost/aaa.conf
server
{
listen 80 default_server; // 有这个标记的就是默认虚拟主机
server_name aaa.com;
index index.html index.htm index.php;
root /data/wwwroot/default;
}
vim /data/wwwroot/default/index.html
This is a default site.
curl localhost
curl -x127.0.0.1:80 aaa.conf
curl -x127.0.0.1:80 111.com
会先去访问aaa.conf里边的内容,然后脚本再指向/data/wwroot/default,再访问里边的index.html,即This is a default site.
这里虚拟主机就是default_server
三、Nginx用户认证
cd /usr/local/nginx/conf/vhost //写入如下内容
[root@davery vhost]# vim aaa.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; 用户名密码文件
}
}
yum install -y httpd
htpasswd -c /usr/local/nginx/conf/htpasswd davery 设置密码为davery
cat一下就生成文件了
生成第二个
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload //测试配置并重新加载
测试提示401就说明需要用户认证
aaa.com.conf就可以,test.com就不能访问
[root@davery vhost]# mkdir /data/wwwroot/test.com
[root@davery vhost]# echo "test.com" > /data/wwwroot/test.com/index.html
[root@davery vhost]# curl -udavery:mimA123 -x127.0.0.1:80 test.com
访问失败为没有创建test.com主目录,创建后,使用认证密码登录就可以访问到了
定义访问admin时才需要认证,在配置文件中添加用户admin就可以
vim /usr/local/nginx/conf/vhost/aaa.com.conf
这个设置只针对目录
访问test.com就不需要用户密码认证了
在admin下边创建一个index.html文件
写入测试脚本,再次使用密码就可以访问到了
[root@davery vhost]# mkdir /data/wwwroot/test.com/admin
[root@davery vhost]# echo "test.com admin dir" > /data/wwwroot/test.com/admin/index.html
设置用户认证针对文件
vim /usr/local/nginx/conf/vhost/aaa.com.conf
设置为匹配admin.php
/usr/local/nginx/sbin/nginx -t 测试语法
/usr/local/nginx/sbin/nginx -s reload重加载
测试:这时候访问目录就不需要用户认证了,访问admin.php文件才需要用户认证
具体内容访问:centos7.aminglinux.com
四、Nginx域名重定向
更改配置文件
vim /usr/local/nginx/conf/vhost/aaa.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; 即全写rewrite http://$host/(.*)$ http://test.com/%1 permanent
}
}
server_name后面支持写多个域名,这里要和httpd的做一个对比
permanent为永久重定向,状态码为301,如果写redirect则为302
vim /usr/local/nginx/conf/vhost/aaa.com.conf
测试
/usr/local/nginx/sbin/nginx -t 测试语法
/usr/local/nginx/sbin/nginx -s reload重加载
最新文章
- Objective-C中的集合类
- bootstrap源码分析之tab(选项卡)
- 前端之css、JavaScript和DOM
- Dynamics AX 2012 R2 为运行失败的批处理任务设置预警
- 在unity3d中使用opencv
- js返回上一页报网页过期问题解决
- .NET高端职位招聘要求
- Windows命令行语法说明
- C++, const:
- PHP - 拒绝低版本PHP
- ie 如何判断正在执行的脚本
- ImCash:币安下架BSV之辩:规则、中立与去中心化
- 编写程序,将来自文件中的行保存在一个vector<;string>;,然后使用一个istringstream 从vector中读取数据,每次读一个单词
- 【BZOJ4832】抵制克苏恩(矩阵快速幂,动态规划)
- leetcode-algorithms-36 Valid Sudoku
- 如何解决cacti的snmp error
- error LNK2038: 检测到“_MSC_VER”的不匹配项: 值“1600”不匹配值“1800”
- 008-jdk1.7版本新特性
- (1)RGB-D点云生成
- http://code52.org/DownmarkerWPF/