你好,游客 登录 注册 搜索
背景:
阅读新闻

Nginx服务的SSL认证和htpasswd认证

[日期:2017-04-03] 来源:Linux社区  作者:sgk2011 [字体: ]

默认Nginx是没有SSL模块的,需要在编译安装的时候添加上ssl模块!首先是需要安装Nginx服务,这里我们不做详细介绍,具体安装可以参考我的“Nginx介绍及安装配置” http://www.linuxidc.com/Linux/2017-04/142479.htm

使用Openssl生成证书

1、生成RSA密钥的方法
[root@web01 conf]# openssl genrsa -des3 -out kell.key 1024
Generating RSA private key, 1024 bit long modulus
.++++++
..++++++
e is 65537 (0x10001)
Enter pass phrase for kell.key:
Verifying - Enter pass phrase for kell.key:
2、生成一个证书请求
[root@web01 conf]# openssl req -new -keykell.key -out kell.csr
这里会要求输入省份,国家等一些信息,在email要输入域名后缀一样,密码是上一步输入的密码
3、拷贝一个不需要输入密码的密钥文件
[root@web01 conf]# openssl rsa -in kell.key -out kell_nopass.key
Enter pass phrase for kell.key:
writing RSA key
密码输入上一次输入的密码
4、自己签发证书
[root@web01 conf]#openssl x509 -req -days 365 -in kell.csr -signkey kell.key -out kell.crt

配置Nginx配置文件
http {
    include      mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        #listen      80;
        listen      443;
        ssl on;
        ssl_certificate /application/nginx/conf/kell.crt;
        ssl_certificate_key /application/nginx/conf/kell_nopass.key;
        server_name  www.etiantian.org;
        location / {
            root  html/www;
            rewrite ^/(.*) https://$host/$1 permanent;
            index  index.html index.htm;
            #rewrite ^/(.*) https://$host/$1 permanent;
        }
    }

通过浏览器https就可以访问了,如果需要rewrite,需要配置rewrite

通过htpasswd设置Nginx认证访问
由于htpasswd是http服务带的命令,所以我们可以安装http服务来得到这个命令,也可以只安装httpd-tools来安装这个命令,这样就不用安装整个http服务
安装htpasswd命令工具
[root@web01 conf]# yum install httpd-tools
[root@web01 conf]# which htpasswd
/usr/bin/htpasswd
创建账号&密码
[root@web01 conf]# htpasswd -bc /application/nginx/conf/htpasswd wangzhan 123456
Adding password for user wangzhan
[root@web01 conf]# chmod 400 /application/nginx/conf/htpasswd
[root@web01 conf]# chown nginx /application/nginx/conf/htpasswd
[root@web01 conf]# cat /application/nginx/conf/htpasswd
wangzhan:b2sfluv5673CE

在配置文件中添加下面两行
    auth_basic        "wangzhan 123456";
    auth_basic_user_file /application/nginx/conf/htpasswd;
重启服务,使用网站访问,会发现出现输入认证口令对话框

本文永久更新链接地址http://www.linuxidc.com/Linux/2017-04/142478.htm

linux
相关资讯       Nginx SSL  SSL认证  htpasswd认证 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款