在我目前正在工作的一个项目中,我面临许多与路由相关的问题。我认为这是一个配置问题,任何有类似经验的人都可以分享他的经验。
项目可以在本地计算机上构建并完美运行。
Devops/
src/
index.html
app/
app-routing.module.ts
app.component.ts
app.component.html
home/
home.component.html
home.component.ts
dashboard/
dashboard.component.html
dashboard.component.ts
在app-routing.module.ts中,设置所有路由,其中所有路径都映射到相应的组件。另外,默认设置为Home组件。按照标准设置,index.html具有默认定义的以下标记和值<base href="/">
在构建过程中,名称空间和应用程序名称存储在环境变量(ENVVALUES)中。
在kubernetes中,可以说,域是www.xcompany.com,我的名称空间是MaxxD,而已部署的应用程序是Devops,那么托管站点的uri将是< [www.xcompany.com/maxxd/devops。以这种方式配置。另外,根据Kubernetes标准,还应该提供一个运行状况检查网页,以确定该Pod的可用性和运行状况。健康页面的路径为:www.xcompany.com/maxxd/devops/api/health
server {
listen 80;
server_name localhost;
location ${ENVVALUES}/ {
alias /usr/share/nginx/html/;
index index.html index.htm;
#try_files $uri $uri/ index.html
}
location /api/health/ {
alias /usr/share/nginx/html/health/;
index health.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
正如我们所看到的,默认域名后面附加了,其中包含名称空间和应用程序名称的信息(“ MaxxD / Devops”)ENVVALUES
Docker Build是一个多阶段的,包括2个步骤。
以下是DockerFile的内容:
### STAGE 1: Build ###
# We label our stage as ‘builder’
FROM node as builder
# Create the working folder
RUN mkdir -p /usr/src/app
# We define our current path inside of the container
WORKDIR /usr/src/app
# We copy both the package.json and the package.lock.json into the image
COPY package*.json /usr/src/app/
# run npm install to install Angular CLI
RUN npm install @angular/cli -g
# run npm install to install all dependencies of the project
RUN npm ci
# copy all file from repo to build machine
COPY . /usr/src/app/
## Build the angular app in production mode and store the artifacts in dist folder
RUN ng build --prod
### STAGE 2: Setup ###
FROM nginx
# Expose the port 80 of the running container
EXPOSE 80
## Copy our default nginx config
COPY conf/default.conf /etc/nginx/conf.d/
## Remove default nginx website
RUN rm -rf /usr/share/nginx/html/*
## From ‘builder’ stage copy over the artifacts in dist folder to default nginx public folder
COPY --from=builder /usr/src/app/dist/devops /usr/share/nginx/html
## From ‘builder’ stage copy over the artifacts in dist folder to default nginx public folder
COPY --from=builder /usr/src/app/health /usr/share/nginx/html/health
CMD /bin/bash -c "envsubst < /etc/nginx/conf.d/default.conf > /etc/nginx/conf.d/default.conf && exec nginx -g 'daemon off;'"
在最后一步,修改了nginx conf文件,并使用值转置了ENVVALUES。“ {namespace / AppName}”
因此,如果我们将Base href值设置为“ /”,那么该站点可以在本地正常运行,但不能在Kubernetes中运行。 但是,如果设置为“ ./”,它将在本地加载不安全的文件,不加载安全的文件(由于如上所述的Okta隐式/回调组件),就像在Kubernetes中一样,它加载了不安全的文件,但是像本地实例一样,在成功身份验证之后,Okta隐式/回调的重定向无法成功进行。
我知道这与nginx路由以及服务器如何处理url并加载适当的部分有关。