<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>key &#8211; 記下來</title>
	<atom:link href="https://noter.tw/tag/key/feed/" rel="self" type="application/rss+xml" />
	<link>https://noter.tw</link>
	<description>一路上踩到的坑、遇到的問題，一點一滴記下來，希望能幫助到需要的人~</description>
	<lastBuildDate>Wed, 11 Aug 2021 12:17:21 +0000</lastBuildDate>
	<language>zh-TW</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.6.3</generator>

<image>
	<url>https://noter.tw/wp-content/uploads/cropped-old-1130742_1920-1-32x32.jpg</url>
	<title>key &#8211; 記下來</title>
	<link>https://noter.tw</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>nginx + SSL Certificate &#8211; 讓 http 變身成為 https</title>
		<link>https://noter.tw/3704/nginx-ssl-certificate-%e8%ae%93-http-%e8%ae%8a%e8%ba%ab%e6%88%90%e7%82%ba-https/</link>
					<comments>https://noter.tw/3704/nginx-ssl-certificate-%e8%ae%93-http-%e8%ae%8a%e8%ba%ab%e6%88%90%e7%82%ba-https/#comments</comments>
		
		<dc:creator><![CDATA[黃小蛙]]></dc:creator>
		<pubDate>Fri, 08 Jul 2016 01:56:44 +0000</pubDate>
				<category><![CDATA[Web Server]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[crt]]></category>
		<category><![CDATA[key]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[https]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[ssl]]></category>
		<category><![CDATA[SSL For Free]]></category>
		<category><![CDATA[CA]]></category>
		<guid isPermaLink="false">https://wazai.net/?p=3704</guid>

					<description><![CDATA[<p>接續&#160;SSL For Free – 免費又有綠色鎖頭的 SSL Certificates 申請好 SSL Certificate 之後，這篇記錄一下怎麼樣把 SSL Certificate &#46;&#46;&#46;</p>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/3704/nginx-ssl-certificate-%e8%ae%93-http-%e8%ae%8a%e8%ba%ab%e6%88%90%e7%82%ba-https/" data-wpel-link="internal">nginx + SSL Certificate &#8211; 讓 http 變身成為 https</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<p>接續&nbsp;<a rel="noreferrer noopener" aria-label=" (在新分頁中開啟)" href="https://noter.tw/3695/ssl-for-free-%e5%85%8d%e8%b2%bb%e5%8f%88%e6%9c%89%e7%b6%a0%e8%89%b2%e9%8e%96%e9%a0%ad%e7%9a%84-ssl-certificates/" target="_blank" data-wpel-link="internal">SSL For Free – 免費又有綠色鎖頭的 SSL Certificates</a> 申請好 SSL Certificate 之後，這篇記錄一下怎麼樣把 SSL Certificate 加入 nginx 中，讓 http 可以進化成 https。 </p>



<span id="more-3704"></span>



<div class="wp-block-image"><figure class="aligncenter"><a href="https://sshfs.crazyall.net/wazai/2016/07/computer-therms-2-1241965-640x480.jpg" target="_blank" rel="noopener nofollow external noreferrer" data-wpel-link="external"><img decoding="async" src="https://sshfs.crazyall.net/wazai/2016/07/computer-therms-2-1241965-640x480-500x375.jpg" alt="https" class="wp-image-3706" title="nginx + SSL Certificate - 讓 http 變身成為 https"></a><figcaption> 圖片來源：http://tw.freeimages.com/photo/computer-therms-2-1241965 </figcaption></figure></div>



<h2 class="para wp-block-heading">囉哩巴唆</h2>



<p>小蛙剛接觸 nginx 的時候，真是吃盡了苦頭，雖然說功能非常強大，但是有些功能 google 了好久才勉強達到，這裡提供的一些作法也許不是最好，小蛙相信有更優雅的作法，不過至少能達成小蛙的需求，正所謂能解決問題的就是好方法！先達到功能，之後看到更好的作法再慢慢修正。</p>



<h2 class="para wp-block-heading">準備所需的 crt 及 key</h2>



<p>把從&nbsp;<a rel="noreferrer noopener" aria-label=" (在新分頁中開啟)" href="https://noter.tw/3695/ssl-for-free-%e5%85%8d%e8%b2%bb%e5%8f%88%e6%9c%89%e7%b6%a0%e8%89%b2%e9%8e%96%e9%a0%ad%e7%9a%84-ssl-certificates/" target="_blank" data-wpel-link="internal">SSL For Free – 免費又有綠色鎖頭的 SSL Certificates</a>&nbsp;下載回來的 3 個檔案，分別是 ca_bundle.crt, certificate.crt, private.key，接下來我們要將 ca_bundle.crt 與 certificate.crt 合併，如果是用 windows 的話，用筆記本新增一個檔案叫做 bundle.crt，然後分別將 certificate.crt 跟 ca_bundle.crt 這兩個檔案的內容貼進 bundle.crt，要注意有沒有不小心複製到換行，或是空白字元，大概會變成下面這樣</p>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code">-----BEGIN CERTIFICATE-----
...
certificate.crt 的內容
...
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
...
ca_bundle.crt 的內容
...
-----END CERTIFICATE-----</pre>



<p>如果像小蛙一樣使用 Linux 的話，可直接輸入</p>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code">&gt;cat certificate.crt ca_bundle.crt &gt;&gt; bundle.crt
// 接著用編輯器把以下的魂結斷開
-----END CERTIFICATE----------BEGIN CERTIFICATE-----</pre>



<p>魂結沒有斷開的話，會得到這個錯誤 </p>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code">[emerg] 20131#0: PEM_read_bio_X509_AUX("/etc/nginx/ssl/crazyall.net.crt") failed (SSL: error:0906D066:PEM routines:PEM_read_bio:bad end line)</pre>



<p>以小蛙的 nginx 為例，為了不要放檔案太亂，小蛙另外建了 ssl 資料夾，該目錄為 /etc/nginx/ssl/，並且將 bundle.crt 更名為 crazyall.net.crt，private.key 也更名為 crazyall.net.key，如此一來如果有多個網域搭配多個 SSL 會比較方便管理，完成後將 crazyall.net.crt 及 crazyall.net.key 複製到 /etc/nginx/ssl/ 資料夾中。</p>



<h2 class="para wp-block-heading">將 SSL 加入 nginx</h2>



<p>這裡記錄給自己看，預設的 nginx 檔案會包含一小段搭配載入 php5-fpm 的部份，例如：</p>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code">location ~ \.php$ {
		fastcgi_split_path_info ^(.+\.php)(/.+)$;
		fastcgi_pass 127.0.0.1:9000;
		fastcgi_read_timeout 120;
		fastcgi_index index.php;
		include fastcgi_params;
}</pre>



<p>如果只有一個網域或一個站，直接這樣就好，但是像小蛙有 crazyall.net 以及 wazai.net 兩個，這一段就會重複，這時候可以把上面的內容獨立成 php.conf 並放在 /etc/nginx/ 資料夾下，接著從 /etc/nginx/sites-avaibale/wazai.net 設定檔中 include php.conf; 就可以了。</p>



<p>wazai.net 的設定部份如下 </p>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code">server {
		listen 443 ssl default_server;	 	 
		listen [::]:443 ssl default_server;
		server_name wazai.net;
		ssl_certificate /etc/nginx/ssl/wazai.net.crt;	 	 
		ssl_certificate_key /etc/nginx/ssl/wazai.net.key;	 	 
		... 以下省略 ...	 	 
}</pre>



