我正在尝试使用nginx和.net核心API在kubernetes上使用反向代理。
当我请求http://localhost:9000/api/message时,我希望发生如下情况:
[Request] --> [nginx](localhost:9000) --> [.net API](internal port 9001)但似乎正在发生的是:
[Request] --> [nginx](localhost:9000)!
Fails because /usr/share/nginx/api/message is not found.显然,nginx未能将请求路由到上游服务器。当我在docker-组合下运行相同的配置时,这是正确的,但是在kubernetes (本地的docker)中失败了。
我对nginx使用了以下configmap:
error_log /dev/stdout info;
events {
worker_connections 2048;
}
http {
access_log /dev/stdout;
upstream web_tier {
server webapi:9001;
}
server {
listen 80;
access_log /dev/stdout;
location / {
proxy_pass http://web_tier;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
location /nginx_status {
stub_status on;
access_log off;
allow all;
}
}
}负载平衡器yaml是:
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: load-balancer
spec:
replicas: 1
template:
metadata:
labels:
app: load-balancer
spec:
containers:
- args:
- nginx
- -g
- daemon off;
env:
- name: NGINX_HOST
value: example.com
- name: NGINX_PORT
value: "80"
image: nginx:1.15.9
name: iac-load-balancer
ports:
- containerPort: 80
volumeMounts:
- mountPath: /var/lib/nginx
readOnly: true
name: vol-config
- mountPath: /tmp/share/nginx/html
readOnly: true
name: vol-html
volumes:
- name: vol-config
configMap:
name: load-balancer-configmap
items:
- key: nginx.conf
path: nginx.conf
- name: vol-html
configMap:
name: load-balancer-configmap
items:
- key: index.html
path: index.html
status: {}
---
apiVersion: v1
kind: Service
metadata:
name: load-balancer
spec:
type: LoadBalancer
ports:
- name: http
port: 9000
targetPort: 80
selector:
app: load-balancer
status:
loadBalancer: {}最后,错误消息是:
2019/04/10 18:47:26 [error] 7#7: *1 open() "/usr/share/nginx/html/api/message" failed (2: No such file or directory), client: 192.168.65.3, server: localhost, request: "GET /api/message HTTP/1.1", host: "localhost:9000",
192.168.65.3 - - [10/Apr/2019:18:47:26 +0000] "GET /api/message HTTP/1.1" 404 555 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36" "-",似乎nginx由于某种原因没有正确读取配置,或者它无法与webapi服务器进行通信,并且默认返回尝试服务静态本地内容(日志中没有任何内容表明存在逗号问题)。
编辑1: i应该包括/nginx_status也没有正确地路由,并且失败时没有找到相同的“/usr/share/nginx/html//nginx_status”。
发布于 2019-04-12 12:35:55
我所理解的是,你们要求的是一个Api,这是404。
http://localhost:9000/api/message
我已经通过创建后端服务作为nodeport来解决这个问题,并且我正在尝试从我的角度应用程序访问api。
下面是我的configure.conf文件,它被原来的nginx配置文件替换
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
server {
listen 5555;
location / {
proxy_pass http://login:5555;
}
}
server {
listen 5554;
location / {
proxy_pass http://dashboard:5554;
}
}在这里,我的外部流量通过端口5554/5555到达服务选择器名称。
在这里,登录和仪表板是我的服务,类型为NodePort
这是我的码头文件
from nginx:1.11-alpine
copy configure.conf /etc/nginx/conf.d/default.conf
copy dockerpoc /usr/share/nginx/html
expose 80
expose 5555
expose 5554
cmd ["nginx","-g","daemon off;"]在这里,我保留了前端服务的类型为LoadBalancer,它将公开一个公共端点,
我在前端给我的后端Api打电话:
http://loadbalancer-endpoint:5555/login
希望这能帮到你。
发布于 2019-04-10 19:46:01
您能分享一下如何创建configmap吗?验证configmap有一个名为nginx.conf的数据条目。它可能与readOnly标志相关,也许您也可以尝试删除它或更改/etc/nginx/的路径,如停靠图像文档中所述。
https://stackoverflow.com/questions/55619585
复制相似问题