12.6 Nginx安装 12.7 默认虚拟主机 12.8 Nginx用户认证 12.9 Nginx域名重定向
Posted sisul
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了12.6 Nginx安装 12.7 默认虚拟主机 12.8 Nginx用户认证 12.9 Nginx域名重定向相关的知识,希望对你有一定的参考价值。
[root@lizhipenglinux01 php-5.6.30]# cd /usr/local/src
[root@lizhipenglinux01 src]# wget http://nginx.org/download/nginx-1.12.1.tar.gz
[root@lizhipenglinux01 src]# tar zxf nginx-1.12.1.tar.gz
[root@lizhipenglinux01 src]# cd nginx-1.12.1/
[root@lizhipenglinux01 nginx-1.12.1]# ./configure --prefix=/usr/local/nginx
[root@lizhipenglinux01 nginx-1.12.1]# make
[root@lizhipenglinux01 nginx-1.12.1]# make install
[root@lizhipenglinux01 nginx-1.12.1]# ls /usr/local/nginx/ conf配置文件
conf html logs sbin
[root@lizhipenglinux01 nginx-1.12.1]# vim /etc/init.d/nginx
[root@lizhipenglinux01 nginx-1.12.1]# chmod 755 /etc/init.d/nginx
[root@lizhipenglinux01 nginx-1.12.1]# chkconfig --add nginx
[root@lizhipenglinux01 nginx-1.12.1]# chkconfig nginx on
配置的模板已经存在,不用他,故改名备份一下
[root@lizhipenglinux01 conf]# vim nginx.conf
[root@lizhipenglinux01 conf]# /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
s代表父进程,root是父进程,nobody是子进程
默认虚拟主机
[root@lizhipenglinux01 conf]# curl localhost/1.php
This is nginx test page.[root@lizhipenglinux01 conf]#
先来解释“虚拟主机”。在介绍httpd的时候,已经解释过默认虚拟主机的概念,在Nginx中也有默认虚拟主机,跟httpd类似,第一个被Nginx加载的虚拟主机就是默认主机。但和httpd不同的地方是,它还有一个配置用来标记默认虚拟主机。也就是说,如果没有这个标记,第一个虚拟主机默认虚拟主机。删除nginx.conf如下部分。
添加如下部分
意思是,/usr/local/nginx/conf/vhost/下面的所有以.conf结尾的文件都会加载,这样我们就可以把所有虚拟主机配置文件放到vhost目录下面
[root@lizhipenglinux01 conf]# pwd
/usr/local/nginx/conf
[root@lizhipenglinux01 conf]# mkdir vhost
[root@lizhipenglinux01 conf]# cd vhost/
[root@lizhipenglinux01 vhost]# ls
[root@lizhipenglinux01 vhost]# vim aaa.com.conf
server
{
listen 80 default_server; // 有这个标记的就是默认虚拟主机
server_name aaa.com;
index index.html index.htm index.php; 指定索引页
root /data/wwwroot/default;
}
[root@lizhipenglinux01 vhost]# mkdir /data/wwwroot/default/
[root@lizhipenglinux01 vhost]# cd /data/wwwroot/default/
[root@lizhipenglinux01 default]# ls
[root@lizhipenglinux01 default]# vim index.html
This is the default site.
[root@lizhipenglinux01 default]# /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@lizhipenglinux01 default]# /usr/local/nginx/sbin/nginx -s reload 重新加载
[root@lizhipenglinux01 default]# curl localhost
This is the default site.
[root@lizhipenglinux01 default]# curl -x127.0.0.1:80 bbb.com
This is the default site.
[root@lizhipenglinux01 default]# curl -x127.0.0.1:80 aaa.com
This is the default site.
[root@lizhipenglinux01 default]# 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 /
{
auth_basic "Auth"; 定义用户认证的名字
auth_basic_user_file /usr/local/nginx/conf/htpasswd; 用户名密码文件
}
}
[root@lizhipenglinux01 default]# vim /usr/local/nginx/conf/vhost/test.com.conf
[root@lizhipenglinux01 default]# /usr/local/apache2.4/bin/htpasswd -c /usr/local/nginx/conf/htpasswd aming
New password:
Re-type new password:
Adding password for user aming
[root@lizhipenglinux01 default]# /usr/local/apache2.4/bin/htpasswd /usr/local/nginx/conf/htpasswd user1 第二个的时候不要-c,-c表示生成,会重置之前的
New password:
Re-type new password:
Adding password for user user1
[root@lizhipenglinux01 default]# cat /usr/local/nginx/conf/htpasswd
aming:$apr1$tmIuz3nE$1j60Dm8yXQkDEJtWnF5s31
user1:$apr1$erglUoeF$vY7Rnm9/A12JjRoc0fM4d.
[root@lizhipenglinux01 default]# /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@lizhipenglinux01 default]# /usr/local/nginx/sbin/nginx -s reload
这个是针对目录的
[root@lizhipenglinux01 default]# /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@lizhipenglinux01 default]# /usr/local/nginx/sbin/nginx -s reload
这个时候连接test.com不需要密码,但是/admin目录下 需要
[root@lizhipenglinux01 default]# curl -uaming:lizhipeng -x127.0.0.1:80 test.com/admin/
test.com admin dir
并不需要-u
[root@lizhipenglinux01 default]# curl -x127.0.0.1:80 test.com/admin.php 这个就需要了
<html>
<head><title>401 Authorization Required</title></head>
<body bgcolor="white">
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx/1.12.1</center>
</body>
</html>
test4.com会去访问默认的虚拟主机
以上是关于12.6 Nginx安装 12.7 默认虚拟主机 12.8 Nginx用户认证 12.9 Nginx域名重定向的主要内容,如果未能解决你的问题,请参考以下文章
12.6 Nginx安装 12.7 默认虚拟主机 12.8 Nginx用户认证 12.9 Nginx
12.6 Nginx安装12.7 默认虚拟主机12.8Nginx用户认证12.9Nginx域名重定向
12.6 Nginx安装;12.7 Nginx默认虚拟主机;12.8 Nginx用户认证;12.9