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

Wesley13
• 阅读 543

本文索引:

  • 配置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; # 设置服务器的名称 server_name aaa.com; # 设置服务器默认网页 index index.html index.htm index.php; # 设置服务器的根目录 root /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; # 添加下列代码 location / { auth_basic "Auth"; auth_basic_user_file /usr/local/nginx/conf/htpasswd; } }

  • 创建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; # 修改location即可,其他都不变 location /admin/ { auth_basic "Auth"; auth_basic_user_file /usr/local/nginx/conf/htpasswd; } }

  • 重启服务

    [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/

    401 Authorization Required

    401 Authorization Required


    nginx/1.12.2

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

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

[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;
    
    # 修改location即可,其他都不变,这里匹配admin.php只是对简单的表示
    # 可以使用更复杂的正则来显示精准的文件认证
    location ~ admin.php
        {
        auth_basic "Auth";
        auth_basic_user_file /usr/local/nginx/conf/htpasswd;
    }
}
  • 重启服务

    [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

    401 Authorization Required

    401 Authorization Required


    nginx/1.12.2

域名重定向

  • 修改虚拟主机配置文件

    [root@localhost default]# vim /usr/local/nginx/conf/vhost/test.com.conf server { listen 80; # nginx可以配置多个主机名,apache只能使用ServerAlias来指定别名 server_name test.com test2.com; index index.html index.htm index.php; root /data/www/test.com; # 在多个域名 # 判断host是否为test.com if ($host != 'test.com') { rewrite ^/(.*)$ http://test.com/$1 permanent; } }

  • 重启服务

    [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

    301 Moved Permanently

    301 Moved Permanently


    nginx/1.12.2
    [root@localhost default]# curl -x 127.0.0.1:80 test2.com/admin/index.html 301 Moved Permanently

    301 Moved Permanently


    nginx/1.12.2
    [root@localhost default]# curl -x 127.0.0.1:80 test3.com/index.html aaa.com

点赞
收藏
评论区
推荐文章
blmius blmius
1年前
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
Wesley13 Wesley13
1年前
nginx配置虚拟主机相关教程
1.配置虚拟主机配置虚拟主机即:在一台服务器上启动多个网站;区分不同的网站方式:域名不同,端口不同;1.1通过端口区分虚拟主机1.1.1server节点的介绍nginx的配置文件路径:/usr/local/nginx/conf/nginx.conf一个se
SPDK QOS机制解析
本文关键词:intelspdkbdevqos序:intelspdk软件在存储领域应用广泛。因其可以高效管理linux系统的nvmessd盘,又支持vhostuser协议可以对接qemu虚拟机,在云计算领域通常被用来做本地盘云主机的存储管理软件。如此优秀的一款软件,有必要仔细分析其内部的实现机制,本篇文章主要介绍spdkqos机制。spdk
天翼云高可用虚拟IP(HAVIP)实践
(一)产品概述天翼云高可用虚拟IP(HighAvailabilityVirtualIPAddress,简称HAVIP)是一种可用独立创建和删除的私有网络IP地址资源。通过在VIPCIDR中申请一个私有网络IP地址,然后与高可用软件(如高可用软件Keepalived)配合使用,可用在VPC中搭建高可用的主备集群服务,提高VPC中服务的可用性。限制和说明
一个关于SDWAN单臂部署方案验证的实验
假设有这样一张网络,其中RTA和PCA表示某公司的A分支,通过中国电信CT路由器接入互联网ISP;RTB和PCB表示某公司的B分支,通过中国联通CU路由器接入互联网ISP。DNS(8.8.8.8)表示某互联网应用。为实现A分支私网192.168.2.0/24和B分支私网192.168.3.0/24的互通,现计划使用某厂商的SDWAN方案进打通两个内网,像下图
高性能API网关Kong介绍
本文关键词:高性能、API网关、Kong、微服务1.Introduction是随着微服务(Microservice)概念兴起的一种架构模式。原本一个庞大的单体应用(Allinone)业务系统被拆分成许多微服务(Microservice)系统进行独立的维护和部署,服务拆分带来的变化是API的规模成倍增长,API的管理难度也在日益增加,使用API网关发布和管
SPDK对接Ceph性能优化
关键词:SPDK、NVMeOF、Ceph、CPU负载均衡SPDK是intel公司主导开发的一套存储高性能开发套件,提供了一组工具和库,用于编写高性能、可扩展和用户态存储应用。它通过使用一些关键技术实现了高性能:1.将所有必需的驱动程序移到用户空间,以避免系统调用并且支持零拷贝访问2.IO的完成通过轮询硬件而不是依赖中断,以降低时延3.使用消息传递,以避免IO
3A网络 3A网络
5个月前
理解 virt、res、shr 之间的关系(linux 系统篇)
理解virt、res、shr之间的关系(linux系统篇)前言想必在linux上写过程序的同学都有分析进程占用多少内存的经历,或者被问到这样的问题——你的程序在运行时占用了多少内存(物理内存)?通常我们可以通过t
3A网络 3A网络
5个月前
开发一个不需要重写成 Hive QL 的大数据 SQL 引擎
开发一个不需要重写成HiveQL的大数据SQL引擎学习大数据技术的核心原理,掌握一些高效的思考和思维方式,构建自己的技术知识体系。明白了原理,有时甚至不需要学习,顺着原理就可以推导出各种实现细节。各种知识表象看杂乱无章,若只是学习
初识DevOps
基本概念和延伸的思考DevOps,是Development(开发)和Operations(运维)组成的复合词,一般译为“开发运维一体化”。看到这个概念,首先会产生几个问题:开发是什么,哪些环节是开发?运维是什么,哪些环节是运维?开发人员写好代码在本地调试,环境出问题了自己来调整,这是开发工作还是运维工作?系统故障后,运维人员发现是配置文件内容出错了就改成了正