前言:
以主应用demo-base、子应用1(sub-system)、子应用2(sub-report)为例。
总结,微前端部署配置方法及优缺点。
方法一:一个nginx搞定,配置多个server监听多个端口。
见配置:(nginx.config)
# 主应用
server {
listen 9000;
server_name localhost;
location / {
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
root /usr/local/micro/demo-base/dist/;
if (!-e $request_filename) {
rewrite ^(.*)$ /index.html;
}
}
location ~*\.(js|css|png|jpg|jpeg|gif|ico)$ {
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
root /usr/local/micro/demo-base/dist/;
expires 30d;
}
location /api/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://IP:PORT/;
}
location /system_api/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://IP1:PORT1/;
}
location /report_api/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://IP2:PORT2/;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
# 子应用1
server {
listen 9001;
server_name localhost;
location / {
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
root /usr/local/micro/sub-system/dist/;
if (!-e $request_filename) {
rewrite ^(.*)$ /index.html;
}
}
location ~*\.(js|css|png|jpg|jpeg|gif|ico)$ {