Nginx安装、默认虚拟主机、Nginx用户认证、Nginx域名重定向

6月7日任务

12.6 Nginx安装
12.7 默认虚拟主机
12.8 Nginx用户认证
12.9 Nginx域名重定向

扩展
nginx.conf 配置详解 http://www.ha97.com/5194.htmlhttp://my.oschina.net/duxuefeng/blog/34880
nginx rewrite四种flag http://www.netingcn.com/nginx-rewrite-flag.htmlhttp://unixman.blog.51cto.com/10163040/1711943php

 

安装步骤:

Nginx安装

  • 切换到/usr/local/src/目录下
[root@yong-01 ~]# cd /usr/local/src/
[root@yong-01 src]# wget http://nginx.org/download/nginx-1.4.7.tar.gz
  • 解压安装包
[root@yong-01 src]# tar zxvf nginx-1.4.7.tar.gz
  • 切换到nginx-1.4.7目录下  cd nginx-1.4.7/
  • 初始化./configure --prefix=/usr/local/nginx
[root@yong-01 nginx-1.4.7]# ./configure --prefix=/usr/local/nginx
  • 而后make && make install
[root@yong-01 nginx-1.4.7]# make && make install
  • 查看nginx目录
[root@yong-01 nginx-1.4.7]# ls /usr/local/nginx/
conf  html  logs  sbin
  1. conf目录,配置文件目录
  2. html目录,样例文件
  3. logs目录,存放日志的
  4. sbin目录,核心进程目录
  • 支持-t 检查配置文件语法错误
[root@yong-01 nginx-1.4.7]# /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
  • 给nginx建立启动脚本,vim /etc/init.d/nginx,复制以下内容(参考https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/etc_init.d_nginx )保存退出
  • 更改配置文件的权限
[root@yong-01 nginx-1.4.7]# chmod 755 /etc/init.d/nginx
  • 将nginx加入到服务列表里
[root@yong-01 nginx-1.4.7]# chkconfig --add nginx
  • 配置开启启动nginx服务
[root@yong-01 nginx-1.4.7]# chkconfig nginx on
  • 定义配置文件,默认conf目录下是有一个nginx.conf文件的,但咱们不使用它,使用本身配置的
