nginx tomct https
之前给公司做https,将域名认证后,在nginx中做配置:
nginx ssl 配置
upstream jrapi3_server_pool {
server 127.0.0.1:9798; #访问同一个tomcat的9798端口
}
upstream jrapi3_https_pool { #访问同一个tomcat的9797端口
server 127.0.0.1:9797;
}
server { listen 443; include testapi3_ssl.conf; #将这个域名的ssl设置单独写在conf中。 server_name testapi3-joyreader.wawayaya.com; ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; location / { proxy_next_upstream http_502 http_504 error timeout invalid_header; proxy_pass http://jrapi3_https_pool; proxy_set_header Host $host; #proxy_set_header X-forwarded-for $remote_addr; proxy_set_header X-forwarded-for $proxy_add_x_forwarded_for; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-Proto https; #这个参数一会tomcat中的配置会用的到。 proxy_redirect off; #root html; #index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } access_log /opt/logs/nginx_logs/testapi3_ssl.access.log; error_log /opt/logs/nginx_logs/testapi3_ssl.error.log; }
这样从外面访问nignx的时候就是https了,但是这样反代到后面的tomcat还是会使用http协议,如果此时客户端只是单纯的请求数据,哪没有什么问题,但是要是设计到有页面展示的话就会有样式,格式不对的情况发生。所以我们也要让后面的tomcat支持https。
tomcat默认不支持https。所以我们要改几个参数
现在有几个方法可以实现,一个是让tomcat自己解析https这个协议,就是将域名申请得认证在tomcat中也做配置,这样做就是将tomcat作为web服务器访问使用。
第二就是让tomcat接受https协议。因为我们前面有nginx,并且nginx做了https,所以我们只需要使用第二种方法就可以了。而且这种方法也比较简单
tomcat ssl 配置
<Connector port="9797" protocol="HTTP/1.1" #注意这个端口也下面http的端口不能一样,否则端口冲突 connectionTimeout="20000" redirectPort="443" #这样tomcat就支持https了 proxyPort = "443"/> <Valve className="org.apache.catalina.valves.RemoteIpValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t "%r" %s %b" remoteIpHeader="X-forwarded-for" remoteIpProxiesHeader="x-forwarded-by" protocolHeader="x-forwarded-proto" /> 这样这个tomcat就还支持了https了,但是如果要是使用http协议访问就出之前不支持https时使用https访问的错误了,所以我们要让tomcat同时支持https和http 那就在原有的配置文件里面再加一个端标签
<Service name="Catalina_http"> <Connector port="9798" protocol="HTTP/1.1" #这个端口是http访问时的端口,和上面的https的端口不能一样。 connectionTimeout="20000" redirectPort="8643" /> #这样就支持http了 <!-- <Connector port="9709" protocol="AJP/1.3" redirectPort="9743" /> --> <Engine name="Catalina" defaultHost="localhost"> <Realm className="org.apache.catalina.realm.LockOutRealm"> <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/> </Realm> <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> </Host> </Engine> </Service>
这样nginx 反代时 如果需要使用https协议时就将proxy_pass 指向 9797,如果需要使用http协议,就再起一个server将 proxy_pass 指向9798。