[App] Nginx 通过 https 方式访问

转自 http://www.linuxidc.com/Linux/2013-02/80069p4.htm


众所周知,我们在互联网上冲浪,一般都是使用的http协议(超文本传输协议),默认情况下数据是明文传送的,这些数据在传输过程中都可能会被捕获和窃听,因此是不安全的。https可以说是http协议的安全版,就是为了满足对安全性要求比较高的用户而设计的。如果您的邮件中有敏感数据,不希望被人窃听;如果您不希望被钓鱼网站盗用帐号信息,如果您希望您在使用邮箱的过程中更安全,那么我们推荐您使用https安全连接。

现在是我们要做一个网站 www2.88181.com要求通过https://www2.88181.com进行访问.

www2.88181.com 192.168.100.107

DNS 192.168.100.102

实验步骤

第一步、首先生成一对证书。你需要使用到openssl命令,所以请确认系统已经安装过此包

RHEL6中在/etc/pki/tls/certs 目录有个脚本可以帮助我们简化证书生成的过程,所以

我们首先切换到此目录

[root@rhel6u3-7 ~]# cd /etc/pki/tls/certs/ 

[root@rhel6u3-7 certs]# make server.key //生成私钥 

umask 77 ; \ 

    /usr/bin/openssl genrsa -aes128 2048> server.key 

Generating RSA private key, 2048 bit longmodulus 

..........+++ 

.....................................+++ 

e is 65537 (0x10001) 

Enter pass phrase: 

Verifying - Enter pass phrase: 

[root@rhel6u3-7 certs]# openssl rsa -in server.key-out server.key  //除去密码以便询问时不需要密码 

Enter pass phrase for server.key: 

writing RSA key 

[root@rhel6u3-7 certs]# make server.csr //生成证书颁发机构,用于颁发公钥 

umask 77 ; \ 

    /usr/bin/openssl req -utf8 -new -keyserver.key -out server.csr 

You are about to be asked to enter information thatwill be incorporated 

into your certificate request. 

What you are about to enter is what is called aDistinguished Name or a DN. 

There are quite a few fields but you can leave someblank 

For some fields there will be a default value, 

If you enter '.', the field will be leftblank. 

----- 

Country Name (2 letter code) [XX]:cn 

State or Province Name (full name) []:sh 

Locality Name (eg, city) [Default City]:sh 

Organization Name (eg, company) [Default CompanyLtd]:88181 

Organizational Unit Name (eg, section)[]:88181 

Common Name (eg, your name or your server'shostname) []:xiaonuo 

Email Address []:unix.root@hotmail.com 

Please enter the following 'extra' attributes 

to be sent with your certificate request 

A challenge password []:123.com 

An optional company name []:it 

[root@rhel6u3-7 certs]# openssl x509 -in server.csr-req -signkey server.key -days 365 -out server.crt //颁发公钥,不过由于我们并不是去CA证书中心申请的公钥,所以在使用的时候,客户端浏览器会跳出未受信任的警告。如果你在生产环境下,请去CA申请。 

Signature ok 

subject=/C=cn/ST=sh/L=sh/O=88181/OU=88181/CN=xiaonuo/emailAddress=unix.root@hotmail.com 

Getting Private key 

[root@rhel6u3-7 certs]#

第二步、编辑nginx主配置文件,添加ssl模块参数

[root@rhel6u3-7 certs]# vim/usr/local/nginx/conf/nginx.conf 

include/usr/local/nginx/server/www2.88181.com; //将虚拟主机单独设置,然后用include包含到主配置文件中,简化主配置文件的配置 

[root@rhel6u3-7 certs]# vim/usr/local/nginx/server/www2.88181.com  //注意以下配置可以在主配置文件中复制ssl模块信息 

server { 

        listen     443;  监听端口为443 

        server_name www2.88181.com; 

 

        ssl                 on;  //开启ssl 

        ssl_certificate     /etc/pki/tls/certs/server.crt;  //证书位置 

       ssl_certificate_key  /etc/pki/tls/certs/server.key;  //私钥位置 

       ssl_session_timeout  5m; 

        ssl_protocols SSLv2 SSLv3 TLSv1;  //指定密码为openssl支持的格式 

        ssl_ciphers HIGH:!aNULL:!MD5; //密码加密方式 

       ssl_prefer_server_ciphers  on; //依赖SSLv3和TLSv1协议的服务器密码将优先于客户端密码 

 

        location / { 

            root sites/www2;    //www2.88181.com根目录的相对位置 

           index  index.html index.htm; 

        } 

    }

第三步、创建网站的目录、主页、权限。

[root@rhel6u3-7 ~]# cd/usr/local/nginx/sites/  

[root@rhel6u3-7 sites]# mkdir www2 //创建网站根目录 

[root@rhel6u3-7 sites]# echo This ishttps://www2.88181.com >www2/index.html  //写个主测试页面 

[root@rhel6u3-7 server]# chown nginx./usr/local/nginx/server/ -R  //设置配置文件的属主和属组为nginx 

[root@rhel6u3-7 server]# chmod 600/usr/local/nginx/server/ -R  //设置配置文件的权限为600 

[root@rhel6u3-7 server]# chown nginx./usr/local/nginx/sites/www2 –R  //设置网站根目录的属主和属组为nginx 

[root@rhel6u3-7 server]# chmod 755/usr/local/nginx/sites/www2 –R //设置网站根目录权限为755,其他人可以读取网站信息。

第四步、在DNS区域中添加主机A记录

www2    A  192.168.100.107 

第五步、启动nginx服务器。

[root@rhel6u3-7 certs]# /etc/rc.d/init.d/nginxreload  //平滑重启nginx保证网站不被中断 

nginx: the configuration file/usr/local/nginx/conf/nginx.conf syntax is ok 

nginx: configuration file/usr/local/nginx/conf/nginx.conf test is successful 

Reloading nginx:                                        [  OK  ]

第六步、测试网站是否能够通过https访问

将PC机网卡的DNS更改为192.168.100.102


在IE浏览器中输入https://www2.88181.com进行测试。提示安全证书不受信任,是因为证书是本机模拟的。点击“仍然继续”即可。


 

来自 <http://www.linuxidc.com/Linux/2013-02/80069p4.htm>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值