Nginx+HTTPS(SSL/TLS)

來源:互聯網
上載者:User

標籤:

環境

首先確保機器上安裝了openssl和openssl-devel

rpm -qa | grep openssl#yum install openssl#yum install openssl-devel

確認nginx是否安裝了SSL模組,如下的命令:

/opt/nginx/sbin/nginx -V

看是否輸出--with-http_ssl_module,如果沒有需要重新設定並安裝下。

建立認證 【自己頒發認證給自己】
#cd /usr/local/nginx/conf#openssl genrsa -des3 -out server.key 1024#openssl req -new -key server.key -out server.csr#openssl rsa -in server.key -out server_nopwd.key#openssl x509 -req -days 365 -in server.csr -signkey server_nopwd.key -out server.crt
#cd /opt/nginx/conf#openssl genrsa -des3 -out cert.key 2048  //建立伺服器私密金鑰#openssl req -new -key cert.key -out cert.csr  //簽章要求的認證

會給出如下的提示:

Enter pass phraseforcert.key:You are about to be asked to enter information that will be incorporatedinto your certificate request.What you are about to enter is what is called a Distinguished Name or a DN.There are quite a few fields but you can leave some blankFor some fields there will be a default value,If you enter‘.‘, the field will be left blank.-----Country Name (2 letter code) [XX]:CNState or Province Name (full name) [BeijingLocality Name (eg, city) [Default City]:BeijingOrganization Name (eg, company) [Default Company Ltd]:demoOrganizational Unit Name (eg, section) []:localhostCommon Name (eg, your name or your server‘shostname) []:localhostEmail Address []:[email protected]Please enter the following‘extra‘attributesto be sent with your certificate requestA challenge password []:An optional company name []:

冒號後面是需要我們填寫的,最後兩個我都沒有填。帶空的中括弧似乎可以選填。

製作解密後的私密金鑰

先將cert.key檔案複製一份為cert.key.orgcp cert.key cert.key.org#openssl rsa -in cert.key.org -out cert.key接下來,最後一步,用cert.csr和cert.key產生cert.crt檔案#openssl x509 -req -days 365 -in cert.csr -signkey cert.key -out server.crt
配置nginx

必須在server配置塊中開啟SSL協議,還需要指定伺服器端認證和密鑰檔案的位置,開啟conf/vhosts下面的設定檔:
PHP樣本:

server {    listen 443;     server_name _;    access_log off;     ssl on;    ssl_certificate /opt/nginx/conf/vhosts/cert.crt;    ssl_certificate_key /opt/nginx/conf/vhosts/cert.key;    ssl_session_timeout 10m;        ssl_session_cache    shared:SSL:10m;    ssl_protocols  SSLv2 SSLv3 TLSv1;    ssl_ciphers  HIGH:!aNULL:!MD5;    ssl_prefer_server_ciphers   on;     location / {        root /mnt/html/test;        index index.php index.html;    }     error_page 500 502 503 504 /50x.html;    location = /50x.html {        root /opt/nginx/html;    }     location ~ \.php$ {        fastcgi_pass   127.0.0.1:9000;        fastcgi_index  index.php;        fastcgi_param  SCRIPT_FILENAME  /mnt/html/test$fastcgi_script_name;        include        fastcgi_params;    }     location ~ /\.ht {        deny all;    }}

SSL操作需要消耗CPU資源,所以在多處理器的系統,需要啟動多個背景工作處理序,而且數量需要不少於可用CPU的個數。最消耗CPU資源的SSL操作 是SSL握手,有兩種方法可以將每個用戶端的握手運算元量降到最低:第一種是保持用戶端長串連,在一個SSL串連發送多個請求,第二種是在並發的串連或者 後續的串連中重用SSL會話參數,這樣可以避免SSL握手的操作。會話緩衝用於儲存SSL會話,這些緩衝在背景工作處理序間共用,可以使用 ssl_session_cache指令進行配置。1M緩衝可以存放大約4000個會話。預設的緩衝逾時是5分鐘,可以使用 ssl_session_timeout加大它。

如果HTTP和HTTPS虛擬機器主機的功能是一致的,可以配置一個虛擬機器主機,既處理HTTP請求,又處理HTTPS請求。 配置的方法是刪除ssl on的指令,並在*:443連接埠添加參數ssl:

server {    listen              80;    listen              443 ssl;    server_name         www.example.com;    ssl_certificate     www.example.com.crt;    ssl_certificate_key www.example.com.key;    ...}

樣本:【自己去理解各參數!】

server {    listen 80;    listen 443 ssl;    server_name lvtao.net;    client_max_body_size 10M;    ssl_certificate     /etc/nginx/ssl/www.crt;    ssl_certificate_key /etc/nginx/ssl/private.key;    ssl_session_cache shared:SSL:10m;    ssl_session_timeout 5m;    ssl_dhparam /etc/nginx/ssl/dhparam.pem;    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;    ssl_prefer_server_ciphers on;    ssl_ciphers "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES256-GCM-SHA384:AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256:AES256-SHA:AES128-SHA:DES-CBC3-SHA:HIGH:!aNULL:!eNULL:!EXPORT:!DES:!MD5:!PSK:!RC4";    ssl_stapling on;    ssl_stapling_verify on;    resolver 8.8.8.8 8.8.4.4 valid=300s;    resolver_timeout 5s;    add_header Strict-Transport-Security max-age=15768000;}
Nginx 配置 SSL 重啟免密碼

Nginx 裡面的配置還是老樣子,不過有個問題就是重啟 Nginx 的時候會要求輸入密碼,可以有個辦法免輸密碼。敲入如下指令:

openssl rsa -in pupboss.key -out pupboss_unsecure.key
強制 HTTPS

加上如下代碼

server {    listen 80;    server_name lvtao.net;    return 301 https://$server_name$request_uri;}

Nginx+HTTPS(SSL/TLS)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.