<p>這裡如果把 crazyall.net 使用一樣的設定就會得到下列錯誤</p>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code">[emerg] 20005#0: a duplicate default server for 0.0.0.0:443 in /etc/nginx/sites-enabled/wazai.net:58</pre>



<p>因此如果同一台主機上有不只一個網域要使用 SSL，就要改成下面這樣，加了 default_server 就直接錯給你看</p>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code">server {
		listen 443 ssl;
		server_name crazyall.net;
		ssl_certificate /etc/nginx/ssl/crazyall.net.crt;
		ssl_certificate_key /etc/nginx/ssl/crazyall.net.key;
		root /your_www_document;
		index index.php index.html index.htm;
		... 以下省略 ...
}</pre>



<p>經過這些修改 service nginx reload 就可以正常執行囉！試試看 https://your_domain/，如果是用 WordPress 架站的話，還會有很多很多很多問題需要克服，使用 Chrome 的 consloe 可以看到滿山滿谷的 </p>



<div class="wp-block-image"><figure class="aligncenter"><a href="https://sshfs.crazyall.net/wazai/2016/07/chrome_console_ng.png" target="_blank" rel="noopener nofollow external noreferrer" data-wpel-link="external"><img decoding="async" src="https://sshfs.crazyall.net/wazai/2016/07/chrome_console_ng-500x295.png" alt="滿山滿谷的 MixedContent" class="wp-image-3719" title="nginx + SSL Certificate - 讓 http 變身成為 https"></a><figcaption> 滿山滿谷的 MixedContent</figcaption></figure></div>



