shiyuelp

随着微服务的也来越多,每个服务都有单独的文档,那么问题来了,怎么把所有文档整合在一起呢

本方法采用服务器拦截的方式进行处理

首先需要在opt 的主目录中 /opt/ 创建一个新文件 htpasswd
此文件的书写格式是
用户名:密码
每行一个账户
并且 密码必须使用函数 crypt(3) 加密
官方档说 可以用 Apache 的 htpasswd 工具来创建密码文件
[root@localhost /]# htpasswd
-bash: htpasswd: command not found
[root@localhost /]#
如果上述提示则需要安装httpd
yum install httpd
安装好后执行如下命令
htpasswd -c /opt/nginxpwd user
New password:123456
Re-type new password:123456
Adding password for user ngin
生成用户密钥文件为nginxpwd 用户名为user 密码为123456

密码文件生成好后,在 nginx.conf 文件中对应的 server 段中 添加如下内容
auth_basic "Welcome Back! GUOYU!";
auth_basic_user_file /opt/nginxpwd;
如果想限制某一个目录的话需要如下配置:
location ^~ /test/ {
auth_basic "TEST-Login!";
auth_basic_user_file /opt/nginxpwd;
}
如果 不用 ^~ /test/ 而用 /test 的话 那么将只能对目录进行验证直接访问其下的文件,将不会弹出登录验证

重启Nginx服务,使配置生效
或者 ./nginx -s reload

#dms server
location ~ /.*/swagger-ui.html.* {
  #密码登录
  auth_basic "接口文档";
  auth_basic_user_file /opt/nginxpwd;
  # 不同的路径进入到不同地址,还不知道怎么改成一个通配符,自动填充
  if ( $uri ~ /smsapi/.* ) {
    proxy_pass http://smsapi;
  }
  if ( $uri ~ /find/.* ) {
    proxy_pass http://find;
  }
  proxy_redirect off;
  proxy_set_header Host $host;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

location = /swagger-ui.html {
  #密码登录
  auth_basic "宥马接口文档";
  auth_basic_user_file /opt/nginxpwd;
  root /fileDir;
  index swagger-ui.html;
}

相关文章: