Linux下 nginx+tomcat配置https的总结和遇到的坑 - 亲试有效

    xiaoxiao2022-07-05  157

    文章作者:兼爱子墨 文章地址:http://www.cnblogs.com/wbq1113/p/9357332.html 本人参考大佬的这篇文章,并且追了功能:可以使用http://xxx 访问时重定向到https://xxx,原文章当你使用http://xxx访问时会提示找不到服务器o( ̄︶ ̄)o

    Linux下 nginx+tomcat配置https的总结和遇到的坑 - 亲试有效

    前言开始配置1. 首先查看nginx是否支持SSL。2. nginx的配置3.tomcat的配置

    前言

    证书的获取略

    服务器的端口443和80确保外界网络能够进行访问。(也就是你的阿里云安全组放开https的443端口和放开http80端口,否者无法从http://xxx 自动重定向到https://xxx)

    是否配置https:

    nginx:是

    tomcat:否

    开始配置

    1. 首先查看nginx是否支持SSL。

    参考链接:实战http切换成https 查看nginx支持SSL

    [root@ytkj bin]# /usr/local/nginx/sbin/nginx -V nginx version: nginx/1.13.3 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC) built with OpenSSL 1.0.2k-fips 26 Jan 2017 TLS SNI support enabled configure arguments: --with-http_ssl_module [root@ytkj bin]#

    看到有–with-http_ssl_module。说明支持SSL。

    此处我遇到了第一个坑:

    按照我上面的参考链接增加了SSL模块的小伙伴们注意了。在执行命令

    [root@ytkj bin]# /usr/local/nginx/sbin/nginx -s reload

    重启后,可能SSL模块并不会生效。而是要通过重启nginx主线程来重新加载配置。

    [root@ytkj nginx-1.13.3]# ps -ef | grep nginx root 8802 10800 0 10:23 ? 00:00:00 nginx: worker process is shutting down root 8803 10800 0 10:23 ? 00:00:00 nginx: worker process is shutting down root 9992 4681 0 14:45 pts/0 00:00:00 grep --color=auto nginx root 10800 1 0 6月29 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf [root@ytkj nginx-1.13.3]# kill -QUIT 10800 [root@ytkj nginx-1.13.3]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

    2. nginx的配置

    server { listen 443 ssl; server_name www.baidu.com; #这里输入你的域名哦。 ssl on; ssl_certificate cert/yourCA.pem; #当前conf/目录下 ssl_certificate_key cert/yourCA.key; ssl_session_cache shared:SSL:10m; ssl_session_timeout 5m; #ssl_server_tokens off; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; location / { proxy_pass http://127.0.0.1:8080; #映射到本地的Tomcat8080端口。 proxy_redirect off; proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-Proto https; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location /webSocket/ {        #webSocket在https下的配置 proxy_pass http://127.0.0.1:8080; proxy_http_version 1.1; proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;        #主要是通过下方的两个属性来升级该请求,告诉服务器,我这个是webSocket请求。 proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "Upgrade"; } } server { listen 80; server_name www.baidu.com; rewrite ^(.*)$ https://$host$1 permanent; }

    上段代码中第二个server既是追了功能:可以使用http://xxx 访问时重定向到https://xxx

    nginx proxy_set_header设置、自定义header 关于nginx代理中请求头的一些设置的具体原因和原理

    在这个配置中,我遇到的坑是listen 433 ssl;和 ssl on;

    有的是只写listen 433;即可。这是由于nginx 版本的原因。最开始我也没写,但是一直不能访问,添加后,一切正常。

    光是有这个是不够的,同时还要对我们的服务器进行配置。既Tomcat.

    ==在这里提一下,原博主写的是433 无法访问,必须追加ssl , 本人环境CenOS6.8 Nginx1.8 发现加不加ssl都可以访问。 ==

    3.tomcat的配置

    <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> 变成 <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="443" proxyPort="443" /> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> 上面的value是tomcat自带的,下面的使我们要添加的 <Valve className="org.apache.catalina.valves.RemoteIpValve" remoteIpHeader="x-forwarded-for" remoteIpProxiesHeader="x-forwarded-by" protocolHeader="x-forwarded-proto" />

    这里必须表扬原博主,心非常细致(作为程序员必须要严谨嘛,可不能只实现效果即可,好了,说的多了O(∩_∩)O哈哈~),我参考其他博文时下边直接给出value…的配置,我当时以为这是修改呢,原来是追加,重要的话说一遍:<Connector port="8080"是修改 ,<Valve className=是追加内容 ,切记!

    PS:

    **到这里配置就完成了,重启tomcat 和 nginx ,测试一下吧,有问题可以给我留言哦o( ̄︶ ̄)o **

    最新回复(0)