Nginx + Tomcat + HTTPS 配置

本篇文章主要介绍使用nginx配置tomcat和https的ssl证书。

 

方式一:直接修改nginx的conf文件下的nginx.conf文件

 

Nginx + Tomcat + HTTPS 配置_第1张图片

如下可以直接复制整个server节点,修改红框里的证书为自己的即可。


#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;
	
	#include /usr/local/nginx/conf.d/*.config;

    #gzip  on;

   upstream mysvr {
      #weigth参数表示权值,权值越高被分配到的几率越大   
      #1.down 表示单前的server暂时不参与负载
      #2.weight 默认为1.weight越大,负载的权重就越大。     
      #3.backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。  
      #server 192.168.1.116  down;
      #server 192.168.1.116  backup;
      server 39.108.68.29:8080;
    }
    server {
        listen       80;
        server_name  39.108.68.29;
	rewrite ^(.*)$  https://$host$1 permanent; 
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass http://mysvr;
	    proxy_set_header Host $host; 
	    proxy_set_header Cookie $http_cookie;
	    proxy_set_header X-Real-IP $remote_addr; 
	    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
	    proxy_connect_timeout 600;
 	    proxy_read_timeout 600;
 	    proxy_send_timeout 600; 
		}

        #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;
        #}
		
    }


	 #server {
     # listen 80;
     # server_name www.yitaobei.cn;
     # return 301 https://$server_name$request_uri;
    #}
	

    # 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;
    root html;
    index index.html index.htm;
    ssl_certificate   cert/14330112820728.pem;
    ssl_certificate_key  cert/214330112820728.key;
    ssl_session_timeout 5m;
    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 / {
        root html;
        index index.html index.htm;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-Forwarded-Proto https;
        proxy_redirect off;
        proxy_connect_timeout      240;
        proxy_send_timeout         240;
        proxy_read_timeout         240;
		proxy_pass http://mysvr;
    }
}

}

 

Nginx + Tomcat + HTTPS 配置_第2张图片

 

方式二:在nginx的conf文件下的nginx.conf中引用外部文件

Nginx + Tomcat + HTTPS 配置_第3张图片 

upstream 39.108.68.29
{       
	
	server   39.108.68.29:8080;		
}

server {
        listen       80;
        server_name  39.108.68.29;

        location /
        {
            proxy_pass http://39.108.68.29;
            proxy_set_header Host $host;
            proxy_set_header Cookie $http_cookie; 
	    proxy_set_header X-Real-IP $remote_addr; 
	    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
	    proxy_connect_timeout 600;
 	    proxy_read_timeout 600;
 	    proxy_send_timeout 600;
	    client_max_body_size 20m; 
        }
	
	
        access_log /usr/local/nginx/logs/ytb.log;
	error_log /usr/local/nginx/logs/ytb_error.log;	
}


 Nginx + Tomcat + HTTPS 配置_第4张图片

详解部分参数含义

listen:表示当前的代理服务器监听的端口,默认的是监听80端口。注意,如果我们配置了多个server,这个listen要配置不一样,不然就不能确定转到哪里去了。

server_name:表示监听到之后需要转到哪里去,这时我们直接转到本地,这时是直接到nginx文件夹内。

location:表示匹配的路径,这时配置了/表示所有请求都被匹配到这里

root:里面配置了root这时表示当匹配这个请求的路径时,将会在这个文件夹内寻找相应的文件,这里对我们之后的静态文件伺服很有用。

index:当没有指定主页时,默认会选择这个指定的文件,它可以有多个,并按顺序来加载,如果第一个不存在,则找第二个,依此类推。

 

你可能感兴趣的:(nginx)