当前位置: 首页 > news >正文

十二周二次课

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

十二周二次课

12.6Nginx安装

12.7默认虚拟主机

12.8Nginx用户认证

12.9Nginx域名重定向

12.6Nginx安装

Nginx安装

•cd /usr/local/src

• wget http://nginx.org/download/nginx-1.12.1.tar.gz

• 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解析

• vi /usr/local/nginx/html/1.php //加入如下内容

• <?php

•    echo "test php scripts.";

•?>

• curl localhost/1.php

Nginx安装

1.切换到/usr/local/src/目录下

[root@tianqi-01 ~]# cd /usr/local/src/
[root@tianqi-01 src]#

2.下载Nginx安装包

  • wget http://nginx.org/download/nginx-1.12.1.tar.gz

[root@tianqi-01 src]# wget http://nginx.org/download/nginx-1.12.1.tar.gz

3.解压安装包

[root@tianqi-01 src]# tar zxvf nginx-1.12.1.tar.gz 

4.切换到nginx-1.12.1目录下

[root@tianqi-01 src]# cd nginx-1.12.1
[root@tianqi-01 nginx-1.12.1]# 

5.初始化./configure --prefix=/usr/local/nginx,并检查是否成功

[root@tianqi-01 nginx-1.12.1]# ./configure --prefix=/usr/local/nginx

[root@tianqi-01 nginx-1.12.1]# echo $?
0
[root@tianqi-01 nginx-1.12.1]#

6.编译make && make install

[root@tianqi-01 nginx-1.12.1]# make && make install

[root@tianqi-01 nginx-1.12.1]# echo $?
0
[root@tianqi-01 nginx-1.12.1]#

7.查看nginx目录

  • conf目录,就是配置文件目录
  • html目录,样例文件
  • logs目录,存放日志的目录
  • sbin目录,核心进程目录

[root@tianqi-01 nginx-1.12.1]# ls /usr/local/nginx/
conf  html  logs  sbin
[root@tianqi-01 nginx-1.12.1]# 

8.支持-t 检查配置文件语法错误

[root@tianqi-01 nginx-1.12.1]# /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@tianqi-01 nginx-1.12.1]# 

9.给nginx创建启动脚本,放在/etc/init.d/nginx,配置文件内容如下

将一些内容拷贝进去

#!/bin/bash
# chkconfig: - 30 21
# description: http service.
# Source Function Library
. /etc/init.d/functions
# Nginx Settings
NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
RETVAL=0
prog="Nginx"
start()
{
    echo -n $"Starting $prog: "
    mkdir -p /dev/shm/nginx_temp
    daemon $NGINX_SBIN -c $NGINX_CONF
    RETVAL=$?
    echo
    return $RETVAL
}
stop()
{
    echo -n $"Stopping $prog: "
    killproc -p $NGINX_PID $NGINX_SBIN -TERM
    rm -rf /dev/shm/nginx_temp
    RETVAL=$?
    echo
    return $RETVAL
}
reload()
{
    echo -n $"Reloading $prog: "
    killproc -p $NGINX_PID $NGINX_SBIN -HUP
    RETVAL=$?
    echo
    return $RETVAL
}
restart()
{
    stop
    start
}
configtest()
{
    $NGINX_SBIN -c $NGINX_CONF -t
    return 0
}
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  reload)
        reload
        ;;
  restart)
        restart
        ;;
  configtest)
        configtest
        ;;
  *)
        echo $"Usage: $0 {start|stop|reload|restart|configtest}"
        RETVAL=1
esac
exit $RETVAL

然后保存退出

10.更改配置文件的权限

[root@tianqi-01 nginx-1.12.1]# chmod 755 /etc/init.d/nginx
[root@tianqi-01 nginx-1.12.1]# 

11.将nginx加入到服务列表里

[root@tianqi-01 nginx-1.12.1]# chkconfig --add nginx

[root@tianqi-01 nginx-1.12.1]# 

