昨天瞭解了一下ssl相關知識,而後動手將web服務器增長ssl訪問支持。html
服務器是——nginx反向代理+tomcat做爲web服務器——這樣的體系架構。nginx
首先,使用openssl生成相關祕鑰文件和證書,網上有許多這方面的資源,這裏再也不贅述。web
而後,要判斷nginx當前版本是否已經安裝了ssl模塊。將下面的代碼寫入nginx配置文件:後端
listen 443 ssl; ssl_certificate /data/security/ssl_key_for_nginx.crt; ssl_certificate_key /data/security/ssl_key_for_nginx.key; ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on;
而後執行nginx配置文件的測試命令:tomcat
[root@iZ25qrs2oacZ sbin]# ./nginx -t
若是成功,說明支持ssl模塊,不然,須要追加安裝支持https的模塊。安全
進入nginx的source目錄,執行下列命令:bash
./configure --prefix=/opt/nginx --with-http_stub_status_module --with-http_ssl_module --with-zlib --with-pcre make make install
安裝完畢後,將下面的代碼插入nginx配置文件中(這裏參考了紅薯的文章):服務器
server { listen 443 ssl; ssl_certificate /data/security/ssl_key_for_nginx.crt; ssl_certificate_key /data/security/ssl_key_for_nginx.key; ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; server_name myserver.com; location ~* ^(.*)$ { proxy_set_header host myserver.com; proxy_pass http://localhost:8080; } }
執行測試並經過。若是須要強行將http請求轉換成https請求的話,再加上以下代碼便可:markdown
server { listen 80; server_name myserver.com; rewrite ^(.*) https://$server_name$1 permanent; }
至此,外部對本服務器能夠發起https請求了,nginx會將該請求轉發至後臺tomcat的8080端口。session
**************************************分割線****************************************************
這裏須要說明一點:紅薯的文章中提到,在後端tomcat的server.xml文件中的,必須加入 proxyPort=443,不然會形成Tomcat 中的應用在讀取 getScheme() 方法以及在 web.xml 中配置的一些安全策略會不起做用。這個我沒有作測試,可是,若是https對應用是透明的話,這個也就不重要了。
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="443" proxyPort="443"/>
補充:openssl 生成祕鑰和證書的命令:
openssl genrsa -des3 -out my.key 1024 openssl req -new -key my.key -out my.csr openssl req -new -x509 -key my.key -out my.crt -days 3650
其中,my.key是RSA祕鑰文件,my.crt是證書文件,my.csr是證書申請文件
參考資料:
http://www.oschina.net/question/12_213459#tags_nav http://www.cnblogs.com/yanghuahui/archive/2012/06/25/2561568.html