nginx配置虚拟主机和认证模块的配置

nginx虚拟主机配置

所谓虚拟主机,在Web服务里就是一个独立的网站站点,这个站点对应独立的域名(也可能是ip或端口)具有独立的程序及资源目录,可以独立地对外提供服务供用户访问。

这个独立的站点在配置里是由一定格式的标签段标记的,Nginx软件则使用一个server{}标签来标示一个虚拟主机。一个Web服务里可以有多个虚拟主机标签对,即可以同时支持多个虚拟主机站点

配置虚拟主机的步骤

修改配置文件

nginx配置多个虚拟主机也就是在配置文件里添加多个server标签,在server标签里指定不同的域名或IP地址以及监听不同的端口。(下面的示例是基于域名的虚拟主机)

[root@anuo conf]# vim /usr/local/nginx/conf/nginx.conf
……
 10     server {
 11         listen       80;    (--基于端口的虚拟主机就修改监听的不同端口)
 12         server_name  www.anuo1.com;     --第一的虚拟主机的域名
 13         location / {
 14             root   html/www.anuo1.com;      --指定网站存放的目录
 15             index  index.html index.htm;
 16         }
 17         error_page   500 502 503 504  /50x.html;
 18         location = /50x.html {
 19             root   html;
 20        }
 21    }
 22     server {
 23         listen       80;        (--基于IP的虚拟主机也就是在这里加上虚拟主机的IP地址)
 24         server_name  www.anuo2.com;         --第二个虚拟主机的域名
 25         location / {
 26             root   html/www.anuo2.com;      --指定网站存放的目录
 27             index  index.html index.htm;
 28         }
 29         error_page   500 502 503 504  /50x.html;
 30         location = /50x.html {
 31             root   html;
 32       }
 33     
 34     }

检查配置修改的语法是否正确 (这一步最好要养成个习惯,提前检测错误总是比后面焦头烂额的去排错要好)

[root@anuo conf]# nginx -t
nginx: the configuration file /usr/local/nginx-1.10.2/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx-1.10.2/conf/nginx.conf test is successful

重启服务

[root@anuo conf]# nginx -s reload   --这里重新加载即可

注意:配置和IP地址相关的都要(-s stop)重启才生效
[root@anuo conf]# nginx -s stop

创建站点目录和主页文件

[root@anuo conf]# mkdir /usr/local/nginx/html/www.anuo{1..2}.com -p
创建主页文件
[root@anuo conf]# echo "hello anuo2" > /usr/local/nginx/html/www.anuo2.com/index.html
[root@anuo conf]# echo "hello anuo1" > /usr/local/nginx/html/www.anuo1.com/index.html
检查主页内容信息
[root@anuo conf]# cat /usr/local/nginx/html/www.anuo1.com/index.html 
hello anuo1
[root@anuo conf]# cat /usr/local/nginx/html/www.anuo2.com/index.html 
hello anuo2

修改主机hosts文件

[root@anuo conf]# vim /etc/hosts
10.0.0.10       www.anuo1.com www.anuo2.com

测试

[root@anuo conf]# curl www.anuo1.com
hello anuo1
[root@anuo conf]# curl www.anuo2.com
hello anuo2

配置多个虚拟主机的文件规范

虚拟主机多了配置文件规范方便于管理

创建虚拟主机配置文件存储目录

[root@anuo conf]# pwd
/usr/local/nginx/conf
[root@anuo conf]# mkdir xnzj    --在配置文件下创建存放虚拟主机的目录(目录名可随便定义)

给单个虚拟主机创建配置文件

[root@anuo conf]# sed -n '10,21p' nginx.conf > xnzj/anuo1.conf
[root@anuo conf]# sed -n '22,34p' nginx.conf > xnzj/anuo2.conf
     --就是把配置文件里的每个server标签做出单个独立的文件

修改nginx配置文件使之加载识别虚拟主机配置文件

[root@anuo conf]# vim nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    include xnzj/*;     --指定虚拟主机配置文件的目录( *表示该目录下的所有)    
   }

检测配置语法、重启服务、检查监听端口

[root@anuo conf]# nginx -t
[root@anuo conf]# nginx -s stop
[root@anuo conf]# nginx 
[root@anuo conf]# lsof -i:80
COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   8978  root    6u  IPv4  21170      0t0  TCP *:http (LISTEN)
nginx   8979 nginx    6u  IPv4  21170      0t0  TCP *:http (LISTEN)

查看配置文件的加载顺序

[root@web01 logs]# /application/nginx/sbin/nginx -T

参数说明:
-T   : test configuration, dump it and exit
   测试配置文件,并且加载一遍,并显示加载的顺序

需要指定加载顺序时候就改成下面的配置

[root@anuo conf]# vim nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    include xnzj/anuo1.conf;     --通过IP访问默认是加载第一个
    include xnzj/anuo2.conf;
   }

重启、测试结果

[root@anuo conf]# nginx -t
[root@anuo conf]# nginx -s reload

[root@anuo conf]# curl www.anuo1.com
hello anuo1
[root@anuo conf]# curl www.anuo2.com
hello anuo2
[root@anuo conf]# curl 10.0.0.10
hello anuo1

当报403错误时,是因为没有首页文件信息


nginx服务搭建文件共享服务器

修改配置文件

[root@anuo html]# vim ../conf/xnzj/anuo1.conf 
    server {
        listen       80;
        server_name  www.anuo1.com;
        location / {
            root   html/www.anuo1.com;
            autoindex on;   --配置autoindex on参数会以目录的形式显示站点下的文件信息(站点下的首页文件要删除)
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
       }
   }

nginx 的访问认证

修改nginx的相关配置文件

[root@anuo html]# vim ../conf/xnzj/anuo1.conf 
    server {
        listen       80;
        server_name  www.anuo1.com;
        location / {
            root   html/www.anuo1.com;
                autoindex on;
                auth_basic      "haha";     --指定用户
                auth_basic_user_file  /usr/local/nginx/conf/htpasswd;--指定用户和密码的存放文件目录
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
       }
   }

重启系统访问测试

[root@anuo html]# curl 10.0.0.10
<html>
<head><title>401 Authorization Required</title></head>      --报401错误
<body bgcolor="white">
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx/1.10.2</center>
</body>
</html>

(401错误说明: 需要认证,但是没有认证)

下面给用户创建密码文件

[root@anuo html]# yum install httpd-tools -y    --下载htpasswd命令

[root@anuo html]# touch /usr/local/nginx/conf/htpasswd
[root@anuo html]# htpasswd -c /usr/local/nginx/conf/htpasswd haha   ## -c 创建一个新的密码文
New password: 
Re-type new password: 
Adding password for user haha

更改密码文件权限

[root@anuo html]# chmod 400 /usr/local/nginx/conf/htpasswd  --修改密码文件的权限
[root@anuo html]# chown nginx:nginx /usr/local/nginx/conf/htpasswd -R   --修改密码文件的属主、组为nginx
[root@anuo html]# cat /usr/local/nginx/conf/htpasswd    
haha:7S5ayh6C4PmLk

访问测试

交互式输入密码 
[root@anuo html]# curl 10.0.0.10 -uhaha     -- -u指定用户
Enter host password for user 'haha':        --输入用户的密码

[root@anuo html]# curl 10.0.0.10 -uhaha:123456      --免交互输入密码 (注意用户和密码的格式)

rewrite 模块

rewrite模块两个功能

1.实现网站地址信息跳转
2.实现伪静态

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章