Linux+Nginx+SSL架设安全网站
前言:
就像大家所知道的那样,我们的站点有时候需要需要用户登录,注册,或者填写咨询表单,
为了保护用户的隐私,我们需要将用户递交的信息进行加密,这样即使信息被截,
由于数据被加密,产生信息泄漏的可能性大大降低。
今天将讨论如何在CentOS上安装配置SSL网站服务器。
制作生成CSR(Certificate Signing Request)
进入/etc/nginx/conf.d
# cd /etc/nginx/conf.d
为了方便管理,将相关的密码,密钥文件放到同一文件夹下
# mkdir example.com # cd example.com
生成密钥文件
# openssl genrsa -des3 -out ./example.key 2048
这里采用2048bit位加密,根据服务商的要求不同,也有1024bit位的,视情况而定。
CSR文件生成
# openssl req -new -key ./example.key -out ./example.csr
指定CSR信息
Country Name (2 letter code) [AU]:CN State or Province Name (full name) [Some-State]:Shanghai Locality Name (eg, city) []:Pudongxinqu Organization Name (eg, company) [Internet Widgits Pty Ltd]:Qiai Co.,Ltd. Organizational Unit Name (eg, section) []:Development Common Name (eg, YOUR name) []:www.qiais.com Email Address []: A challenge password []: An optional company name []:
查看确认CSR信息
# cat ./ssl.csr/domainname.csr -----BEGIN CERTIFICATE REQUEST----- BAgTBXRva3lvMRAwDgYDVQQHEwdrYXdhZ29lMRcwFQYDVQQKEw5NeSBDb21wYW55 IEx0ZDEZMBcGA1UEAxMQcGVuZ3Vpbi1oZWFkLmNvbTxxxx0wNjA0MjIxMTM3Mjda Fw0xNjA0MTkxMTM3MjdaMGMxCzAJBgNVBAYTAmpwMQ4wDAYDVQQIEwV0b2t5bzEQ MA4GA1UEBxMHa2xxxxxxZTEXMBUGA1UEChMOTXkgQ29tcGFueSBMdGQxGTAXBgNV BAMTEHBlbmd1aW4taGVhZC5jb20wgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGB AMXDYSfWjWUTYxfmleIZE4uH/LK73mZcXg3EXxxxxxxxqrlgdx6P9Phq4q1koXl8 0Vxxxxxo3ZvvV08bhVhRwTRs6JRn4RktIPBWxpbGYD6ezfM+gIZ7tG+SQ4DNDXrG G5K5LkbL3hQc5EWyUAtKiRMnI1slZkviMPJhMi+WE6G/AgMBAAEwDQYJKoZIhvcN -----END CERTIFICATE REQUEST-----
一般来说,我们只需要将上述内容提供给SSL服务商,他们就会根据此密钥文件
生成密码文件。
2.申请SSL服务器认证
接下来,我们从SSL服务上哪里获得SSL认证用的证明文件,内容类似如下
-----BEGIN CERTIFICATE----- MIICNTCCAZ4CAQAwDQYJKoZIhvcNAQEEBQAwYzELMAkGA1UEBhMCanAxDjAMBgNV BAgTBXRvaxxxMRAwDgYDVQQHEwdrYXdhZ29lMRcwFQYDVQQKEw5NeSBDb21wYW55 IEx0ZDEZMBcGA1UEAxxxxxxxZ3Vpbi1oZWFkLmNvbTAeFw0wNjA0MjIxMTM3Mjda Fw0xNjA0MTkxMTM3MjdaMGMxCzAJBgNVBAYTAmpwMQ4wDAYDVQQIEwV0b2t5bzEQ MA4GA1UEBxMHa2F3YWdvZTEXMBUGA1UEChMOTXkgQ29tcGFueSBMdGQxGTAXBgNV BAMTEHBlbxx1aW4taGVhZC5jb20wgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGB AMXDYSfWjWUTYxfmleIZE4uH/LK73mZcXg3EXDtob79bqrlgdx6P9Phq4q1koXl8 0VK0gF0o3ZvvV08bhVhRwxxxxxxx4RktIPBWxpbGYD6ezfM+gIZ7tG+SQ4DNDXrG G5K5LkbL3hQc5EWyUAtKiRMnI1slZkviMPJhMi+WE6G/AgMBAAEwDQYJKoZIhvcN AQEEBQADgYEAoRlykFxCHssjoe/LNMMxxxxxxxxxxdK5M3IGVgDDa9mzs19teagW 7OoRZDxxxxxYAa9fwLjy5PPKRHpr1QGjp7/u0djuQ+ZUiGPrUwxlTOtiWBUwjFQk HY5eoHdGKopo+ezn7NnrelcvYOaXoQvJtu6Jh+zPTnLEHhJOTi4iLs8= -----END CERTIFICATE-----
我们将其保存在/etc/nginx/conf.d/example.com/example.crt文件中。
如果从服务商哪里还有中间证明文件的话,我们将其保存在/etc/nginx/conf.d/example.com/ca.crt文件中
中间证明文件类似于以上文件:
3.Nginx 上配置SSL
如果有中间认证文件的话,我们需要将中间认证文件内容拷贝到证明文件
# cat example.crt ca.crt > cert.crt
设置/etc/nginx/conf.d/ssl.conf
# vi /etc/nginx/conf.d/ssl.conf
# # HTTPS server configuration # server { listen 443; server_name shimbun4946nakano.com; ssl on; # ssl_certificate cert.pem; ssl_certificate /etc/nginx/conf.d/example.com/cert.crt; # ssl_certificate_key cert.key; ssl_certificate_key /etc/nginx/conf.d/example.com/example.key; ssl_session_timeout 5m; ssl_protocols SSLv2 SSLv3 TLSv1; ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP; ssl_prefer_server_ciphers on; location / { root /var/www/html; index index.php index.html index.htm; } error_page 404 /404.html; location = /404.html { root /var/www/html; } # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /var/www/html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { root /var/www/html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
4. 解除服务器重启密码
以上设置SSL的key时设置了密码,nginx重新启动时需要输入密码,这有时候不方便,特别是在我们重新启动服务器(硬件),这个时候我们没法输入key的密码,而导致我们的nginx不能启动。
# cp example.key example.key.bak # openssl rsa -in example.key -out example.key
5. 访问https://example.com
没有错误的话,在URL栏会看到像锁一样的图标,那就表示SSL服务器安装成功。