一个管理端项目:admin-controller,之前是前端和后端项目都放在一个项目中,如下图所示:
现在的需求是需要把protocol目录下的静态文件剥离出springboot项目。静态文件需要部署在nginx上。
1. 备份并删除后端项目中所有protocol目录下的文件。
2. 在/usr/local/nginx/static/目录下新建myprotocol目录,并且把1中备份的静态文件拷贝到/usr/local/nginx/static/myprotocol 下。
3. nginx配置文件nginx.conf中配置:
server {
listen 80;
server_name protocol.com;
location /myprotocol { # myprotocol:为前端项目在服务器上的目录位置
root /usr/local/nginx/static; //会在static目录下寻找 myprotocol/xxx
index index.html index.htm;
}
location /cfgadmin { #cfgadmin:为后端项目接口根路径
proxy_pass https://xxxx.xx.com; #需要代理后端服务的域名,
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
4. 如果nginx不支持https配置,需要安装nginx支持的https模块。重新编译nginx源码。
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
如果之前没有安装openssl,需要在执行./configure命令之前安装:
yum -y install openssl openssl-devel make zlib zlib-devel gcc gcc-c++ libtool pcre pcre-devel
5. 访问静态文件:http://protocol.com/myprotocol/index.html。正常请求后端服务数据成功。请求的域名为自己windows上的host文件映射的自定义假域名。如下图所示: