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;
        }
        }
相關文章
相關標籤/搜索