zl程序教程

您现在的位置是:首页 >  工具

当前栏目

Nginx+tomcat+ssl免费证书配置

TomcatSSL配置Nginx 免费 证书
2023-09-11 14:15:00 时间

0.说明

本文说描写叙述的方式是用nginx的443重定向到tomcat的8443,nginx的80port重定到tomcat的8080;

乱入:个人标记:caicongyang

1.nginx安装

能够參考我前面的文章: Linux tar包安装Nginx  ;http://blog.csdn.net/caicongyang/article/details/46388845

只是这篇文章中。我们编译的时候没有带ssl模块。因此须要又一次编译安装

须要在安装时带上ssl模块的选项 

完毕命令例如以下:

#./configure --with-http_ssl_module 

当然你能够用下面命令查看全部的编译选项

#./configure --help

2.nginx生成免费证书

# cd /opt/nginx/sslkey/
# openssl genrsa -des3 -out server.key 1024
# openssl req -new -key server.key -out server.csr
# cp server.key server.key.org
# openssl rsa -in server.key.org -out server.key
# openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

3.tomcat配置

server.xml

 <Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol"
               maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS" keystoreFile="${user.home}/.
keystore" keystorePass="123456"/>


我的tomcat证书是又一次在生成的:(当前用户路径下)

#keytool -v -genkey -alias tomcat -keyalg RSA -keystore .keystore  -validity 36500
当然你也能够指定文件夹

#keytool -v -genkey -alias tomcat -keyalg RSA -keystore /opt/tomcat/sslkey/server.keystore  -validity 36500


当然你也能够在项目的web.xml中配置某个重要模块强制使用https,其它的模块正常走http

web.xml

 <security-constraint> 
           <web-resource-collection> 
                <web-resource-name>services</web-resource-name> 
                <url-pattern>/login/*</url-pattern> 
                </web-resource-collection> 
                <user-data-constraint> 
                       <transport-guarantee>CONFIDENTIAL</transport-guarantee> 
                </user-data-constraint> 
  </security-constraint> 

以上配置规定路径带login的所有走https


4.nginx配置

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    gzip  on;

	upstream tomcat8080 { 
      server  localhost:8080  weight=10;   
	}
	
	upstream tomcat8443 { 
      server  localhost:8443  weight=10;   
	} 	
	
	
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
			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://tomcat8080;    
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    
  

    
    server {
       listen       443;
       server_name  localhost;

        ssl                  on;
        ssl_certificate      /opt/nginx/sslkey/server.crt;
        ssl_certificate_key   /opt/nginx/sslkey/server.key;

        ssl_session_timeout  5m;

        ssl_protocols  SSLv2 SSLv3 TLSv1;
        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers   on;

        location / {
            proxy_pass https://tomcat8443;
            proxy_set_header Host $host:443;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }
}


不懂运维的程序猿,不是好的project师!

我的个人站点:http://www.caicongyang.com

我的CSDN博客地址: http://blog.csdn.net/caicongyang