[LNMP]用户认证与域名重定向

简介:

用户认证

1、创建虚拟主机

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@juispan vhost] # vi /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 ;
     }
}

2、创建用户

1
2
3
4
5
[root@juispan vhost] # yum install -y httpd
[root@juispan vhost] # htpasswd -c /usr/local/nginx/conf/htpasswd juispan
New password: 
Re- type  new password: 
Adding password  for  user juispan

3、检查与启动

1
2
3
4
[root@juispan 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@juispan vhost] # /usr/local/nginx/sbin/nginx -s reload

4、测试效果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@juispan vhost] # mkdir /data/wwwroot/test.com
[root@juispan vhost] # echo “test.com”>/data/wwwroot/test.com/index.html
[root@juispan vhost] # curl -x127.0.0.1:80 test.com -I
HTTP /1 .1 401 Unauthorized
Server: nginx /1 .12.1
Date: Tue, 08 Aug 2017 14:32:30 GMT
Content-Type: text /html
Content-Length: 195
Connection: keep-alive
WWW-Authenticate: Basic realm= "Auth"
 
[root@juispan vhost] #  curl -ujuispan:mypasswd.com -x127.0.0.1:80 test.com -I
HTTP /1 .1 200 OK
Server: nginx /1 .12.1
Date: Tue, 08 Aug 2017 14:33:17 GMT
Content-Type: text /html
Content-Length: 15
Last-Modified: Tue, 08 Aug 2017 14:32:28 GMT
Connection: keep-alive
ETag:  "5989cb7c-f"
Accept-Ranges: bytes


域名重定向

1、编辑虚拟主机

1
2
3
4
5
6
7
8
9
10
11
[root@juispan vhost] # vi /usr/local/nginx/conf/vhost/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;   ##永久重定向
     }
}

2、检查与启动

1
2
3
4
[root@juispan 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@juispan vhost] # /usr/local/nginx/sbin/nginx -s reload

3、测试效果

1
2
3
4
5
6
7
8
[root@juispan vhost] # curl -x127.0.0.1:80 test1.com/abc.txt -I
HTTP /1 .1 301 Moved Permanently    ##permanent 301,redirect 302
Server: nginx /1 .12.1
Date: Tue, 08 Aug 2017 14:41:58 GMT
Content-Type: text /html
Content-Length: 185
Connection: keep-alive
Location: http: //test .com /abc .txt











本文转自Grodd51CTO博客,原文链接:http://blog.51cto.com/juispan/1955287 ,如需转载请自行联系原作者
相关实践学习
基于函数计算快速搭建Hexo博客系统
本场景介绍如何使用阿里云函数计算服务命令行工具快速搭建一个Hexo博客。
相关文章
【图文】怎么配二级域名?怎么做URL重定向?
1、登录阿里云,进入控制台2、在左侧菜单找到域名》进入域名列表3、点击对应域名右侧的管理,进入单域名管理控制台4、选择域名解析5、点击右侧的添加记录6、这里就是关键了A记录就可以创建一个标准的二级域名;显性URL或隐性URL,就可以将一个域名、二级域名指向一个URL地址。
5484 0
|
应用服务中间件 nginx
Nginx:不带www域名301重定向到www域名
Nginx:不带www域名301重定向到www域名
300 0
|
XML 开发框架 .NET
泛解析泛域名301重定向带参数跳转在iis的web.config中的设置方式 二级域名301重定向
泛解析泛域名301重定向带参数跳转在iis的web.config中的设置方式 二级域名301重定向
556 0
泛解析泛域名301重定向带参数跳转在iis的web.config中的设置方式 二级域名301重定向
|
Windows
【错误记录】GitHub 网站和仓库无法访问 ( 域名重定向 | 检查 C:\Windows\System32\drivers\etc\hosts 配置文件中的 GitHub 地址域名配置 )
【错误记录】GitHub 网站和仓库无法访问 ( 域名重定向 | 检查 C:\Windows\System32\drivers\etc\hosts 配置文件中的 GitHub 地址域名配置 )
438 0
【错误记录】GitHub 网站和仓库无法访问 ( 域名重定向 | 检查 C:\Windows\System32\drivers\etc\hosts 配置文件中的 GitHub 地址域名配置 )
|
安全 Ubuntu 应用服务中间件
win10子系统利用docker搭建LNMP开发环境,配置域名访问
<p>  #以前用子系统WSL2时搭建LNMP开发环境时,由于底层使用虚拟化技术,导致重启ip就变化,当使用虚拟域名访问多个项目时,变的非常麻烦,最近,空闲时间有些,又查了些资料,终于搞定了,那些以前的文章都是些写个脚本重置ip等,这个方法不需要,在这里记录一下,对于那些手上有好多项目的程序汪来说,真是相当于有个女朋友了,再也不用localhost:端口号来访问了</p>
337 0
|
弹性计算 小程序 网络协议
ECS新人用户快速完成LNMP环境搭建、域名注册、SSL证书申请
服务器新人快速上手ECS的亲身经验体会,基于阿里云ecs.t6-c1m2.large ,使用centOS操作系统。
469 0
ECS新人用户快速完成LNMP环境搭建、域名注册、SSL证书申请
|
弹性计算 开发框架 .NET
修改.htaccess域名www与不带www301重定向跳转重写URL
Apache下web环境,通过修改.htaccess文件即可实现重写URL,尤其是处于SEO考虑,将域名格式统一,例如将带www的域名统一301重定向到不带www的域名,或者将不带www的域名统一301重定向到带www的域名。
1501 0
|
JavaScript 前端开发 数据采集
Github Pages页面重定向到新网址,实现域名跳转
1、前言 最新还是决下心来换域名啦!从 http://ihtc.cc 换成 https://ihtcboy.com !然后问题就来了,以前文章的链接打开404了,旧域名也访问不通,这样子不行呢!用了2年多的旧域名,在百度谷歌还是积累了一些爬虫,还有很多分享到第三方平台的文章呢!想一想,还是想救一救它!!! 2、准备工作 首先,因为是2个域名,所以我分别用2个GitHub账号设置对应的Repo地址解析。
4965 0