nginx - 反向代理不使用 nginx 和 kubernetes 路由到 API
问题描述
我正在尝试使用 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-compose 下运行相同的配置但在 kubernetes 中失败(在 docker 中本地)时,这可以正常工作
我正在为 nginx 使用以下配置映射:
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: 我应该包括 /nginx_status 也没有正确路由并且失败并出现相同的“/usr/share/nginx/html/nginx_status”未找到错误。
解决方案
我在这里理解的是你正在请求一个 Api,它给出了 404。
我已经通过将后端服务创建为 nodeport 解决了这个问题,并且我正在尝试从我的 Angular 应用程序访问 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 的外部流量路由到服务 [selector-Name]
这里登录和仪表板是我的服务类型为 NodePort
这是我的 Docker 文件
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 为:
希望这会帮助你。
推荐阅读
- html - 我可以用 CSS 制作这个形状吗?
- linux - 使用多线程分叉会使子进程中的堆栈内存无法访问
- javascript - 悬停在 td 上并添加一个类到 span
- android - 如何从 Kotlin 的内部类访问外部类的成员?
- r - 在函数中创建新的、依赖于变量名的列(以指示表达式数据中的重要性级别)
- sql - 是否可以在 SQL 中存储一个变量(int),然后在到达某个日期后生效?
- android - Locale.getDefault().getCountry() 返回空字符串
- python - 如何在阈值距离后获得新的点列表?
- freemarker - 使用 Freemarker 将字符串转换为数组
- html - 如何在图像缩放后修复父元素大小?