默认Nginx是没有ssl模块的,需要在编译安装的时候添加上ssl模块!
首先是需要安装Nginx服务,这里我们不做详细介绍,具体安装可以参考我的“Nginx介绍及安装配置”。
使用Openssl生成证书 1、生成RSA密钥的方法 [iyunv@web01 conf]# openssl genrsa -des3-out kell.key 1024 GeneratingRSA private key, 1024 bit long modulus .++++++ ..++++++ eis 65537 (0x10001) Enter pass phrase for kell.key: Verifying - Enter pass phrase for kell.key: 2、生成一个证书请求 [iyunv@web01 conf]# openssl req -new -keykell.key -out kell.csr 这里会要求输入省份,国家等一些信息,在email要输入域名后缀一样,密码是上一步输入的密码 3、拷贝一个不需要输入密码的密钥文件 [iyunv@web01 conf]# openssl rsa -in kell.key -out kell_nopass.key Enter pass phrase for kell.key: writingRSA key 密码输入上一次输入的密码 4、自己签发证书 [iyunv@web01 conf]#opensslx509 -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; 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命令工具 [iyunv@web01conf]# yum install httpd-tools [iyunv@web01conf]# which htpasswd /usr/bin/htpasswd 创建账号&密码 [iyunv@web01conf]# htpasswd -bc /application/nginx/conf/htpasswd wangzhan 123456 Addingpassword for user wangzhan [iyunv@web01conf]# chmod 400 /application/nginx/conf/htpasswd [iyunv@web01conf]# chown nginx /application/nginx/conf/htpasswd [iyunv@web01conf]# cat /application/nginx/conf/htpasswd wangzhan:b2sfluv5673CE 在配置文件中添加下面两行 auth_basic "wangzhan123456"; auth_basic_user_file/application/nginx/conf/htpasswd; 重启服务,使用网站访问,会发现出现输入认证口令对话框
|