Nginx 运维之域名验证的方法示例
各公众平台在配置接口域名时会验证开发者对域名的配置权,生成随机的文本及字符串,让放置在域名根目录可以通过域名直接访问到即通过验证。
示例为验证域名abc.com可以通过根路由访问6CysNYj8Hb.txt响应体为字符串01df2ddab4774ba2676a5563ccb79ffa。
$curlhttps://abc.com/6CysNYj8Hb.txt 01df2ddab4774ba2676a5563ccb79ffa
方案一
配置有root的server,直接把随机文档放置在该目录下即可,不需要重启nginx服务。
方案二
匹配路由,指定随机文档所在目录,需要重启nginx。
location~*6CysNYj8Hb\.txt{ root/data/ftp; }
方案三(推荐)
匹配路由,直接返回需要验证的随机字符串,需要重启nginx。
location=/6CysNYj8Hb.txt{ default_typetext/html; return200'01df2ddab4774ba2676a5563ccb79ffa'; }
参考
Nginx的Location从零开始配置
nginx配置返回文本或json
补充:Nginx域名重定向
1、更改配置文件test.com.conf
[root@jimmylinux-001vhost]#vimtest.com.conf server { listen80; server_nametest.comtest2.comtest3.com; indexindex.htmlindex.htmindex.php; root/data/wwwroot/test.com; if($host!='test.com'){ rewrite^/(.*)$http://test.com/$1permanent; } }
2、curl测试
[root@jimmylinux-001vhost]#/usr/local/nginx/sbin/nginx-t nginx:theconfigurationfile/usr/local/nginx/conf/nginx.confsyntaxisok nginx:configurationfile/usr/local/nginx/conf/nginx.conftestissuccessful [root@jimmylinux-001vhost]#/usr/local/nginx/sbin/nginx-sreload [root@jimmylinux-001vhost]#curl-x127.0.0.1:80test2.com/index.html-I HTTP/1.1301MovedPermanently Server:nginx/1.12.1 Date:Thu,07Jun201816:47:36GMT Content-Type:text/html Content-Length:185 Connection:keep-alive Location:http://test.com/index.html [root@jimmylinux-001vhost]#curl-x127.0.0.1:80test2.com/admin/index.html-I HTTP/1.1301MovedPermanently Server:nginx/1.12.1 Date:Thu,07Jun201816:48:08GMT Content-Type:text/html Content-Length:185 Connection:keep-alive Location:http://test.com/admin/index.html [root@jimmylinux-001vhost]#curl-x127.0.0.1:80test3.com/admin/index.html/adjlfj-I HTTP/1.1301MovedPermanently Server:nginx/1.12.1 Date:Thu,07Jun201816:48:35GMT Content-Type:text/html Content-Length:185 Connection:keep-alive Location:http://test.com/admin/index.html/adjlfj [root@jimmylinux-001vhost]#curl-x127.0.0.1:80test4.com/admin/index.html/adjlfj-I HTTP/1.1404NotFound Server:nginx/1.12.1 Date:Thu,07Jun201816:48:43GMT Content-Type:text/html Content-Length:169 Connection:keep-alive
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。