码迷,mamicode.com
首页 > 其他好文 > 详细

Nginx服务的ssl认证和htpasswd认证

时间:2016-06-20 22:28:58      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:nginx

默认Nginx是没有ssl模块的,需要在编译安装的时候添加上ssl模块!

首先是需要安装Nginx服务,这里我们不做详细介绍,具体安装可以参考我的“Nginx介绍及安装配置”。

使用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认证访问

由于htpasswdhttp服务带的命令,所以我们可以安装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://sgk2011.blog.51cto.com/1551358/1791148

Nginx服务的ssl认证和htpasswd认证

标签:nginx

原文地址:http://sgk2011.blog.51cto.com/1551358/1791148

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!