12.设置开机启动nginx服务
[root@tianqi-01 nginx-1.12.1]# chkconfig nginx on

[root@tianqi-01 nginx-1.12.1]# 

13.定义配置文件,默认conf目录下有一个nginx.conf文件,但我们不使用它,我们使用自己配置的

[root@tianqi-01 nginx-1.12.1]# cd /usr/local/nginx/conf/
[root@tianqi-01 conf]# ls

fastcgi.conf          fastcgi_params.default  mime.types          nginx.conf.default   uwsgi_params
fastcgi.conf.default  koi-utf                 mime.types.default  scgi_params          uwsgi_params.default
fastcgi_params        koi-win                 nginx.conf          scgi_params.default  win-utf
[root@tianqi-01 conf]# mv nginx.conf nginx.conf.1
[root@tianqi-01 conf]# 

14.创建一个配置文件,内容如下

[root@tianqi-01 conf]# vim nginx.conf

在配置文件中添加以下内容

代码地址为https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/nginx.conf

user nobody nobody;    // user定义启动nginx的用户
worker_processes 2;    //定义子进程有几个
error_log /usr/local/nginx/logs/nginx_error.log crit;   //错误日志
pid /usr/local/nginx/logs/nginx.pid;  // PID所在
worker_rlimit_nofile 51200;   //nginx最多可以打开文件的上限
events
{
use epoll;                                   //使用epoll模式
worker_connections 6000;      // 进程最多有多少个连接
}

