mangreen / Some-Note

Development Memo
1 stars 0 forks source link

nginx set SSL (Secure Sockets Layer) #6

Open mangreen opened 9 years ago

mangreen commented 9 years ago

你真的了解如何將Nginx 配置為Web服務器嗎

https://lufficc.com/blog/configure-nginx-as-a-web-server

檢查設定錯誤

$ sudo nginx -t

Server自己生的認證key

SSLCertificateFile /etc/ssl/certs/ssl-cert-snakeoil.pem SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key

http://www.codeceo.com/article/nginx-ssl-nodejs.html

https://www.digitalocean.com/community/tutorials/how-to-create-a-ssl-certificate-on-nginx-for-ubuntu-12-04

http://www.adj.idv.tw/html/51/t-135151.html

https://www.sslshopper.com/article-most-common-openssl-commands.html

1.確認網域資訊

在購買 SSL 之前,必須先擁有一個認證的網域。我們可以使用 whois 來查詢網域的申請資料。whois 查詢出來的資料,請確定 “Administrative Contact Email” 的電子信箱是正確的。因為 SSL 會以此信箱為對象寄發。

2. 產生 Private Key / CSR (Certificate signing request)

$ openssl req -out CSR.csr -new -newkey rsa:2048 -nodes -keyout privateKey.key

Country Name (2 letter code) [AU]: TW State or Province Name (full name) [Some-State]: Taiwan Locality Name (eg, city) []: Taichung Organization Name (eg, company) [Internet Widgits Pty Ltd]: ADJ Organizational Unit Name (eg, section) []: IT Department Common Name (eg, YOUR name) []: (這裡一定要輸入正確的網域) Email Address []:

Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []: (可不填) An optional company name []: (可不填)

3. Godaddy SSL

Godaddy SSL 的大類分為 “Standard SSL” 及 “Deluxe SSL",進一步又可再細分為 "Single Domain"、"Multiple Domain” 及 “Unlimited Sub Domains"。你可以在網站上試算一下何方案對你比較有利。我自己用的是 Single Domain…

購買後約等幾分鐘,你就會收到 Godaddy 寄發的訊息。此時登入 Godaddy 後台,你可以在 "SSL Certificates” 看到你購買的紀錄。接著從 “Pending Request” 中設定你的 SSL。若你是購買 “Multiple Domain",則可以設定多個。

切記,若你的伺服器不是放在 Godaddy 自家的服務,設定的時候寄得要選 "Third Party, or Dedicated Server or Virtual Dedicated Server”。

當 Godaddy 要求你提供 CSR (Certificate signing request) 時,請將先前產生的 server.csr 的內容全部貼上。

設定完成後,Godaddy 會寄發 whois 提供的電子信箱,裡面附著認證碼。唯有通過認證後,SSL 才會由 “Pending Request” 變更為 “Certificates"。

4. 下載 SSL

從 Godaddy 下載 SSL 時,會包含兩個檔案: your-domain.crt gd_bundle.crt 記得要將 "gd_bundle.crt” 的內容全部貼到 .crt,否則有些瀏覽器會不支援你的 SSL。

5. 伺服器配置 SSL

準備好 server.key 以及 server.crt (包括 gd_bundle.crt 的內容)。

#80
server{
        listen 80;
        server_name www.example.com;

        client_max_body_size 100M;
        location /{
                proxy_pass http://localhost:3001;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                #proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
        }
}

#or
server {
        listen 80;
        server_name www.example.com;
        return  301 https://$server_name$request_uri;
}

#443 ssl
server {
       listen       443 ssl;
       server_name  www.example.com;

       ssl_certificate      /home/ubuntu/sslcert/server.crt;
       ssl_certificate_key  /home/ubuntu/sslcert/server.key;

       ssl on;
       ssl_session_timeout  5m;
       ssl_protocols  TLSv1 TLSv1.1 TLSv1.2;

       ssl_ciphers  HIGH:!aNULL:!MD5;
       ssl_prefer_server_ciphers  on;

       location / {
                proxy_pass http://localhost:3000;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                #proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
                proxy_redirect http://localhost:3000 http://www.example.com;
       }
}