我正在尝试通过8080端口上的rest api将与后端通信的前端连接,前端在nginx上运行,具有非常基本的配置,该配置可服务根文件夹中的静态内容并通过/ api将请求代理到api
下面是配置和部署。
1 /后端部署和服务Yaml文件
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: wetrust-backend
spec:
replicas: 1
template:
metadata:
labels:
app: wetrust-backend
tier: backend
track: stable
spec:
securityContext:
runAsUser: 1000
fsGroup: 1000
containers:
- name: backend
image: "leean/wetrust-backend"
imagePullPolicy: Always
ports:
- containerPort: 8080
volumeMounts:
- name: nfs-volume
mountPath: /home/uploadDir # Please change the destination you like the share to be mounted too
volumes:
- name: nfs-volume
nfs:
server: 192.168.99.98 # Please change this to your NFS server
path: /home/uploadDir # Please change this to the relevant share
---
apiVersion: v1
kind: Service
metadata:
name: wetrust-backend
spec:
ports:
- port: 8080
targetPort: 8080
selector:
app: wetrust-backend
tier: backend
2 /前端部署和服务yaml文件
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: wetrust-frontend
spec:
replicas: 1
template:
metadata:
labels:
app: wetrust-frontend
tier: frontend
track: stable
spec:
containers:
- name: nginx
image: "leean/wetrust-frontend"
imagePullPolicy: Always
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: wetrust-frontend
spec:
ports:
- port: 8889
targetPort: 80
selector:
app: wetrust-frontend
tier: frontend
type: NodePort
3 /在要生成图像的静态工件的dist文件夹中的nginx配置
upstream wetrust-backend {
server wetrust-backend:8080;
}
server {
listen 80;
charset uft-8
gzip on;
gzip_vary on;
gzip_min_length 10240;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/xml;
gzip_disable "MSIE [1-6]\.";
#location / {
# proxy_pass http://backend;
#}
location / {
root /usr/share/nginx/html;
index index.html;
expires -1;
add_header Pragma "no-cache";
add_header Cache-Control "no-store, no-cache, must-revalidate, post-check=0, pre-check=0";
try_files $uri$args $uri$args/ $uri $uri/ /index.html =404;
}
location /api/ {
proxy_pass http://wetrust-backend;
}
}
我现在正在努力使nginx代理通过配置使前端与后端通信,但这无济于事
在部署所有yaml文件而没有任何问题之后,我可以通过服务端口(如192.168.x.x:30812)上的nodeport访问frontedn
用户界面正常,但无法在后端端口8080(springboot应用)上连接api
下面是托管前端Pod上的Nginx日志
10.244.0.0 - - [01/Sep/2019:05:30:04 +0000] "GET /Simple-Line-Icons.0cb0b9c589c0624c9c78.woff2?v=2.4.0 HTTP/1.1" 200 30064 "http://192.168.99.98:30812/styles.c40c8aff0199546fb524.css" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Firefox/68.0" "-"
2019/09/01 05:30:09 [error] 7#7: *4 open() "/usr/share/nginx/html/api/authenticate" failed (2: No such file or directory), client: 10.244.0.0, server: localhost, request: "POST /api/authenticate HTTP/1.1", host: "192.168.99.98:30812", referrer: "http://192.168.99.98:30812/"
10.244.0.0 - - [01/Sep/2019:05:30:09 +0000] "POST /api/authenticate HTTP/1.1" 404 153 "http://192.168.99.98:30812/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Firefox/68.0" "-"
感谢您的帮助,在此问题上花了我太多时间 谢谢