<p>而且網站打開來之後慘不忍睹， 這也是小蛙為什麼當初卻步的原因，下一篇根據 google 來的資料一一修正這些問題。</p>



<div class="wp-block-image"><figure class="aligncenter is-resized"><a href="https://sshfs.crazyall.net/wazai/2016/07/crazyall.png" target="_blank" rel="noopener nofollow external noreferrer" data-wpel-link="external"><img fetchpriority="high" decoding="async" src="https://sshfs.crazyall.net/wazai/2016/07/crazyall.png" alt="http://crazyall.net" class="wp-image-3720" width="244" height="315" title="nginx + SSL Certificate - 讓 http 變身成為 https"></a><figcaption>正常的都瘋：http://crazyall.net</figcaption></figure></div>



<div class="wp-block-image"><figure class="aligncenter is-resized"><a href="https://sshfs.crazyall.net/wazai/2016/07/crazyallng.png" target="_blank" rel="noopener nofollow external noreferrer" data-wpel-link="external"><img decoding="async" src="https://sshfs.crazyall.net/wazai/2016/07/crazyallng.png" alt="慘不忍睹的都瘋：https://crazyall.net" class="wp-image-3721" width="244" height="314" title="nginx + SSL Certificate - 讓 http 變身成為 https"></a><figcaption>慘的都瘋：https://crazyall.net</figcaption></figure></div>



<h2 class="para wp-block-heading">參考資料</h2>



<ul class="my-li bg-darkblue wp-block-list"><li><a href="https://www.digicert.com/ssl-certificate-installation-nginx.htm" target="_blank" rel="noopener nofollow external noreferrer" data-wpel-link="external" class="wpel-icon-right">Nginx SSL Certificate Installation<span class="wpel-icon wpel-image wpel-icon-6"></span></a></li></ul>



<p><strong>網站架設系列文章：</strong></p>



