nginx + tomcat + https配置html
模式:
客戶端 ---https -----> nginx ----- http ------> tomcatpython
瀏覽器和 Nginx 之間走的 HTTPS 通信,而 Nginx 到 Tomcat 經過 proxy_pass 走的是普通 HTTP 鏈接。nginx
證書申請:web
在有域名的服務器上部署申請證書的程序:apache
備註:python 版本在2.6以上瀏覽器
1.
#mkdir ~/cert/
wget https://dl.eff.org/certbot-auto
chmod a+x certbot-autotomcat
二、mkdir ~/.pip
pip.conf配置文件:
[global]
index-url=https://pypi.doubanio.com/simple/安全
[install]
trusted-host=pypi.doubanio.com服務器
三、安裝申請證書所依賴的工具
cd ~/cert
#./certbot-auto網絡
安裝過程可能須要比較長的時間,有時多是網絡鏈接很差執行不成功
You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=www.lelaohui.com.cn
-------------------------------------------------------------------------------
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/www.lelaohui.com.cn/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/www.lelaohui.com.cn/privkey.pem
Your cert will expire on 2017-12-12. To obtain a new or tweaked
version of this certificate in the future, simply run certbot-auto
again with the "certonly" option. To non-interactively renew *all*
of your certificates, run "certbot-auto renew"
./certbot-auto certonly
四、網站申請證書
備註: 申請以前443端口應用關閉
#./certbot-auto certonly --standalone -d piaoyu.online -d www.piaoyu.online
申請成功後會在目錄:/etc/letsencrypt/live/www.piaoyu.online/ 保存證書
#ls /etc/letsencrypt/live/www.piaoyu.online/
cert.pem chain.pem fullchain.pem privkey.pem
證書延期測試:
./certbot-auto renew --dry-run
自動續約證書:
30 */8 */80 * * root /root/cert/certbot-auto renew --quiet
備註: 續約以前443端口應用關閉
五、nginx 配置
[root@appserver88 conf.d]# cat default.conf
#
# The default server
#
server {
listen 80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
proxy_buffering off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://tomcat;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
###########################
[root@appserver88 conf.d]# cat ssl.conf
#
# HTTPS server configuration
#
server {
listen 443 ssl default_server;
server_name _;
root /usr/share/nginx/html;
#
ssl_certificate /etc/letsencrypt/live/www.piaoyu.online/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.piaoyu.online/privkey.pem;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 10m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
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_redirect off;
proxy_connect_timeout 240;
proxy_send_timeout 240;
proxy_read_timeout 240;
proxy_pass http://tomcat;
}
#
error_page 404 /404.html;
location = /40x.html {
}
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
######################
[root@appserver88 conf.d]# cat upstream.conf
upstream tomcat {
#server 127.0.0.1:8080 fail_timeout=0;
server 10.28.11.117:8090;
}
########################
六、tomcat配置
主要修改:server.xml文件
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="443"
proxyPort="443" />
添加:
<Valve className="org.apache.catalina.valves.RemoteIpValve"
remoteIpHeader="x-forwarded-for"
remoteIpProxiesHeader="x-forwarded-by"
protocolHeader="x-forwarded-proto" />
注意的是必須有proxyPort=」443″,這是整篇文章的關鍵,固然 redirectPort 也必須是 443。
同時 <Value> 節點的配置也很是重要,不然你在 Tomcat 中的應用在讀取 getScheme() 方法以及在 web.xml 中配置的一些安全策略會不起做用。
那麼,在同一個IP上,如何配置多個HTTPS主機呢?
nginx支持TLS協議的SNI擴展(Server Name Indication,簡單地說這個擴展使得在同一個IP上能夠以不一樣的證書serv不一樣的域名)。不過,SNI擴展還必須有客戶端的支持,另外本地的OpenSSL必須支持它。
若是啓用了SSL支持,nginx便會自動識別OpenSSL並啓用SNI。是否啓用SNI支持,是在編譯時由當時的 ssl.h 決定的(SSL_CTRL_SET_TLSEXT_HOSTNAME),若是編譯時使用的OpenSSL庫支持SNI,則目標系統的OpenSSL庫只要支持它就能夠正常使用SNI了。
nginx在默認狀況下是TLS SNI support disabled。
2# /usr/local/nginx/sbin/nginx -VTLS SNI support enabled