阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Nginx服务的SSL认证和htpasswd认证

135次阅读
没有评论

共计 1888 个字符,预计需要花费 5 分钟才能阅读完成。

默认 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

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计1888字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中