執行個體1:為已安裝nginx動态添加子產品
以安裝rtmp媒流子產品為例:
1)下載下傳第三方子產品到
[root@LNMP nginx-1.8.1]# git clone https://github.com/arut/nginx-rtmp-module.git
2)檢視nginx編譯安裝時安裝的子產品
<code>[root@LNMP nginx-1.8.1]</code><code># nginx -V</code>
<code>nginx version: nginx</code><code>/1</code><code>.8.1</code>
<code>built by gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC) </code>
<code>built with OpenSSL 1.0.1e-fips 11 Feb 2013</code>
<code>TLS SNI support enabled</code>
<code>configure arguments: --user=nginx --group=nginx --prefix=</code><code>/usr/local/nginx</code> <code>--with-http_ssl_module --with-http_stub_status_module --with-http_ssl_module</code>
3)cd到源碼目錄添加子產品重新配置編譯
<code>[root@LNMP nginx]</code><code># cd /root/tools/nginx-1.8.1</code>
<code>[root@LNMP nginx-1.8.1]</code><code># ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-http_ssl_module --add-module=/root/tools/nginx-1.8.1/nginx-rtmp-module</code>
<code>[root@LNMP nginx-1.8.1]</code><code># make</code>
#此處隻進行編譯不進行安裝,如安裝的話會覆寫源檔案。
4)在編譯完成後,會在目前目錄下生成一個objs檔案夾,将nginx二進制檔案拷貝到源安裝目錄下,注意備份源檔案,然後檢視編譯後的子產品。
<code>[root@LNMP nginx-1.8.1]</code><code># mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.20170825</code>
<code>[root@LNMP nginx-1.8.1]</code><code># cp objs/nginx /usr/local/nginx/sbin/nginx</code>
<code>configure arguments: --user=nginx --group=nginx --prefix=</code><code>/usr/local/nginx</code> <code>--with-http_ssl_module --with-http_stub_status_module --with-http_ssl_module --add-module=</code><code>/root/tools/nginx-1</code><code>.8.1</code><code>/nginx-rtmp-module</code>
<code>[root@LNMP nginx-1.8.1]</code><code># nginx -s reload</code>
執行個體2:nginx使用ssl子產品配置https支援
1、生成證書(注意此證書為自己頒發的在公網上不受信任)
1)生成一個rsa密鑰:
<code>[root@LNMP ssl]</code><code># openssl genrsa -des3 -out test.key 1024</code>
<code>Generating RSA private key, 1024 bit long modulus</code>
<code>..............................++++++</code>
<code>...........................++++++</code>
<code>e is 65537 (0x10001)</code>
<code>Enter pass phrase </code><code>for</code> <code>test</code><code>.key: </code><code>#輸入密碼,需要複雜性要求</code>
<code>Verifying - Enter pass phrase </code><code>for</code> <code>test</code><code>.key: </code><code>#重複密碼</code>
2)拷貝剛才的密碼檔案,生成一個不需要密碼的密鑰檔案:
<code>[root@LNMP ssl]</code><code># openssl rsa -in test.key -out test_nopass.key</code>
<code>Enter pass phrase </code><code>for</code> <code>test</code><code>.key: </code><code>#輸入以上建立時輸入的密碼</code>
<code>writing RSA key</code>
3)生成一個證書請求檔案
<code>[root@LNMP ssl]</code><code># openssl req -new -key test.key -out test.csr</code>
<code>You are about to be asked to enter information that will be incorporated</code>
<code>into your certificate request.</code>
<code>What you are about to enter is what is called a Distinguished Name or a DN.</code>
<code>There are quite a few fields but you can leave some blank</code>
<code>For some fields there will be a default value,</code>
<code>If you enter </code><code>'.'</code><code>, the field will be left blank.</code>
<code>-----</code>
<code>Country Name (2 letter code) [XX]:cn </code><code>#國家</code>
<code>State or Province Name (full name) []:shanghai </code><code>#省份</code>
<code>Locality Name (eg, city) [Default City]:shanghai </code><code>#城市</code>
<code>Organization Name (eg, company) [Default Company Ltd]:shanghai information company Ltd </code><code>#具體名稱</code>
<code>Organizational Unit Name (eg, section) []:</code><code>test</code> <code>#機關名稱</code>
<code>Common Name (eg, your name or your server</code><code>''</code><code>s </code><code>hostname</code><code>) []:*.</code><code>test</code><code>.cn </code><code>#伺服器域名</code>
<code>Email Address []:admin@</code><code>test</code><code>.cn </code><code>#郵箱</code>
<code>Please enter the following </code><code>'extra'</code> <code>attributes</code>
<code>to be sent with your certificate request</code>
<code>A challenge password []: </code><code>#密碼為空,直接回車</code>
<code>An optional company name []: </code><code>#密碼為空,直接回車</code>
4)自己簽發證書
<code>[root@LNMP ssl]</code><code># openssl x509 -req -days 365 -in test.csr -signkey test.key -out test.crt</code>
<code>Signature ok</code>
<code>subject=</code><code>/C</code><code>=cn</code><code>/ST</code><code>=shanghai</code><code>/L</code><code>=shanghai</code><code>/O</code><code>=shanghai information company Ltd</code><code>/OU</code><code>=</code><code>test</code><code>/CN</code><code>=*.</code><code>test</code><code>.cn</code><code>/emailAddress</code><code>=admin@</code><code>test</code><code>.cn</code>
<code>Getting Private key</code>
<code>Enter pass phrase </code><code>for</code> <code>test</code><code>.key: </code><code>#輸入test.key設定的密碼</code>
2、配置nginx.conf檔案
<code>[root@LNMP ssl]</code><code># vim /usr/local/nginx/conf/nginx.conf</code>
<code>添加如下:</code>
<code>server {</code>
<code> </code><code>listen 80;</code>
<code> </code><code>server_name localhost;</code>
<code> </code><code>listen 443; </code><code>#監聽端口</code>
<code> </code><code>ssl on; </code><code>#開啟ssl</code>
<code> </code><code>ssl_certificate </code><code>/usr/local/nginx/conf/test</code><code>.crt; </code><code>#指定證書位置</code>
<code> </code><code>ssl_certificate_key </code><code>/usr/local/nginx/conf/test_nopass</code><code>.key; </code><code>#指定密鑰檔案,如此處使用test.key則每次啟動nginx伺服器需要舒服key密碼。</code>
<code>[root@LNMP ssl]</code><code># nginx -s reload #重加載配置</code>
-----------------------------------end-----------------------------------------------------
本文轉自 80後小菜鳥 51CTO部落格,原文連結:http://blog.51cto.com/zhangxinqi/1959393