nginx配置ssl

迈不过友情╰ 2022-08-06 10:05 287阅读 0赞

感谢大牛的技术分享,本文参照:http://blog.csdn.net/kunoy/article/details/8239653,进行,并修正为自己的配置,本文为双向验证的配置。
配置环境
系统:centos6.5
nginx:1.6.2(安装通过yum安装,添加源,这个是stable`稳定版本`,http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm)
开始配置
nginx配置文件位置:

/etc/nginx/conf.d/example_ssl.conf
修改/etc/nginx/conf.d/example_ssl.conf
注:/etc/nginx/conf.d/example_ssl.conf是被/etc/nginx/nginx.conf包含到自身的一个文件。

改为内容如下

  1. #HTTP-SERVER
  2. server {
  3. listen 443;
  4. server_name localhost;
  5. ssi on;
  6. ssi_silent_errors on;
  7. ssi_types text/shtml;
  8. ssl on;
  9. ssl_certificate /etc/nginx/ca/server/server.crt;
  10. ssl_certificate_key /etc/nginx/ca/server/server.key;
  11. ssl_client_certificate /etc/nginx/ca/private/ca.crt;
  12. ssl_session_timeout 5m;
  13. ssl_verify_client on;
  14. ssl_protocols SSLv2 SSLv3 TLSv1;
  15. ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
  16. ssl_prefer_server_ciphers on;
  17. location / {
  18. root /usr/share/nginx/html;
  19. index index.html index.htm;
  20. }
  21. }

以上就是简单的配置了,下面就是签发证书的一些工作了

签发证书

创建配置文件/etc/nginx/ca/conf/openssl.conf

  1. [ ca ]
  2. default_ca = foo # The default ca section
  3. [ foo ]
  4. dir = /etc/nginx/ca # top dir
  5. database = /etc/nginx/ca/index.txt # index file.
  6. new_certs_dir = /etc/nginx/ca/newcerts # new certs dir
  7. certificate = /etc/nginx/ca/private/ca.crt # The CA cert
  8. serial = /etc/nginx/ca/serial # serial no file
  9. private_key = /etc/nginx/ca/private/ca.key # CA private key
  10. RANDFILE = /etc/nginx/ca/private/.rand # random number file
  11. default_days = 365 # how long to certify for
  12. default_crl_days= 30 # how long before next CRL
  13. default_md = md5 # message digest method to use
  14. unique_subject = no # Set to 'no' to allow creation of
  15. # several ctificates with same subject.
  16. policy = policy_any # default policy
  17. [ policy_any ]
  18. countryName = match
  19. stateOrProvinceName = match
  20. organizationName = match
  21. organizationalUnitName = match
  22. localityName = match
  23. commonName = match
  24. emailAddress = match

在/etc/niginx/ca 下 创建文件new_ca.sh ,执行生成根证书

  1. #!/bin/sh
  2. # Generate the key.
  3. openssl genrsa -out private/ca.key
  4. # Generate a certificate request.
  5. openssl req -new -key private/ca.key -out private/ca.csr
  6. # Self signing key is bad... this could work with a third party signed key... registeryfly has them on for $16 but I'm too cheap lazy to get one on a lark.
  7. # I'm also not 100% sure if any old certificate will work or if you have to buy a special one that you can sign with. I could investigate further but since this
  8. # service will never see the light of an unencrypted Internet see the cheap and lazy remark.
  9. # So self sign our root key.
  10. openssl x509 -req -days 365 -in private/ca.csr -signkey private/ca.key -out private/ca.crt
  11. # Setup the first serial number for our keys... can be any 4 digit hex string... not sure if there are broader bounds but everything I've seen uses 4 digits.
  12. echo FACE > serial
  13. # Create the CA's key database.
  14. touch index.txt
  15. # Create a Certificate Revocation list for removing 'user certificates.'
  16. openssl ca -gencrl -out /etc/nginx/ca/private/ca.crl -crldays 7 -config "/etc/nginx/ca/conf/openssl.conf"

在/etc/niginx/ca 下 创建文件new_server.sh ,执行生成服务器证书

  1. # Create us a key. Don't bother putting a password on it since you will need it to start apache. If you have a better work around I'd love to hear it.
  2. openssl genrsa -out server/server.key
  3. # Take our key and create a Certificate Signing Request for it.
  4. openssl req -new -key server/server.key -out server/server.csr
  5. # Sign this bastard key with our bastard CA key.
  6. openssl ca -in server/server.csr -cert private/ca.crt -keyfile private/ca.key -out server/server.crt -config "/etc/nginx/ca/conf/openssl.conf"

在/etc/niginx/ca 下 创建文件new_user.sh ,执行生成客户端证书

  1. #!/bin/sh
  2. # The base of where our SSL stuff lives.
  3. base="/etc/nginx/ca"
  4. # Were we would like to store keys... in this case we take the username given to us and store everything there.
  5. mkdir -p $base/users/
  6. # Let's create us a key for this user... yeah not sure why people want to use DES3 but at least let's make us a nice big key.
  7. openssl genrsa -des3 -out $base/users/client.key 1024
  8. # Create a Certificate Signing Request for said key.
  9. openssl req -new -key $base/users/client.key -out $base/users/client.csr
  10. # Sign the key with our CA's key and cert and create the user's certificate out of it.
  11. openssl ca -in $base/users/client.csr -cert $base/private/ca.crt -keyfile $base/private/ca.key -out $base/users/client.crt -config "/etc/nginx/ca/conf/openssl.conf"
  12. # This is the tricky bit... convert the certificate into a form that most browsers will understand PKCS12 to be specific.
  13. # The export password is the password used for the browser to extract the bits it needs and insert the key into the user's keychain.
  14. # Take the same precaution with the export password that would take with any other password based authentication scheme.
  15. openssl pkcs12 -export -clcerts -in $base/users/client.crt -inkey $base/users/client.key -out $base/users/client.p12

以上工作做完之后把/etc.nginx/ca/user/client.p12文件导入到自己的浏览器就可以了。

发表评论

表情:
评论列表 (有 0 条评论,287人围观)

还没有评论,来说两句吧...

相关阅读

    相关 nginx 配置 ssl

    1.1 Nginx如果未开启SSL模块,配置Https时提示错误 原因也很简单,nginx缺少http\_ssl\_module模块,编译安装的时候带上--wit

    相关 nginx 配置ssl

    前言 服务器安全一直是近些年来热门的话题,一个正常的请求发送到服务端之后,服务端将响应结果返回客户端,可以理解此次交互建立了一个请求响应的通道; 如果这个通道不够安全,

    相关 nginx配置ssl

    先生成网关证书 ,仿照CA模式  1 生成私钥,需要密码的  openssl genrsa -des3 -out server.key 1024  2 生成证书请求

    相关 Nginx 配置 ssl 证书

    > 在部署线下测试环境时浏览器会自动将http替换成https,又因为测试环境没有证书经常会导致跳转错误,所以自己配置一个ssl证书就可以避免这个问题。 > 下面记录我在M