小知识:Nginx 运维之域名验证的方法示例

各公众平台在配置接口域名时会验证开发者对域名的配置权, 生成随机的文本及字符串,让放置在域名根目录可以通过域名直接访问到即通过验证

示例为验证域名 abc.com 可以通过根路由访问 6CysNYj8Hb.txt 响应体为字符串 01df2ddab4774ba2676a5563ccb79ffa。

?
1
2
$ curl https://abc.com/6CysNYj8Hb.txt
01df2ddab4774ba2676a5563ccb79ffa

方案一

配置有 root 的 server,直接把随机文档放置在该目录下即可,不需要重启 nginx 服务。

方案二

匹配路由,指定随机文档所在目录, 需要重启 nginx。

?
1
2
3
location ~* 6CysNYj8Hb\.txt {
root /data/ftp;
}

方案三(推荐)

匹配路由,直接返回需要验证的随机字符串,需要重启 nginx。

?
1
2
3
4
location = /6CysNYj8Hb.txt {
default_type text/html;
return 200 01df2ddab4774ba2676a5563ccb79ffa;
}

参考

Nginx 的 Location 从零开始配置

nginx 配置返回文本或 json

补充:Nginx域名重定向

1、更改配置文件test.com.conf

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

2、curl测试

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
[root@jimmylinux-001 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@jimmylinux-001 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@jimmylinux-001 vhost]# curl -x127.0.0.1:80 test2.com/index.html -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.1
Date: Thu, 07 Jun 2018 16:47:36 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/index.html
[root@jimmylinux-001 vhost]# curl -x127.0.0.1:80 test2.com/admin/index.html -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.1
Date: Thu, 07 Jun 2018 16:48:08 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/admin/index.html
[root@jimmylinux-001 vhost]# curl -x127.0.0.1:80 test3.com/admin/index.html/adjlfj -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.1
Date: Thu, 07 Jun 2018 16:48:35 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/admin/index.html/adjlfj
[root@jimmylinux-001 vhost]# curl -x127.0.0.1:80 test4.com/admin/index.html/adjlfj -I
HTTP/1.1 404 Not Found
Server: nginx/1.12.1
Date: Thu, 07 Jun 2018 16:48:43 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://www.jianshu.com/p/73054d74ddc6

声明: 猿站网有关资源均来自网络搜集与网友提供,任何涉及商业盈利目的的均不得使用,否则产生的一切后果将由您自己承担! 本平台资源仅供个人学习交流、测试使用 所有内容请在下载后24小时内删除,制止非法恶意传播,不对任何下载或转载者造成的危害负任何法律责任!也请大家支持、购置正版! 。本站一律禁止以任何方式发布或转载任何违法的相关信息访客发现请向站长举报,会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。本网站的资源部分来源于网络,如有侵权烦请发送邮件至:2697268773@qq.com进行处理。
建站知识

小知识:手把手教您在 Linux 上使用 GPG 加解密文件

2023-4-12 2:03:48

建站知识

小知识:Linux内核增加HDF驱动子系统

2023-4-12 2:12:21

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索