nginx+tomcat服务器+ssl(阿里云证书)配置+vue前端项目+java后端项目

windows环境下本地搭建


所用工具及版本

  • apache-tomcat-9.0.10
  • nginx-1.15.3
  • IntelliJ IDEA 2018.1.4 x64
  • JetBrains WebStorm 2018.2.1 x64
  • JDK1.8
  • 阿里云证书 (自行登录申请)

nginx配置ssl证书,tomcat不需要配置

  • 在nginx解压目录下的conf文件夹下新建cert文件夹和host文件夹
    nginx+tomcat服务器+ssl(阿里云证书)配置+vue前端项目+java后端项目_第1张图片

  • 下载阿里云证书到本地
    nginx+tomcat服务器+ssl(阿里云证书)配置+vue前端项目+java后端项目_第2张图片
    nginx+tomcat服务器+ssl(阿里云证书)配置+vue前端项目+java后端项目_第3张图片

  • 将java项目打war包放入tomcat安装目录下的webapps文件夹下

  • 将vue前端项目打包并解压放到某个盘符文件夹下,后面用到

  • 将下载好的证书放解压放到cert文件夹下
    nginx+tomcat服务器+ssl(阿里云证书)配置+vue前端项目+java后端项目_第4张图片

  • 在host文件夹下新建hx.conf文件
    nginx+tomcat服务器+ssl(阿里云证书)配置+vue前端项目+java后端项目_第5张图片

hx.conf配置为:

server {
    listen 8090 ssl;     //此端口只是为了测试8090跳转8080
    server_name localhost;

    ssl_certificate    cert/你下载好的证书名字.pem;
    ssl_certificate_key    cert/你下载好的证书名字.key;
    # ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  5m;
    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers  on;

    #proxy_buffering off;

    location = / {
        # root localhost:8080;
        # index index2.html;
        proxy_pass http://localhost:8080;
        proxy_set_header Host      $host;
        proxy_redirect http:// $scheme://;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    location / {
        root "E:/demo/";     //这里是我的E盘下demo文件夹,你可以在此文件夹下放入index.html页面测试
    }
}

server {
    listen 443 ssl;
    server_name localhost;

    ssl_certificate    cert/你下载好的证书名字.pem;
    ssl_certificate_key    cert/你下载好的证书名字.key;
    # ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  5m;
    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers  on;

    #proxy_buffering off;

        root  "E:/demo/dist";     //将前端vue框架项目打包后解压放进E:/demo/dist这里
        index index.html;


        location / {
           root    "E:/demo/dist";
           try_files $uri $uri/ /index.html;
           index  index.html;
        }

        location @router {
             rewrite ^.*$ /index.html last;
        }
        location /api {
                proxy_set_header   Host             $host;
                proxy_set_header   x-forwarded-for  $remote_addr;
                proxy_set_header   X-Real-IP        $remote_addr;
                proxy_pass http://localhost:8080/项目名;     //tomcat默认8080地址,后面是你的java打包项目名
        }

}
  • 找到nginx解压目录下的nginx.conf文件(文件一般在conf文件夹下),这里只新增了两处配置
#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;

    server {
        listen       80;
        server_name  localhost;
        
        #这是第一处新增的配置,将http转发为https
		rewrite ^(.*) https://$server_name$1 permanent;
		
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

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

        #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 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
	
	#这是第二处新增的配置,将host文件夹里的配置引入
	include host/*.conf;
}
  • 配置完毕,启动tomcat和nginx测试吧

cmd命令 windows系统启动nginx命令:

启动
D:\Program Files\nginx-1.15.3>start nginx.exe
重启
D:\Program Files\nginx-1.15.3>nginx.exe -s reload
快速停止
D:\Program Files\nginx-1.15.3>nginx.exe -s stop
停止
D:\Program Files\nginx-1.15.3>nginx.exe -s quit
查看配置是否成功
D:\Program Files\nginx-1.15.3>nginx.exe -t

tomcat启动

linux环境下搭建


所用工具及版本

  • apache-tomcat-9.0.10
  • nginx-1.15.3
  • JDK1.8
  • 阿里云证书 (自行登录申请)

nginx配置ssl证书,tomcat不需要配置

  • 在nginx安装目录下的conf文件夹下新建cert文件夹 /usr/local/nginx/conf

  • 将前面申请到的证书文件放入cert文件夹下

  • 配置nginx.conf

server {
    listen 8443 ssl;
    server_name 你申请证书时用的域名;

    root    /usr/local/dist;     #前端vue项目放的地方
    index index.html;

    ssl_certificate  cert/证书名.pem;
    ssl_certificate_key  cert/证书名.key;

    #ssl_session_cache    shared:SSL:1m;
    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    /usr/local/dist;
           try_files $uri $uri/ /index.html;
           index  index.html;
        }

        location @router {
             rewrite ^.*$ /index.html last;
        }
        location /api {
                proxy_set_header   Host             $host;
                proxy_set_header   x-forwarded-for  $remote_addr;
                proxy_set_header   X-Real-IP        $remote_addr;
                proxy_pass http://ip:端口号/;      #此处为你后台端口
        }

 }
  • 配置完成,重启nginx

效果展示

效果


你可能感兴趣的:(服务器,java)