nginx 经过openssl配置https公网证书(非443端口下http与https共存)

更详细的参数设定请参考:http://www.javashuo.com/article/p-pqdfumdy-cx.htmlnginx

步骤:segmentfault

1.生成一个权威的ssl证书对(若是本身颁发的话,那么https是不被浏览器承认的,就是https上面会有一个大红叉)api

推荐一个免费的网站:https://www.startssl.com/(注册邮箱:公司邮箱浏览器

startssl的操做教程看这个:http://www.freehao123.com/startssl-ssl/(大体就是先注册,而后验证域名,最后申请证书)bash

2.根据ssl.key和ssl.crt部署nginxsession

首先nginx须要支持ssl_module,而后修改nginx.conf以下ide

server {
        listen       443;
        server_name  wx.ltanx.cn;
        ssl                  on;
        ssl_certificate      ../key/1_wx.ltanx.cn_bundle.crt;
        ssl_certificate_key  ../key/ltanx_nopass.key;#这个是有密码的,重启或者reload nginx的时候会提示密码
        ssl_session_timeout  30m;#默认时间只有5分钟,若是5分钟就挂掉未免过短了
        location /test/ {#若是要反向代理也支持,那就在这里添加,千万别在80端口下没用的!
                proxy_pass http://192.168.180.198/zabbix/;
                proxy_redirect off;
                #proxy_set_header        Host $host;
               #proxy_set_header        X-Real-IP $remote_addr;
               #proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }

在相应的位置放置crt文件和key文件,注意到这边的key是nopassword的,就是重启nginx的时候,不须要输入密码。网站

ltanx_nopass.key是根据ltanx.key生成的,生成命令以下:spa

openssl rsa -in ltanx.key -out ltanx_nopass.key
而后输入密码就行

到这里重启nginx后就能够访问https://wx.ltanx.cn代理

3.80端口重定向到443端口

server {
    listen 80;
    server_name wx.ltanx.cn;
    rewrite ^(.*) https://$server_name$1 permanent;
 ### 使用return的效率会更高 
 #  return 301 https://$server_name$request_uri;
}

 4.非443端口http与https共存

    若是非443的状况下仍是按上面的作法强制转换是行不通的,只要一个server下定义一个442并配置ssl on,但若是用户访问http链接的话会提示“the plain http request was sent to https”意思就是http请求转到https了,其实nginx官网认为这个是正常现象定义了个497的状态,只要添加error_page 497 https://...就OK了

server {
        listen 442;
        server_name wx.ltanx.cn;
        error_page 497  https://$server_name:442$request_uri; #正常错误反馈转换到https
        ssl on;
        ssl_certificate ../key/1_wx.ltanx.cn_cert.crt;
        ssl_certificate_key ../key/2_wx.ltanx.cn.key;
        ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
        ssl_prefer_server_ciphers on;
        location /test/ {
                proxy_pass http://122.xx.8.xx/hzctopenapi/;
                proxy_redirect off;
        }
        }
相关文章
相关标签/搜索