[root@yong-01 nginx-1.4.7]# cd /usr/local/nginx/conf/
[root@yong-01 conf]# ls
fastcgi.conf            koi-utf             nginx.conf           uwsgi_params
fastcgi.conf.default    koi-win             nginx.conf.default   uwsgi_params.default
fastcgi_params          mime.types          scgi_params          win-utf
fastcgi_params.default  mime.types.default  scgi_params.default
[root@yong-01 conf]# mv nginx.conf nginx.conf.bak
  • 建立一个配置文件vim nginx.conf ,写入以下内容(参考https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/nginx.conf
  • 做为一个网站的服务,必须监听一个端口,默认监听的是80端口,假如没有配置 server 这个几行,那么nginx将识别不到监听端口,致使服务不可用

  • 编译好配置文件,检查配置文件是否存在语法错误
[root@yong-01 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
  • 启动nginx服务
[root@yong-01 conf]# /etc/init.d/nginx start
Starting nginx (via systemctl):                            [  肯定  ]
  • 查看nginx进程
[root@yong-01 conf]# ps aux |grep nginx
root      4036  0.0  0.0  24800   784 ?        Ss   21:24   0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody    4037  0.0  0.1  27104  3364 ?        S    21:24   0:00 nginx: worker process
nobody    4038  0.0  0.1  27104  3364 ?        S    21:24   0:00 nginx: worker process
root      4043  0.0  0.0 112676   980 pts/0    R+   21:24   0:00 grep --color=auto nginx
  • 测试nginx,这里能够输入curl localhost 或者输入curl 127.0.0.1 获得的结果相同
[root@yong-01 conf]# curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
………………
  • 在本地电脑访问192.168.180.134

  • 测试解析php,新建一个1.php文件
[root@yong-01 conf]# vim /usr/local/nginx/html/1.php
<?php
echo "hello nginx.";
[root@yong-01 conf]# curl localhost/1.php
hello nginx.

默认虚拟主机目录概要

  • vim /usr/local/nginx/conf/nginx.conf //增长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
  • curl localhost
  • curl -x127.0.0.1:80 123.com

默认虚拟主机

  • 首先删除/usr/local/nginx/conf/nginx.conf 中的一部份内容——>目的是修改nginx.cnf配置,删除server后面的内容 ,从新定义虚拟主机配置所在路径

  • 而后在配置文件中增长一行,include vhost/*.conf;,保存退出

  • 新建/usr/local/nginx/conf/vhost目录
[root@yong-01 conf]# mkdir /usr/local/nginx/conf/vhost
  • 进入到/usr/local/nginx/conf/vhost目录下
[root@yong-01 conf]# cd !$
cd /usr/local/nginx/conf/vhost
  • 定义新增虚拟主机的配置,而后保存退出
[root@yong-01 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;
}
  • 建立目录 mkdir -p /data/wwwroot/default/
[root@yong-01 vhost]# mkdir -p /data/wwwroot/default/
  • 切换到/data/wwwroot/default/目录下,在目录下写入一些东西vim index.html 
[root@hanfeng vhost]# cd /data/wwwroot/default/
[root@yong-01 default]# vim index.html
This is a test default site.
  • 检测配置文件是否存在语法错误 /usr/local/nginx/sbin/nginx -t
[root@yong-01 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
  1. 再修改配置文件后,通常都 -t 去检查下,防止误操做
  2. 修改完,重启nginx或者从新加载nginx
  • 使用/etc/init.d/nginx restart 或者 /usr/local/nginx/sbin/nginx -s reload从新加载
[root@yong-01 default]# /usr/local/nginx/sbin/nginx -s reload
  • 测试访问默认页
  • 出来的就是以前/data/wwwroot/default/index.html里面定义的内容
[root@yong-01 default]# curl localhost
This is a test default site.
[root@yong-01 default]# curl localhost:80 bbb.com
This is a test default site.
  • nginx支持include这种语法

定义默认虚拟主机

由于修改了nginx.conf的配置,如今看到的默认索引页,是咱们刚刚新增的vhost的虚拟主机的索引页了 定义默认虚拟主机的两种办法: 1.默认虚拟主机,是根据目录的第一个.conf了进行选择,因此只须要在vhost目录下依次建立就能够了,固然这种方法不智能 2.只须要在vhost目录的.conf配置文件内,加上一个“default_server ”便可,把当前的这个配置对应的网站设置为第一个默认虚拟主机html

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  /
    {
        auth_basic              "Auth";
        auth_basic_user_file   /usr/local/nginx/conf/htpasswd;
    }
}
  • yum install -y httpd
  • 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会有输入用户、密码的弹窗
  • 针对目录的用户认证
location  /admin/
    {
        auth_basic              "Auth";
        auth_basic_user_file   /usr/local/nginx/conf/htpasswd;
}

Nginx用户认证

  • 首先切换到usr/local/nginx/conf/vhost/目录下
[root@yong-01 default]# cd /usr/local/nginx/conf/vhost/
  1. 新建新建一个虚拟主机test.com.conf,并编辑
[root@yong-01 vhost]# vim 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         //这个地方只要加上” /admin ” 就变成 针对这个站点的“admin” 这个目录须要用户认证
    #location  ~ admin.php       //若是把这行这样写,就会变成,匹配 “ admin.php ”这个页面的时候才须要用户认证
    {
        auth_basic              "Auth";            //定义用户认证的名字
        auth_basic_user_file   /usr/local/nginx/conf/htpasswd;         //用户名密码文件
    }
}

保存退出
  1. 在配置完成后,须要生成密码文件
  2. 在生成密码文件,须要用到Apache生成密码文件的工具“ htpasswd ”
  • 若本机已经安装过Apache,能够直接使用命令htpasswd进行生成
/usr/local/apache2/bin/htpasswd
  • 如果本机未安装Apache,可直接 yum install -y httpd 进行安装,由于yum安装的,因此工具存放在/usr/bin/下,能够直接使用htpasswd
  • htpasswd指定文件,生成用户
[root@yong-01 vhost]# /usr/local/apache2/bin/htpasswd -c /usr/local/nginx/conf/htpasswd yueyong
New password: 
Re-type new password: 
Adding password for user yueyong
  • 使用cat 命令查看/usr/local/nginx/conf/htpasswd 文件,会看到生成了一行字符串
[root@yong-01 vhost]# cat /usr/local/nginx/conf/htpasswd 
yueyong:$apr1$2z3VXNaH$ACdIVwH0mjC7f92wip8AG0
  1. 关于htpasswd -c 命令 第一次建立的时候由于没有htpasswd这个文件,须要-c建立,第二使用的时候由于已经有这个htpasswd文件了,将再也不须要-c 选项,若是还继续使用-c 这个选项,将会重置 htpasswd里的东西
  2. 再来htpasswd指定文件,生成另外一个用户
[root@yong-01 vhost]# /usr/local/apache2/bin/htpasswd /usr/local/nginx/conf/htpasswd user1
New password: 
Re-type new password: 
Adding password for user user1
[root@yong-01 vhost]# cat /usr/local/nginx/conf/htpasswd 
yueyong:$apr1$2z3VXNaH$ACdIVwH0mjC7f92wip8AG0
user1:$apr1$MRSdQqmY$ou3wQ.ZdYU70WVfvntg6u.
  • 检查配置nginx文件是否存在语法错误
[root@yong-01 vhost]# /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
  • 从新加载配置文件
  • 在从新加载的时候,若配置文件中存在错误,配置文件将不会生效;
  • 若是是直接使用restart,若是配置有错,将会直接影响到网站的运行
[root@yong-01 vhost]# /usr/local/nginx/sbin/nginx -s reload
  • 测试
[root@yong-01 vhost]# curl -x127.0.0.1:80 test.com
<html>
<head><title>401 Authorization Required</title></head>
<body bgcolor="white">
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx/1.4.7</center>
</body>
</html>
  1. 会提示错误码401,就是须要用户,因此用curl指定用户
  2. 这时指定用户和密码再来访问,会提示404,这是由于去访问index.html,可是还未建立
[root@yong-01 vhost]# curl -uyueyong:123456 -x127.0.0.1:80 test.com
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.4.7</center>
</body>
</html>
  • 建立目录,而后新建index.html,再来访问,会看到显示正常
[root@yong-01 vhost]# mkdir /data/wwwroot/test.com
[root@yong-01 vhost]# echo "test.com" > /data/wwwroot/test.com/index.html
[root@yong-01 vhost]# curl -uyueyong:123456 -x127.0.0.1:80 test.com
test.com
  • 这里的用户认证是针对整站

针对某一个目录下,才须要认证

  • 好比访问admin的时候,才须要认证
  • 首先访问admin尝试下
[root@yong-01 vhost]# curl -uyueyong:123456 -x127.0.0.1:80 test.com/admin/
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.4.7</center>
</body>
</html>
  • 而后在/usr/local/nginx/conf/vhost/test.com.conf配置文件中定义,只须要在location / 后加上admin/ 目录便可
[root@yong-01 vhost]# vim test.com.conf 
server
{
    listen 80;
    server_name test.com;
    index index.html index.htm index.php;
    root /data/wwwroot/test.com;

    location  /admin/   // 后加上admin/ 目录
    {
        auth_basic              "Auth";
        auth_basic_user_file   /usr/local/nginx/conf/htpasswd;
    }
}
  • 检查配置文件是否存在语法错误 从新加载配置文件
[root@yong-01 vhost]# /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@yong-01 vhost]# /usr/local/nginx/sbin/nginx -s reload
  • 这时候再来访问test.com,就不须要指定用户名和密码了
[root@yong-01 vhost]# curl -x127.0.0.1:80 test.com
test.com
  • 访问test.com/admin/目录
[root@yong-01 vhost]# curl -x127.0.0.1:80 test.com/admin
<html>
<head><title>401 Authorization Required</title></head>
<body bgcolor="white">
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx/1.4.7</center>
</body>
</html>
  • 这时建立一个测试页面 先新建目录 而后在admin目录下新建index.html
[root@yong-01 vhost]# mkdir /data/wwwroot/test.com/admin
[root@yong-01 vhost]# echo "test.com admin dir" > /data/wwwroot/test.com/admin/index.html
  • 这时再来访问 test.com/admin/ 会显示401,可是指定用户名和密码后就会正常显示
[root@yong-01 vhost]# curl -x127.0.0.1:80 test.com/admin/
<html>
<head><title>401 Authorization Required</title></head>
<body bgcolor="white">
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx/1.4.7</center>
</body>
</html>
[root@yong-01 vhost]# curl -uyueyong:123456 -x127.0.0.1:80 test.com/admin/
test.com admin dir

针对URL

  • 好比针对admin.php
  • 首先在配置文件/usr/local/nginx/conf/vhost/test.com.conf下定义,在 location 后加~ admin.php便可
[root@yong-01 vhost]# vim test.com.conf 

在 location  后加~ admin.php便可
server
{
    listen 80;
    server_name test.com;
    index index.html index.htm index.php;
    root /data/wwwroot/test.com;

    location  ~ admin.php
    {
        auth_basic              "Auth";
        auth_basic_user_file   /usr/local/nginx/conf/htpasswd;
    }
}
  • 检查配置文件是否存在语法错误 从新加载配置文件
[root@yong-01 vhost]# /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@yong-01 vhost]# /usr/local/nginx/sbin/nginx -s reload
  • 这时候就能够直接访问 test.com/admin/,不须要指定用户名和密码了,可是在访问admin.php的时候,则会显示401——>状态码为401说明须要验证
[root@yong-01 vhost]# curl -x127.0.0.1:80 test.com/admin/
test.com admin dir
[root@yong-01 vhost]# echo "test admin.php" > /data/wwwroot/test.com/admin.php
[root@yong-01 vhost]# 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.4.7</center>
</body>
</html>
加上用户名和密码就能够正常访问了
[root@yong-01 vhost]# curl -uyueyong:123456 -x127.0.0.1:80 test.com/admin.php
test admin.php

Nginx域名重定向目录概要

  • 更改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;
    }
}
  • server_name后面支持写多个域名,这里要和httpd的作一个对比
  • permanent为永久重定向,状态码为301,若是写redirect则为302

Nginx域名重定向

  • 在Nginx里“server_name” 支持跟多个域名;可是Apache“server_name”只能跟一个域名,须要跟多个域名,须要使用Alisa;
  • 在Nginx的conf配置文件里“server_name ” 设置了多个域名,就会使网站的权重变了,到底须要哪一个域名为主站点,因此须要域名重定向
  • 修改配置文件vim /usr/local/nginx/conf/vhost/test.com.conf,(这里删除用户认证那一块代码)
[root@yong-01 vhost]# vim 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;
    }
}
  • if ($host != ‘test.com’ ) //假如域名,“!=”不等于 test.com,将执行下面的脚本
  • rewrite ^/(.)$ http://test.com/$1 permanent; // ^/(.)$ 正式写法 http://$host/(.*)$ 这段能够直接省略掉的,同时还能够加上一些规则,
  • permanent 就是301的意思
  • 若是想弄成302,只须要更改成 redirect

检查配置文件语法错误,并从新加载配置文件linux

[root@yong-01 vhost]# /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@yong-01 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@yong-01 vhost]# curl -x127.0.0.1:80 test2.com/index.html -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.4.7
Date: Thu, 07 Jun 2018 14:46:00 GMT
Content-Type: text/html
Content-Length: 184
Connection: keep-alive
Location: http://test.com/index.html

[root@yong-01 vhost]# curl -x127.0.0.1:80 test2.com/admin/index.html -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.4.7
Date: Thu, 07 Jun 2018 14:46:21 GMT
Content-Type: text/html
Content-Length: 184
Connection: keep-alive
Location: http://test.com/admin/index.html
  • 它会访问默认虚拟主机
  • 这时如果随意访问一个不存在的网址,则会显示404
[root@yong-01 vhost]# curl -x127.0.0.1:80 test5.com/admin/index.html -I
HTTP/1.1 404 Not Found
Server: nginx/1.4.7
Date: Thu, 07 Jun 2018 14:47:19 GMT
Content-Type: text/html
Content-Length: 168
Connection: keep-alive
相关文章
相关标签/搜索