当前位置: 移动技术网 > 网络运营>服务器>nginx > nginx https反向代理tomcat的2种实现方法

nginx https反向代理tomcat的2种实现方法

2019年04月18日  | 移动技术网网络运营  | 我要评论

反向代理

在计算机世界里,由于单个服务器的处理客户端(用户)请求能力有一个极限,当用户的接入请求蜂拥而入时,会造成服务器忙不过来的局面,可以使用多个服务器来共同分担成千上万的用户请求,这些服务器提供相同的服务,对于用户来说,根本感觉不到任何差别。

nginx做前端代理分发,tomcat处理请求。nginx反代tomcat实现https有二个方法。

一、nginx配置https,tomcat也配置https

1、nginx配置https

upstream https_tomcat_web { 
  server 127.0.0.1:8443; 
} 
server { 
  listen  443; 
  server_name www.test.com; 
  index ; 
  root /var/www/html/test; 
  ssl on; 
  ssl_certificate /etc/nginx/go.pem; 
  ssl_certificate_key /etc/nginx/go.key; 
  ssl_session_timeout 5m; 
  ssl_protocols sslv2 sslv3 tlsv1.2; 
#  ssl_ciphers all:!adh:!export56:rc4+rsa:+high:+medium:+low:+sslv2:+exp; 
  ssl_ciphers ecdhe-rsa-aes128-gcm-sha256:ecdhe:ecdh:aes:high:!null:!anull:!md5:!adh:!rc4; 
  ssl_prefer_server_ciphers on; 
  location ~ ^/admin { 
   proxy_pass https://https_tomcat_web; //是https的 
   proxy_redirect      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; 
   client_max_body_size  100m; 
   client_body_buffer_size 256k; 
   proxy_connect_timeout  60; 
   proxy_send_timeout   30; 
   proxy_read_timeout   30; 
   proxy_buffer_size   8k; 
   proxy_buffers    8 64k; 
   proxy_busy_buffers_size 64k; 
   proxy_temp_file_write_size 64k; 
  } 
  error_page 404 /404.html; 
  location = /40x.html { 
  } 
  error_page 500 502 503 504 /50x.html; 
  location = /50x.html { 
  } 
} 

2、tomcat的https配置,配置文件server.xml

<service name="catalina"> 
 <connector port="8001" protocol="http/1.1" 
 connectiontimeout="20000" 
 redirectport="8443" /> 
 <connector port="8091" 
 protocol="ajp/1.3" 
 redirectport="8443" /> 
//添加以下内容 
 <connector port="8443" 
 protocol="http/1.1" 
 sslenabled="true" 
 scheme="https" 
 secure="false" 
 keystorefile="cert/gotom.pfx" 
 keystoretype="pkcs12" 
 keystorepass="214261272770418" 
 clientauth="false" 
 sslprotocol="tlsv1+tlsv1.1+tlsv1.2" ciphers="tls_rsa_with_aes_128_cbc_sha,tls_rsa_with_aes_256_cbc_sha,tls_ecdhe_rsa_with_aes_128_cbc_sha,tls_ecdhe_rsa_with_aes_128_cbc_sha256,tls_rsa_with_aes_128_cbc_sha256,tls_rsa_with_aes_256_cbc_sha256" /> 
 
 ..................省略.................... 
 </service> 

配置好后重新启动nginx,tomcat,就可以https访问了,这也是我现在采用的配置方式 。

二、nginx采用https,tomcat采用http

1、nginx配置https

upstream https_tomcat_web { 
  server 127.0.0.1:8001; 
} 
server { 
  listen  443; 
  server_name www.test.com; 
  index ; 
  root /var/www/html/test; 
  ssl on; 
  ssl_certificate /etc/nginx/go.pem; 
  ssl_certificate_key /etc/nginx/go.key; 
  ssl_session_timeout 5m; 
  ssl_protocols sslv2 sslv3 tlsv1.2; 
#  ssl_ciphers all:!adh:!export56:rc4+rsa:+high:+medium:+low:+sslv2:+exp; 
  ssl_ciphers ecdhe-rsa-aes128-gcm-sha256:ecdhe:ecdh:aes:high:!null:!anull:!md5:!adh:!rc4; 
  ssl_prefer_server_ciphers on; 
  location ~ ^/admin { 
   proxy_pass http://https_tomcat_web; //是http的 
   proxy_redirect      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; 
   client_max_body_size  100m; 
   client_body_buffer_size 256k; 
   proxy_connect_timeout  60; 
   proxy_send_timeout   30; 
   proxy_read_timeout   30; 
   proxy_buffer_size   8k; 
   proxy_buffers    8 64k; 
   proxy_busy_buffers_size 64k; 
   proxy_temp_file_write_size 64k; 
  } 
  error_page 404 /404.html; 
  location = /40x.html { 
  } 
  error_page 500 502 503 504 /50x.html; 
  location = /50x.html { 
  } 
} 

2、tomcat的http配置,配置文件server.xml

<service name="catalina"> 
 <connector port="8001" protocol="http/1.1" 
 connectiontimeout="20000" 
 redirectport="443" /> //在这里重新定向到了443端口 
 
 <connector port="8091" 
 protocol="ajp/1.3" 
 redirectport="443" /> 
 
 ..................省略.................... 
 </service> 

重启nginx,tomcat,https就配置好了。

不管是第一种方法,还是第二种方法,如果通过http,直接访问8001端口,浏览器都会提示你不安全的访问,因为本身是http,确被重定向到了https。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对移动技术网的支持。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网