LNMP架构之虚拟主机配置、用户认证及域名重定向

本文索引:

  • 配置nginx虚拟主机
  • nginx用户认证
    • 针对目录
    • 针对文件
  • 域名重定向

配置nginx虚拟主机

  • 修改nginx主配置文件

    [root@localhost nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf

    删除原有的server语句块,替换为下面的代码

    include vhost/*.conf;

  • 创建并修改虚拟主机配置文件(默认虚拟主机)

    [root@localhost nginx-1.12.2]# cd /usr/local/nginx/conf [root@localhost conf]# mkdir vhost [root@localhost conf]# cd vhost/ [root@localhost vhost]# vim aaa.com.conf server { # 指定监听80端口,并将该虚拟主机设置为默认虚拟主机 listen 80 default_server;

    1# 设置服务器的名称 2server_name aaa.com; 3 4# 设置服务器默认网页 5index index.html index.htm index.php; 6 7# 设置服务器的根目录 8root /data/www/default;

    }

  • 创建默认虚拟主机的根目录及默认页面

    [root@localhost vhost]# mkdir -p /data/www/default [root@localhost vhost]# cd /data/www/default/

    [root@localhost default]# vim index.html aaa.com

  • 检测代码并重启服务

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

  • 效果测试

    [root@localhost default]# curl -x 127.0.0.1:80 aaa.com aaa.com

    由于是默认的虚拟主机,任何域名都可以显示默认网页信息

    [root@localhost default]# curl -x 127.0.0.1:80 bbb.com aaa.com


nginx用户认证

nginx中一个虚拟主机对于一个配置文件

  • 创建新的虚拟主机配置文件

    [root@localhost default]# vim /usr/local/nginx/conf/vhost/test.com.conf server { # 这个不是默认虚拟主机,default_server不需要配置 listen 80; server_name test.com; index index.html index.htm index.php; root /data/www/test.com;

    1# 添加下列代码 2location / 3 { 4 auth_basic "Auth"; 5 auth_basic_user_file /usr/local/nginx/conf/htpasswd; 6}

    }

  • 创建test.com相关目录和文件

    [root@localhost default]# mkdir /data/www/test.com [root@localhost default]# vim /data/www/test.com/index.html test.com

  • 创建密码文件 由于用户认证密码文件需要使用apache的htpasswd命令生成,安装httpd,并创建用户

    [root@localhost default]# yum install -y httpd [root@localhost default]# htpasswd -c /usr/local/nginx/conf/htpasswd test New password: Re-type new password: Adding password for user test

  • 重启服务

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

  • 测试效果

    普通访问

    [root@localhost default]# curl -x 127.0.0.1:80 test.com -I HTTP/1.1 401 Unauthorized Server: nginx/1.12.2 Date: Sun, 31 Dec 2017 06:55:24 GMT Content-Type: text/html Content-Length: 195 Connection: keep-alive WWW-Authenticate: Basic realm="Auth"

    指定用户访问

    [root@localhost default]# curl -x 127.0.0.1:80 -utest:1 test.com -I HTTP/1.1 200 OK Server: nginx/1.12.2 Date: Sun, 31 Dec 2017 06:55:33 GMT Content-Type: text/html Content-Length: 8 Last-Modified: Sun, 31 Dec 2017 06:17:09 GMT Connection: keep-alive ETag: "5a4880e5-8" Accept-Ranges: bytes [root@localhost default]# curl -x 127.0.0.1:80 -utest:1 test.com test.com

针对虚拟主机下的某个目录进行认证

  • 修改代码 针对某个目录进行的认证,只需对上述的代码进行简单修改即可;

    [root@localhost 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/www/test.com;

    1# 修改location即可,其他都不变 2location /admin/ 3 { 4 auth_basic "Auth"; 5 auth_basic_user_file /usr/local/nginx/conf/htpasswd; 6}

    }

  • 重启服务

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

  • 验证

    test.com可以访问

    [root@localhost default]# curl -x 127.0.0.1:80 test.com test.com

    test.com下的admin目录需要用户认证

    [root@localhost default]# curl -x 127.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.2</center> </body> </html>

针对虚拟主机下的某个文件(访问的URL)进行认证

*( 修改虚拟主机配置文件(使用~匹配文件)

1[root@localhost default]# vim /usr/local/nginx/conf/vhost/test.com.conf 2server 3{ 4 listen 80; 5 server_name test.com; 6 index index.html index.htm index.php; 7 root /data/www/test.com; 8 9 # 修改location即可,其他都不变,这里匹配admin.php只是对简单的表示 10 # 可以使用更复杂的正则来显示精准的文件认证 11 location ~ admin.php 12 { 13 auth_basic "Auth"; 14 auth_basic_user_file /usr/local/nginx/conf/htpasswd; 15 } 16}
  • 重启服务

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

  • 验证

    [root@localhost default]# curl -x 127.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.2</center> </body> </html>

域名重定向

  • 修改虚拟主机配置文件

    [root@localhost default]# vim /usr/local/nginx/conf/vhost/test.com.conf server { listen 80;

    1# nginx可以配置多个主机名,apache只能使用ServerAlias来指定别名 2server_name test.com test2.com; 3index index.html index.htm index.php; 4root /data/www/test.com; 5 6# 在多个域名 7# 判断host是否为test.com 8if ($host != 'test.com') { 9rewrite ^/(.*)$ http://test.com/$1 permanent; 10}

    }

  • 重启服务

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

  • 验证

    [root@localhost default]# curl -x 127.0.0.1:80 test2.com/index.html

    <html> <head><title>301 Moved Permanently</title></head> <body bgcolor="white"> <center><h1>301 Moved Permanently</h1></center> <hr><center>nginx/1.12.2</center> </body> </html> [root@localhost default]# curl -x 127.0.0.1:80 test2.com/admin/index.html <html> <head><title>301 Moved Permanently</title></head> <body bgcolor="white"> <center><h1>301 Moved Permanently</h1></center> <hr><center>nginx/1.12.2</center> </body> </html> [root@localhost default]# curl -x 127.0.0.1:80 test3.com/index.html aaa.com

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )

Nginx配置https

一、开启nginx的ssl模块1.未安装过nginx,编译安装配置参数如下:./configure\prefix/usr/local/nginx\withpcre\withhttp\_ssl\_modulessl模块\withhttp\_stub\_status\_module\wit