http
{   
    include mime.types;
    default_type application/octet-stream;
    server_names_hash_bucket_size 3526;
    server_names_hash_max_size 4096;
    log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
    ' $host "$request_uri" $status'
    ' "$http_referer" "$http_user_agent"';
    sendfile on;
    tcp_nopush on;
    client_header_timeout 3m;
    client_body_timeout 3m;
    send_timeout 3m;
    connection_pool_size 256;
    client_header_buffer_size 1k;
    large_client_header_buffers 8 4k;
    request_pool_size 4k;
    output_buffers 4 32k;
    postpone_output 1460;
    client_max_body_size 10m;
    client_body_buffer_size 256k;
    client_body_temp_path /usr/local/nginx/client_body_temp;
    proxy_temp_path /usr/local/nginx/proxy_temp;
    fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
    fastcgi_intercept_errors on;
    tcp_nodelay on;
    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 8k;
    gzip_comp_level 5;
    gzip_http_version 1.1;
    gzip_types text/plain application/x-javascript text/css text/htm
    application/xml;
    server        //一个server 对应一个虚拟主机,定义这个,才能正常访问网站,下面一整段,就是一个默认的虚拟主机
    {
        listen 80;
        server_name localhost;            //网站域名
        index index.html index.htm index.php; 
        root /usr/local/nginx/html;            //网站的根目录
        location ~ \.php$                         //配置解析php的部分
        {
            include fastcgi_params;
            fastcgi_pass unix:/tmp/php-fcgi.sock;        //nginx通过这一行配置来调用php-fpm服务
           # fastcgi_pass 127.0.0.1:9000;            //如果php-fpm监听的是9000端口,直接这样写配置即可
            fastcgi_index index.php; 
            fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
        }    
    }   

然后保存退出

  • 作为一个网站的服务,必须监听一个端口,默认监听的是80端口,假如没有配置 server 这个几行,那么nginx将识别不到监听端口,导致服务不可用

15.编译好配置文件,检查配置文件是否存在语法错误

[root@tianqi-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
[root@tianqi-01 conf]# 

16.启动nginx服务

[root@tianqi-01 conf]# /etc/init.d/nginx start
Starting nginx (via systemctl):                            [  OK  ]
[root@tianqi-01 conf]# 

17.查看nginx进程

[root@tianqi-01 conf]# ps aux |grep nginx
root       3701  0.0  0.0  20496   624 ?        Ss   17:32   0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/confnginx.conf
nobody     3702  0.0  0.3  22940  3208 ?        S    17:32   0:00 nginx: worker process
nobody     3703  0.0  0.3  22940  3208 ?        S    17:32   0:00 nginx: worker process
root       3705  0.0  0.0 112660   984 pts/0    R+   17:32   0:00 grep --color=auto nginx

[root@tianqi-01 src]# ps aux |grep php-fpm
root       1009  0.0  0.4 227204  4952 ?        Ss   15:57   0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)
php-fpm    1010  0.0  0.4 227204  4712 ?        S    15:57   0:00 php-fpm: pool www
php-fpm    1011  0.0  0.4 227204  4712 ?        S    15:57   0:00 php-fpm: pool www
php-fpm    1012  0.0  0.4 227204  4712 ?        S    15:57   0:00 php-fpm: pool www
php-fpm    1013  0.0  0.4 227204  4712 ?        S    15:57   0:00 php-fpm: pool www
php-fpm    1014  0.0  0.4 227204  4716 ?        S    15:57   0:00 php-fpm: pool www
php-fpm    1015  0.0  0.5 227204  5724 ?        S    15:57   0:00 php-fpm: pool www
php-fpm    1016  0.0  0.4 227204  4716 ?        S    15:57   0:00 php-fpm: pool www
php-fpm    1017  0.0  0.4 227204  4716 ?        S    15:57   0:00 php-fpm: pool www
php-fpm    1018  0.0  0.4 227204  4716 ?        S    15:57   0:00 php-fpm: pool www
php-fpm    1019  0.0  0.4 227204  4716 ?        S    15:57   0:00 php-fpm: pool www
php-fpm    1020  0.0  0.4 227204  4716 ?        S    15:57   0:00 php-fpm: pool www
php-fpm    1021  0.0  0.4 227204  4716 ?        S    15:57   0:00 php-fpm: pool www
php-fpm    1022  0.0  0.4 227204  4716 ?        S    15:57   0:00 php-fpm: pool www
php-fpm    1023  0.0  0.4 227204  4720 ?        S    15:57   0:00 php-fpm: pool www
php-fpm    1024  0.0  0.4 227204  4720 ?        S    15:57   0:00 php-fpm: pool www
php-fpm    1025  0.0  0.4 227204  4720 ?        S    15:57   0:00 php-fpm: pool www
php-fpm    1026  0.0  0.4 227204  4720 ?        S    15:57   0:00 php-fpm: pool www
php-fpm    1027  0.0  0.4 227204  4720 ?        S    15:57   0:00 php-fpm: pool www
php-fpm    1028  0.0  0.4 227204  4720 ?        S    15:57   0:00 php-fpm: pool www
php-fpm    1029  0.0  0.4 227204  4720 ?        S    15:57   0:00 php-fpm: pool www
root       3791  0.0  0.0 112660   984 pts/0    R+   19:12   0:00 grep --color=auto php-fpm
[root@tianqi-01 src]# 

18.测试nginx,这里可以输入curl localhost 或者输入curl 127.0.0.1 得到的结果相同

[root@tianqi-01 conf]# curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>    //nginx欢迎页面
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

[root@tianqi-01 conf]# ls /usr/local/nginx/html/
50x.html  index.html

[root@tianqi-01 conf]# curl 127.0.0.1
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@tianqi-01 src]# 

19.测试解析php,新建一个1.php文件

[root@tianqi-01 conf]# vim /usr/local/nginx/html/1.php

<?php
echo "This is nginx test page.";
?>

保存退出

[root@tianqi-01 conf]# curl localhost/1.php
This is nginx test page.[root@tianqi-01 conf]#     //说明解析成功

12.7默认虚拟主机

•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

默认虚拟主机

1.首先删除/usr/local/nginx/conf/nginx.conf 中的一部分内容——>目的是修改nginx.cnf配置,删除默认的虚拟主机配置,重新定义虚拟主机配置所在路径

[root@tianqi-01 conf]# vim /usr/local/nginx/conf/nginx.conf

