使用 Nginx API 网关访问微服务时出现 {“status”:400,“message”:“Bad request”}

使用 Nginx API 网关访问微服务时出现 {“status”:400,“message”:“Bad request”}

你好,我正在尝试通过 Nginx API 网关使用 api:我有这个 url:example.com/api/nodeapp。在我的设置中,我有一个在 .com 上对外公开的 Ingress 控制器,它接受请求执行 oauth 并重定向到内部 nginx pod(在私有集群中公开为 Cluster IP 服务)这个内部 nginx pod 将重新路由到目标微服务 pod(也在同一私有集群中公开为 Cluster IP 服务)。目标:我浏览 url 作为 example.com/api/nodeapp ---> Ingress 将执行 oauth 并将请求重定向到 Nginx(这很正常)Nginx 接受我的请求并将我重定向到目标 nodeapp 微服务(出现错误 400)我的代码片段:

入口文件

spec:
  rules:
    - host: example.com
      http:
        paths:
          - path: /api/nodeapp
            backend:
              serviceName: nginx-internal-service
              servicePort: 80
          - path: /api/tea
            backend:
              serviceName: nginx-internal-service
              servicePort: 80

Nginx配置文件

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log debug;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;
    include /etc/nginx/api_gateway.conf;
    include /etc/nginx/conf.d/*.conf;
}

api_gateway.conf

include api_backends.conf;
#include api_keys.conf;

server {
    access_log /var/log/nginx/api_access.log main; # Each API may also log to a separate file

    listen 80; # TLS config goes here (for production use)
    server_name example.com;

    # TLS config
    #ssl_certificate      /etc/ssl/certs/api.example.com.crt;
    #ssl_certificate_key  /etc/ssl/private/api.example.com.key;
    #ssl_session_cache    shared:SSL:10m;
    #ssl_session_timeout  5m;
    #ssl_ciphers          HIGH:!aNULL:!MD5;
    #ssl_protocols        TLSv1.2 TLSv1.3;
   # Invalid resource
    location / {
        rewrite ^ https://$host$request_uri permanent;
    }
    # API definitions, one per file
    include api_conf.d/*.conf;

    # Error responses
    error_page 404 = @400;         # Invalid paths are treated as bad requests
    location @400 {
        return 400 '{"status":400,"message":"Bad request"}\n';
    }
    proxy_intercept_errors on;     # Do not send backend errors to the client
    include errors_json.conf;  # API client friendly JSON error responses
    default_type application/json; # If no content-type then assume JSON
}

nodeapi_simple.conf

# Node API
#
location /api/ {
    # Policy configuration here (authentication, rate limiting, logging, more...)
    #
    access_log /var/log/nginx/nodeapp_api.log main;

    # URI routing
    #
    location /api/tea {
        proxy_pass http://tea;
    }

    location /api/nodeapp {
        proxy_pass http://nodeapp;
    }

    return 404; # Catch-all
}

# vim: syntax=nginx

api_backend.conf

upstream nodeapp {
    zone nodeapp 64k;
    server nodeapp IP:8000;
    
    
}


upstream tea {
    zone tea 64k;
    server Tea app IP:80;
    
    
}



# vim: syntax=nginx

我在这里主要关注的是运行 example.com/api/nodeapp,但它无法运行,尽管我已经在 localhost 上测试过 nodeapp,它运行良好。同样奇怪的是,这个 tea 应用程序只是一个基于文本的示例应用程序,当我在浏览器中输入 URL example.com/api/tea 时,它运行了[[1]: https://i.stack.imgur.com/3fP3k.png][1]s,如下图所示:

以下是我的日志[1]:https://i.stack.imgur.com/jssnb.png][1] 我已经在这个错误上呆了 3 天了,因此请求大家尽快帮我解决这个问题

相关内容