前情提要
当一个项目部署测试服后会遇到跨域、SSL证书、访问静态文件等各种问题,这个时候我们就可以采用Nginx来解决上述的问题了。Nginx的安装方法请参考《脚本搭建Nginx、Redis、MySql、Maven》,里面有各种软件的脚本安装方式,此篇文章在此基础上开展,后端使用SpringBoot框架。
需求描述:同一个后台地址,多个不同的接口地址后端页面地址为:https://nginx.ifilldream.com/admin/index.html后端页面以及小程序访问多个接口请求地址:1、https://nginx.ifilldream.com/edu/2、https://nginx.ifilldream.com/pre/
系统环境:CentOS Linux release 7.6.1810 (Core)
SpringBoot版本:2.1.7和1.5.3
Jdk:1.8
配置
首先配置SpringBoot的server.servlet.context-path和端口,具体配置请参考如下:
第一个SpringBoot配置:server.port=9000server.servlet.context-path=/edu第一个SpringBoot配置:server.port=8998server.servlet.context-path=/pre注:SpringBoot1.5.3的上下文配置为server.context-path
配置好后台上下文路径后,我们约定如下路径:
后台管理页面的静态文件地址:/usr/share/nginx/html/ifilldreamNginx的配置文件地址:/etc/nginx/conf.d/Nginx的主配置文件地址:/etc/nginx/SSL证书地址:/etc/nginx/cert/
约定好各种路径之后我们就可以开始Nginx的配置文件的编写了
#第一个jar或项目的负载端口配置upstream ifilldream-test1{ server 124.70.156.187:9000 weight=1 max_fails=1 fail_timeout=10s;}#第二个jar或项目的负载端口配置upstream ifilldream-test2{ server 124.70.156.187:8998 weight=1 max_fails=1 fail_timeout=10s;}server { listen 80; #监听80端口 listen 443 ssl; #监听443端口 server_name nginx.ifilldream.com; #域名地址 ssl_certificate /etc/nginx/cert/ifilldream/nginx.ifilldream.com.pem; #SSL的pem文件 ssl_certificate_key /etc/nginx/cert/ifilldream/nginx.ifilldream.com.key; #SSL的key文件 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; ssl_session_cache shared:SSL:10m; ssl_session_timeout 10m; #后台页面的静态资源地址 location /admin { alias /usr/share/nginx/html/hdty; index index.html index.htm; } #第一个jar或目的接口请求配置 location /edu { proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_redirect off; 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; #跨域配置 add_header Access-Control-Allow-Origin $http_origin; add_header Access-Control-Allow-Headers 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,eid,signature,random,timestamp,token,customerid'; #后面可以跟具体项目的请求头字段比如token、eid等 add_header Access-Control-Allow-Credentials 'true'; add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS,DELETE'; client_max_body_size 640m; client_body_buffer_size 256k; proxy_buffers 32 4k; proxy_connect_timeout 6000s; proxy_send_timeout 6000s; proxy_read_timeout 600s; proxy_pass http://ifilldream-test1; #http://后面必须跟upstream的名称 if ($request_method = 'OPTIONS') { return 204; } } #第二个jar或目的接口请求配置 location /pre { proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_redirect off; 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; add_header Access-Control-Allow-Origin $http_origin; add_header Access-Control-Allow-Headers 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,eid,signature,random,timestamp,token,customerid'; add_header Access-Control-Allow-Credentials 'true'; add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS,DELETE'; client_max_body_size 640m; client_body_buffer_size 256k; proxy_buffers 32 4k; proxy_connect_timeout 6000s; proxy_send_timeout 6000s; proxy_read_timeout 600s; proxy_pass http://ifilldream-test2; if ($request_method = 'OPTIONS') { return 204; } } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; }}
好了,至此Nginx的配置就配置好了。如果就是一个单体项目则可以不用配置server.servlet.context-path,然后将location改为/,即 location /。更多关于Nginx的知识敬请期待,如有不解还请留言。
【更多精彩】
自制WebSocket测试工具(前端篇)
Java微信解密算法
脚本搭建Nginx、Redis、MySql、Maven