Nginx + Tomcat + Openssl 配置https的web服務
linux 地址 192.168.221.128
一:系統環境
apache-tomcat-8.0.15
jdk1.8.0_05
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
二:使用openssl生成證書
1:首先要生成服務器端的私鑰(key文件):
openssl genrsa -des3 -out server.key 1024
2:運行時會提示輸入密碼,此密碼用于加密key文件(參數des3便是指加密算法,當然也可以選用其他你認為安全的算法.),以后每當需讀取此文件(通過openssl提供的命令或API)都需輸入口令.如果覺得不方便,也可以去除這個口令,但一定要采取其他的保護措施!
去除key文件口令的命令:
openssl rsa -in server.key -out server.key
openssl req -new -key server.key -out server.csr
3:生成證書
openssl req -new -x509 -nodes -out server.crt -keyout server.key
三:配置tomcat
編輯 conf/server.xml文件,添加
1:把原來的
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
改成
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="443" proxyPort="443" />2:添加
<Host name="love.tangshiyi.com" autoDeploy="false">
<Valve className="org.apache.catalina.valves.RemoteIpValve"
remoteIpHeader="x-forwarded-for"
remoteIpProxiesHeader="x-forwarded-by"
protocolHeader="x-forwarded-proto"
/>
<Context path="" docBase="/home/hadoop/vs/tangshiyi" />
</Host>四:配置Nginx
在http里面加入
upstream tomcat {
server 127.0.0.1:8080;
}
server {
listen 443 ssl;
server_name love.tangshiyi.com ;
ssl_certificate /home/hadoop/key/server.crt;
ssl_certificate_key /home/hadoop/key/server.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Proto https;
proxy_pass http://tomcat;
}
}注意,這里面的server_name,必須和tomcat那里的Host保持一致
proxy_set_header Host $http_host; 這一句必須有,否則代理不了
四:配置客戶端Hosts文件
在hosts里面加入
192.168.221.128 love.tangshiyi.com
在瀏覽器中,輸入https://love.tangshiyi.com即可訪問