删除的内容

server
    {
        listen 80;
        server_name localhost;
        index index.html index.htm index.php;
        root /usr/local/nginx/html;
        location ~ \.php$
        {
            include fastcgi_params;
            fastcgi_pass unix:/tmp/php-fcgi.sock;
           # fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
        }
    }
2.然后在配置文件中增加一行,include vhost/*.conf   

    application/xml;
    include vhost/*.conf;

然后保存退出

3.新建/usr/local/nginx/conf/vhost目录

[root@tianqi-01 conf]# pwd
/usr/local/nginx/conf
[root@tianqi-01 conf]# mkdir vhost
[root@tianqi-01 conf]# 

4.进入到/usr/local/nginx/conf/vhost目录下

[root@tianqi-01 conf]# cd vhost
[root@tianqi-01 vhost]# 

5.定义新增虚拟主机的配置

[root@tianqi-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;
}

然后保存退出

6.创建目录

[root@tianqi-01 vhost]# mkdir /data/wwwroot/default/
[root@tianqi-01 vhost]# 

7.切换到/data/wwwroot/default/目录下,在目录下写入一些东西

[root@tianqi-01 vhost]# cd /data/wwwroot/default/
[root@tianqi-01 default]# 

8.新建index.html,写入一些东西

[root@tianqi-01 default]# vim index.html

写入以下内容

This is the default site.

保存退出

9.检测配置文件是否存在语法错误

[root@tianqi-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
[root@tianqi-01 default]# 

下面故意写错,查看错误提示

[root@tianqi-01 default]# vim /usr/local/nginx/conf/vhost/aaa.com.conf 

server
{
    listen 80 default_server;
    server_name aaa.com;
    index index.html index.htm index.php;
    root /data/wwwroot/default;
    afagstyasf        //写入乱码
}

保存退出

检查语法错误

[root@tianqi-01 default]# /usr/local/nginx/sbin/nginx -t
nginx: [emerg] unexpected "}" in /usr/local/nginx/conf/vhost/aaa.com.conf:8
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed
[root@tianqi-01 default]# 

再改回来

[root@tianqi-01 default]# vim /usr/local/nginx/conf/vhost/aaa.com.conf 
[root@tianqi-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
[root@tianqi-01 default]# 

11.在修改配置文件后,一般都 -t 去检查下,防止误操作

12.修改完,重启nginx或者重新加载nginx

  • 使用/etc/init.d/nginx restart 或者 /usr/local/nginx/sbin/nginx -s reload重新加载

[root@tianqi-01 default]# /usr/local/nginx/sbin/nginx -s reload
[root@tianqi-01 default]# 

13.测试访问默认页

  • 出来的就是之前/data/wwwroot/default/index.html里面定义的内容

[root@tianqi-01 default]# curl localhost
This is the default site.
[root@tianqi-01 default]# curl -x127.0.0.1:80 bbb.com
This is the default site.

[root@tianqi-01 default]# curl -x127.0.0.1:80 aaa.com
This is the default site.
[root@tianqi-01 default]# 

//默认是aaa.com,不管访问什么域名,默认虚拟主机就是这样,不管什么域名,只要解析过来,只想到我们的服务器,它都能访问到这个站点。

14.nginx支持include这种语法

定义默认虚拟主机

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

12.8Nginx用户认证

•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用户认证

1.首先切换到usr/local/nginx/conf/vhost/目录下

[root@tianqi-01 default]# cd /usr/local/nginx/conf/vhost/
[root@tianqi-01 vhost]# 

2.新建新建一个虚拟主机test.com.conf,并编辑

[root@tianqi-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_basic_user_file   /usr/local/nginx/conf/htpasswd;    //用户名密码文件

}

}

保存退出

3.在配置完成后,需要生成密码文件

4.在生成密码文件,需要用到Apache生成密码文件的工具“ htpasswd ”

  • 若本机已经安装过Apache,可以直接使用命令htpasswd进行生成

/usr/local/apache2.4/bin/htpasswd

  • 若是本机未安装Apache,可直接 yum install -y httpd 进行安装,因为yum安装的,所以工具存放在/usr/bin/下,可以直接使用htpasswd

yum install -y httpd

5.这里由于未安装过Apache,所以先yum安装

[root@tianqi-01 vhost]# yum install -y httpd

6.htpasswd指定文件,生成用户

[root@tianqi-01 vhost]# htpasswd -c /usr/local/nginx/conf/htpasswd tianqi
New password:         //密码123456
Re-type new password: 
Adding password for user tianqi
[root@tianqi-01 vhost]# 

7.使用cat 命令查看/usr/local/nginx/conf/htpasswd 文件,会看到生成了一行字符串

[root@tianqi-01 vhost]# cat /usr/local/nginx/conf/htpasswd
tianqi:$apr1$MO1ysyDK$r0Eq.JoAuWRSVzXBYY1Xq0
[root@tianqi-01 vhost]# 

8.关于htpasswd -c 命令 第一次创建的时候因为没有htpasswd这个文件,需要-c创建,第二使用的时候因为已经有这个htpasswd文件了,将不再需要-c 选项,如果还继续使用-c 这个选项,将会重置 htpasswd里的东西

9.再来htpasswd指定文件,生成另一个用户

[root@tianqi-01 vhost]# /usr/local/apache2.4/bin/htpasswd /usr/local/nginx/conf/htpasswd user1
New password:     //密码为123456
Re-type new password: 
Adding password for user user1

[root@tianqi-01 vhost]# cat /usr/local/nginx/conf/htpasswd
tianqi:$apr1$MO1ysyDK$r0Eq.JoAuWRSVzXBYY1Xq0
user1:$apr1$ckJMbZrV$J1hEZ3dgpg7mwXe0nf/hD1
[root@tianqi-01 vhost]# 

10.检查配置nginx文件是否存在语法错误

[root@tianqi-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

11.重新加载配置文件

  • 在重新加载的时候,若配置文件中存在错误,配置文件将不会生效;
  • 如果是直接使用restart,如果配置有错,将会直接影响到网站的运行

[root@tianqi-01 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@tianqi-01 vhost]# 

12.测试

[root@tianqi-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.12.1</center>
</body>
</html>
[root@tianqi-01 vhost]# curl -x127.0.0.1:80 test.com -I
HTTP/1.1 401 Unauthorized
Server: nginx/1.12.1
Date: Mon, 12 Mar 2018 12:55:19 GMT
Content-Type: text/html
Content-Length: 195
Connection: keep-alive
WWW-Authenticate: Basic realm="Auth"

[root@tianqi-01 vhost]# 

13.会提示错误码401,就是需要用户,所以用curl指定用户

14.这时指定用户和密码再来访问,会提示404,这是因为去访问index.html,但是还未创建

[root@tianqi-01 vhost]# curl -utianqi: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.12.1</center>
</body>
</html>
[root@tianqi-01 vhost]# 

15.创建目录,然后新建index.html

[root@tianqi-01 vhost]# mkdir /data/wwwroot/test.com
[root@tianqi-01 vhost]# echo "test.com" > /data/wwwroot/test.com/index.html

[root@tianqi-01 vhost]# 

16.这时再来访问,会看到显示正常

[root@tianqi-01 vhost]# curl -utianqi:123456 -x127.0.0.1:80 test.com
test.com
[root@tianqi-01 vhost]# 

17.这里的用户认证是针对整站

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

  • 比如访问admin的时候,才需要认证

1.首先访问admin尝试下

[root@tianqi-01 vhost]# curl -utianqi: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.12.1</center>
</body>
</html>
[root@tianqi-01 vhost]# 

2.然后在/usr/local/nginx/conf/vhost/test.com.conf配置文件中定义,只需要在location / 后加上admin/ 目录即可

[root@tianqi-01 vhost]# vim test.com.conf

//在location后面添加 / admin  /即可

server
{
    listen 80;
    server_name test.com;
    index index.html index.htm index.php;
    root /data/wwwroot/test.com;

    location  /admin/
    {
        auth_basic              "Auth";
        auth_basic_user_file   /usr/local/nginx/conf/htpasswd;
    }
}

3.检查配置文件是否存在语法错误

[root@tianqi-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@tianqi-01 vhost]# 

4.重新加载配置文件

[root@tianqi-01 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@tianqi-01 vhost]# 

5.这时候再来访问test.com,就不需要指定用户名和密码了

[root@tianqi-01 vhost]# curl -x127.0.0.1:80 test.com
test.com
[root@tianqi-01 vhost]# 

6.访问test.com/admin/目录

[root@tianqi-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.12.1</center>
</body>
</html>
[root@tianqi-01 vhost]# 

7.这时创建一个测试页面

8.先新建目录

[root@tianqi-01 vhost]# mkdir /data/wwwroot/test.com/admin
[root@tianqi-01 vhost]# 

9.然后在admin目录下新建index.html

[root@tianqi-01 vhost]# echo "test.com admin dir" > /data/wwwroot/test.com/admin/index.html
[root@tianqi-01 vhost]# 

10.这时再来访问 test.com/admin/ 会显示401,但是指定用户名和密码后就会正常显示

[root@tianqi-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.12.1</center>
</body>
</html>
[root@tianqi-01 vhost]# curl -utianqi:123456 -x127.0.0.1:80 test.com/admin/
test.com admin dir
[root@tianqi-01 vhost]# 

针对URL

  • 比如针对admin.php

1.首先在配置文件/usr/local/nginx/conf/vhost/test.com.conf下定义,在 location 后加~ admin.php即可

[root@tianqi-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.php    //在location后面加~admin.php
    {
        auth_basic              "Auth";
        auth_basic_user_file   /usr/local/nginx/conf/htpasswd;
    }
}

保存退出

2.检查配置文件是否存在语法错误

[root@tianqi-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@tianqi-01 vhost]# 

3.重新加载配置文件

[root@tianqi-01 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@tianqi-01 vhost]# 

4.这时候就可以直接访问 test.com/admin/,不需要指定用户名和密码了,但是在访问admin.php的时候,则会显示401——>状态码为401说明需要验证

[root@tianqi-01 vhost]# curl -x127.0.0.1:80 test.com/admin/
test.com admin dir
[root@tianqi-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.12.1</center>
</body>
</html>
[root@tianqi-01 vhost]# 

12.9Nginx域名重定向

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 ” 设置了多个域名,就会使网站的权重变了,到底需要哪个域名为主站点,所以需要域名重定向

1.修改配置文件vim /usr/local/nginx/conf/vhost/test.com.conf,(这里删除用户认证那一块代码)

[root@tianqi-01 vhost]# !vim
vim test.com.conf

server
{
    listen 80;
    server_name test.com test2.com test3.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

2.检查配置文件语法错误,并重新加载配置文件

[root@tianqi-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@tianqi-01 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@tianqi-01 vhost]# 

3.测试,用test2.com去访问,会看到显示301,给它重定向到了http://test.com/index.html

[root@tianqi-01 vhost]# curl -x127.0.0.1:80 test2.com/index.html -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.1
Date: Mon, 12 Mar 2018 14:00:50 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/index.html

[root@tianqi-01 vhost]# 

4.定义一个不同的网址再来访问

[root@tianqi-01 vhost]# curl -x127.0.0.1:80 test2.com/dagrfe -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.1
Date: Mon, 12 Mar 2018 14:01:56 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/dagrfe

[root@tianqi-01 vhost]# curl -x127.0.0.1:80 test3.com/dagrfe -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.1
Date: Mon, 12 Mar 2018 14:04:37 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/dagrfe

[root@tianqi-01 vhost]# 

5.它会访问默认虚拟主机

6.这时若是随意访问一个不存在的网址,则会显示404

[root@tianqi-01 vhost]# curl -x127.0.0.1:80 tianqi.com/admin/index.html -I
HTTP/1.1 404 Not Found
Server: nginx/1.12.1
Date: Mon, 12 Mar 2018 14:03:29 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive

[root@tianqi-01 vhost]# 

友情链接:阿铭Linux

友情链接:nginx.conf 配置详解1

友情链接:nginx.conf 配置详解2

友情链接:nginx rewrite四种flag

转载于:https://my.oschina.net/u/3744518/blog/1633606

相关文章:

  • 【面试题】2018年最全Java面试通关秘籍汇总集!
  • Win8Metro(C#)数字图像处理--2.18图像平移变换
  • 银行卡号编码规则
  • iOS开发:瀑布流效果的实现(使用UICollectionView)
  • 666!中国企业凭人脸识别勇夺“金牌”
  • CentOS7.2安装配置nginx+flask+python+uwsgi运行环境
  • 快速理解Linux内核态与用户态
  • python3 写一个简单的websocket程序(转)
  • 自测之Lesson16:并发通信
  • 软工作业PSP与单元测试训练
  • ElasticSearch入门及核心概念介绍
  • 软件工程第二周阅读作业
  • 前台vue的使用简单小结
  • SSRF(服务端请求伪造)
  • ubuntu下unzip解压zip文件中文乱码问题
  • [Vue CLI 3] 配置解析之 css.extract
  • [分享]iOS开发 - 实现UITableView Plain SectionView和table不停留一起滑动
  • Codepen 每日精选(2018-3-25)
  • gulp 教程
  • Java 网络编程(2):UDP 的使用
  • leetcode378. Kth Smallest Element in a Sorted Matrix
  • LeetCode算法系列_0891_子序列宽度之和
  • Otto开发初探——微服务依赖管理新利器
  • python学习笔记-类对象的信息
  • storm drpc实例
  • Vue学习第二天
  • 阿里中间件开源组件:Sentinel 0.2.0正式发布
  • 初识MongoDB分片
  • 服务器之间,相同帐号,实现免密钥登录
  • 开放才能进步!Angular和Wijmo一起走过的日子
  • 理清楚Vue的结构
  • 掌握面试——弹出框的实现(一道题中包含布局/js设计模式)
  • 06-01 点餐小程序前台界面搭建
  • # Pytorch 中可以直接调用的Loss Functions总结:
  • # 睡眠3秒_床上这样睡觉的人,睡眠质量多半不好
  • #define
  • #LLM入门|Prompt#3.3_存储_Memory
  • (4) PIVOT 和 UPIVOT 的使用
  • (aiohttp-asyncio-FFmpeg-Docker-SRS)实现异步摄像头转码服务器
  • (Redis使用系列) Springboot 整合Redisson 实现分布式锁 七
  • (八)光盘的挂载与解挂、挂载CentOS镜像、rpm安装软件详细学习笔记
  • (板子)A* astar算法,AcWing第k短路+八数码 带注释
  • (附源码)springboot学生选课系统 毕业设计 612555
  • (附源码)ssm基于web技术的医务志愿者管理系统 毕业设计 100910
  • (附源码)流浪动物保护平台的设计与实现 毕业设计 161154
  • (十) 初识 Docker file
  • .NET CF命令行调试器MDbg入门(三) 进程控制
  • .net 写了一个支持重试、熔断和超时策略的 HttpClient 实例池
  • .Net 中Partitioner static与dynamic的性能对比
  • .NET 中的轻量级线程安全
  • .NET/C# 判断某个类是否是泛型类型或泛型接口的子类型
  • .NET/C# 中你可以在代码中写多个 Main 函数,然后按需要随时切换
  • .NET单元测试
  • .NET框架
  • .one4-V-XXXXXXXX勒索病毒数据怎么处理|数据解密恢复