Nginx之Https 認證加密

來源:互聯網
上載者:User

標籤:https   加密訪問   逗哥   

1.1檢查NginxSSL模組是否安裝

[[email protected]~]# /application/nginx/sbin/nginx -Vnginx version: nginx/1.6.3built by gcc 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC)TLS SNI support enabledconfigure arguments: --prefix=/application/nginx-1.6.3 --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module

1.2準備私密金鑰和認證

1.2.1建立伺服器私密金鑰

[[email protected]~]# cd /application/nginx/conf/[[email protected] conf]# mkdir key[[email protected] conf]# cd key/[[email protected] key]# openssl genrsa -des3 -out server.key 1024Generating RSA private key, 1024 bit long modulus..++++++...++++++e is 65537 (0x10001)Enter pass phrase for server.key:Verifying - Enter pass phrase for server.key:

1.2.2簽發認證

[[email protected] key]# openssl req -new -key server.key -out server.csrEnter pass phrase for server.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) []:BJLocality Name (eg, city) [Default City]:BJOrganization Name (eg, company) [Default Company Ltd]:SDUOrganizational Unit Name (eg, section) []:SA         Common Name (eg, your name or your server‘s hostname) []:XuBuSiEmail Address []:[email protected] Please enter the following ‘extra‘ attributesto be sent with your certificate requestA challenge password []:     An optional company name []:

1.2.3刪除伺服器私密金鑰口令

[[email protected] key]# cp server.key server.key.ori[[email protected] key]# openssl rsa -in server.key.ori -out server.keyEnter pass phrase for server.key.ori:writing RSA key1.2.4產生使用簽章要求認證

和私密金鑰產生自簽認證

[[email protected] key]# openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crtSignature oksubject=/C=CN/ST=BJ/L=BJ/O=SDU/OU=SA/CN=XuBuSi/[email protected]Getting Private key

1.3開啟Nginx SSL

[[email protected] ~]# cat /application/nginx/conf/vhosts/www.conf    server {     server_name jianguo.yi***.com;       #listen       80;       listen       443;       ssl on;       ssl_certificate key/server.crt;       ssl_certificate_key key/server.key; location / {          roothtml/blog;            index  index.php index.html index.htm;        access_log /app/logs/jianguo.log main;        }    }

1.3.1重啟nginx生效

[[email protected] ~]# /application/nginx/sbin/nginx -s reload[[email protected] ~]# netstat -lntup|grep 443tcp        0    0 0.0.0.0:443                 0.0.0.0:*                   LISTEN    1711/nginx

1.3.2測試https

由於該認證非第三方權威機構頒發,而是我們自己簽發的,所以瀏覽器會警告,如果是對外的業務需要加密,必須使用商用第三方簽署憑證。

1.4配置重新導向80連接埠轉443連接埠

以上配置有個不好的地方,如果使用者使用時忘了使用https或者443連接埠,那麼將會報錯,所以我們需要80連接埠的訪問轉到443連接埠並使用ssl加密訪問。

只需要增加一個server段,使用301永久重新導向。

[[email protected] ~]# tail -5 /application/nginx/conf/vhosts/www.confserver {        listen 80;        server_name jianguo.yi*****.cn;        rewrite ^(.*) https://$server_name$1 permanent;}[[email protected] ~]# /application/nginx/sbin/nginx -tnginx: the configuration file /application/nginx-1.6.3/conf/nginx.conf syntax is oknginx: configuration file /application/nginx-1.6.3/conf/nginx.conf test is successful[[email protected] ~]# /application/nginx/sbin/nginx -s reload



本文出自 “逗哥筆記” 部落格,請務必保留此出處http://qiuyt.blog.51cto.com/1229789/1955139

Nginx之Https 認證加密

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.