1. 程式人生 > >Linux下 nginx+tomcat配置https的總結和遇到的坑

Linux下 nginx+tomcat配置https的總結和遇到的坑

master gcc apache ddr code style remote protocol lis

證書的獲取略

服務器的端口443確保外界網絡能夠進行訪問。

是否配置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  uhear.com.cn;

        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; #映射到本地的8080端口。
            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"; } }

nginx proxy_set_header設置、自定義header

關於nginx代理中請求頭的一些設置的具體原因和原理

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

有的是只寫listen 433;即可。這是由於nginx 版本的原因。最開始我也沒寫,但是一直不能訪問,添加後,一切正常。

光是有這個是不夠的,同時還要對我們的服務器進行配置。既Tomcat.

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 &quot;%r&quot; %s %b" />

上面的value是tomcat自帶的,下面的使我們要添加的
        <Valve className="org.apache.catalina.valves.RemoteIpValve"  
                  remoteIpHeader="x-forwarded-for"  
                   remoteIpProxiesHeader="x-forwarded-by"  
                   protocolHeader="x-forwarded-proto" />

Linux下 nginx+tomcat配置https的總結和遇到的坑