<ul class="my-li bg-darkblue wp-block-list"><li><a href="https://noter.tw/10151/%e7%9b%a4%e9%bb%9e%e9%82%a3%e4%ba%9b%e6%9b%be%e7%b6%93%e9%99%aa%e4%bc%b4%e9%81%8e%e5%b0%8f%e8%9b%99%e7%9a%84-vps-%e8%99%9b%e6%93%ac%e4%b8%bb%e6%a9%9f/" data-wpel-link="internal">盤點那些曾經陪伴過小蛙的 VPS 虛擬主機</a></li><li><a href="https://noter.tw/176/%e5%a6%82%e4%bd%95%e6%8c%91%e9%81%b8-vps-%e6%9c%80%e5%88%92%e7%ae%97%e7%9a%84%e8%b2%b7%e6%b3%95/" data-wpel-link="internal">如何挑選 VPS + 最划算的買法</a></li><li><a href="https://noter.tw/406/%E6%89%8B%E6%8A%8A%E6%89%8B%E6%95%99%E4%BD%A0%E5%A6%82%E4%BD%95%E8%B3%BC%E8%B2%B7-vps/" data-wpel-link="internal">手把手教你如何購買 VPS</a></li><li><a href="https://noter.tw/4463/第一次買-kvm-虛擬主機/" data-wpel-link="internal">第一次買 KVM 虛擬主機</a></li><li><a href="https://noter.tw/4242/%e8%a1%80%e7%9a%84%e6%95%99%e8%a8%93-hiformance-%e8%b7%91%e8%b7%af%e4%ba%86/" data-wpel-link="internal">血的教訓 &#8211; HiFormance 跑路了 &#8230;</a></li><li><a href="https://noter.tw/196/%e7%82%ba%e8%87%aa%e5%b7%b1%e7%94%b3%e8%ab%8b%e4%b8%80%e5%80%8b%e5%9f%9f%e5%90%8d-hinet-%e7%82%ba%e4%be%8b/" data-wpel-link="internal">為自己申請一個域名 – HiNet 為例</a></li><li><a href="https://noter.tw/117/cloudflare-%E8%B6%85%E5%BC%B7%E5%A4%A7%E7%9A%84%E5%85%8D%E8%B2%BB-cdn/" data-wpel-link="internal">Cloudflare – 超強大的免費 CDN (?)</a></li><li><a href="https://noter.tw/195/%e4%bd%bf%e7%94%a8-cloudflare-%e4%bb%a3%e7%ae%a1%e4%bd%a0%e7%9a%84%e5%9f%9f%e5%90%8d/" data-wpel-link="internal">使用 Cloudflare 代管你的域名</a></li><li><a href="https://noter.tw/396/ubuntu-16-04-設定環境語言編碼，讓中文可以正常顯示/" data-wpel-link="internal">Ubuntu 16.04 設定環境語言編碼，讓中文可以正常顯示</a></li><li><a href="https://noter.tw/255/ubuntu-16-04-%e5%ae%89%e8%a3%9d-fail2ban/" data-wpel-link="internal">Ubuntu 16.04 安裝 fail2ban</a></li><li><a href="https://noter.tw/436/%E4%B8%80%E5%80%8B%E8%85%B3%E6%9C%AC%E6%AF%8F%E5%A4%A9%E8%87%AA%E5%8B%95%E5%82%99%E4%BB%BD/" data-wpel-link="internal">一個腳本每天自動備份</a></li><li><a href="https://noter.tw/32/linux-tar-%E5%AE%8C%E6%95%B4%E5%82%99%E4%BB%BD%E3%80%81%E5%A2%9E%E9%87%8F%E5%82%99%E4%BB%BD%E5%8F%8A%E5%B7%AE%E7%95%B0%E5%82%99%E4%BB%BD%E6%AF%94%E8%BC%83/" data-wpel-link="internal">Linux tar 完整備份、增量備份及差異備份比較</a></li><li><a href="https://noter.tw/3704/nginx-ssl-certificate-%E8%AE%93-http-%E8%AE%8A%E8%BA%AB%E6%88%90%E7%82%BA-https/" data-wpel-link="internal">nginx + SSL Certificate – 讓 http 變身成為 https</a></li><li><a href="https://noter.tw/3695/ssl-for-free-%E5%85%8D%E8%B2%BB%E5%8F%88%E6%9C%89%E7%B6%A0%E8%89%B2%E9%8E%96%E9%A0%AD%E7%9A%84-ssl-certificates/" data-wpel-link="internal">SSL For Free – 免費又有綠色鎖頭的 SSL Certificates</a></li><li><a href="https://noter.tw/8596/certbot-error/" data-wpel-link="internal">Certbot 申請 SSL certificate 錯誤問題</a></li><li><a href="https://noter.tw/9107/certbot-error-acmev1-is-deprecated/" data-wpel-link="internal">Certbot 錯誤：ACMEv1 is deprecated and you can …</a></li><li><a href="https://noter.tw/5967/ubuntu-18-04-%e5%ae%89%e8%a3%9d-awstats-%e4%be%86%e7%b5%b1%e8%a8%88%e7%b6%b2%e7%ab%99%e6%b5%81%e9%87%8f/" data-wpel-link="internal">Ubuntu 18.04 安裝 AWStats 來統計網站流量</a></li><li><a href="https://noter.tw/9287/pchome-%e8%b2%b7%e7%b6%b2%e5%9d%80%e7%ba%8c%e7%b4%84%e6%b5%81%e7%a8%8b/" data-wpel-link="internal">PCHome 買網址續約流程</a></li></ul>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/3704/nginx-ssl-certificate-%e8%ae%93-http-%e8%ae%8a%e8%ba%ab%e6%88%90%e7%82%ba-https/" data-wpel-link="internal">nginx + SSL Certificate &#8211; 讓 http 變身成為 https</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://noter.tw/3704/nginx-ssl-certificate-%e8%ae%93-http-%e8%ae%8a%e8%ba%ab%e6%88%90%e7%82%ba-https/feed/</wfw:commentRss>
			<slash:comments>4</slash:comments>
		
		
			</item>
	</channel>
</rss>
