阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

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

177次阅读
没有评论

共计 3058 个字符,预计需要花费 8 分钟才能阅读完成。

Nginx 默认虚拟主机定义默认虚拟主机配置文件, 在 http 下面加入 include vhost/*.conf

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

在 /usr/local/nginx/conf/ 下创建目录

#mkdir vhost/                           // 创建 vhost 目录
#cd 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 /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             // 重新加载 nginx
#curl   -x127.0.0.1:80    aaa.com                          // 测试
                                      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;               //密码所在的位置文件
}
}

htpasswd 是 apache 的一个工具,该工具主要用于建立和更新存储用户名、密码的文本文件,主要用于对基于 http 用户的认证。htpasswd 的安装很简单,它是随 apache 的安装而生成。

#yum install -y httpd                                                  // 下载 httpd 包
#htpasswd -c /usr/local/nginx/conf/htpasswd zenwen                      // 生成密码文件
#/usr/local/nginx/sbin/nginx  -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 说明需要验证

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

#curl -uzenwen:passwd -x127.0.0.1:80 test.com -I               // 访问状态码变为 200

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

针对目录的用户认证
当访问 admin 时,只需要在 location / 加上 admin 目录就可以对 admin 进行用户的验证
location  /admin/    
    {auth_basic              "Auth";        
            auth_basic_user_file   /usr/local/nginx/conf/htpasswd;
}
                                                  nginx 域名重定向
更改 test.com.conf
server{listen 80;    
         server_name test.comtest1.comtest2.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
# /usr/local/nginx/sbin/nginx  -t                  // 检测语法
#/usr/local/nginx/sbin/nginx  -s reload           // 重新加载
#curl -x127.0.0.1:80 test2.com/index.html  -I                      // 测试

HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.1
Date: Wed, 03 Jan 2018 02:45:07 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/index.html // 直接重定向到了 test.com

#curl -x127.0.0.1:80 test1.com/index.html  -I                     // 显示状态码为 301

HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.1
Date: Wed, 03 Jan 2018 06:53:13 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/index.html

下面关于 Nginx 的文章您也可能喜欢,不妨参考下:

Nginx 403 forbidden 的解决办法  http://www.linuxidc.com/Linux/2017-08/146084.htm

CentOS 7 下 Nginx 服务器的安装配置  http://www.linuxidc.com/Linux/2017-04/142986.htm

CentOS 上安装 Nginx 服务器实现虚拟主机和域名重定向  http://www.linuxidc.com/Linux/2017-04/142642.htm

CentOS 6.8 安装 LNMP 环境(Linux+Nginx+MySQL+PHP)http://www.linuxidc.com/Linux/2017-04/142880.htm

Linux 下安装 PHP 环境并配置 Nginx 支持 php-fpm 模块  http://www.linuxidc.com/Linux/2017-05/144333.htm

Nginx 服务的 SSL 认证和 htpasswd 认证  http://www.linuxidc.com/Linux/2017-04/142478.htm

Ubuntu 16.04 上启用加密安全的 Nginx Web 服务器  http://www.linuxidc.com/Linux/2017-07/145522.htm

Linux 中安装配置 Nginx 及参数详解  http://www.linuxidc.com/Linux/2017-05/143853.htm

Nginx 日志过滤 使用 ngx_log_if 不记录特定日志 http://www.linuxidc.com/Linux/2014-07/104686.htm

CentOS 7.2 下 Nginx+PHP+MySQL+Memcache 缓存服务器安装配置  http://www.linuxidc.com/Linux/2017-03/142168.htm

CentOS6.9 编译安装 Nginx1.4.7  http://www.linuxidc.com/Linux/2017-06/144473.htm

Nginx 的详细介绍:请点这里
Nginx 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2018-01/150064.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计3058字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中