Nginx路由-如何使用多子路由在不基于基本URL的情况下代理服务器/ API?

时间:2019-04-28 17:59:42

标签: http nginx proxy routing

我有以下问题。我想在此设置中访问API端点。蛋。

  

localhost:9000 / api / links

但是,当键入这样的请求时,nginx重定向了请求,我最终在这里

  

localhost:9000 /链接

当我使用尾随破折号时,不会发生这种情况。

  

localhost:9000 / api / links /

# Upstream are linked to the docker-compose services
# and must match their name and port
upstream api {
    server api:9000;
}
upstream app {
    server app:80;
}
upstream dashboard {
    server dashboard:80;
}

server { # PROXY SERVER
    listen 9000;

    # the proxy server handles the traffic on
    # the docker-compose network

    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 / {
        root /usr/share/nginx/html;
        index index.html index.htm;
    }

    # This "hack" is required because normally
    # it would be the root URI "/"
    # but since we handle these as routes on a single
    # server, we have to mess a little with that routing
    location = /app { return 302 /cactus/;}
    location /app/ {
        proxy_pass http://app/;  # note the trailing slash
    }

    location /dashboard {  # the issue with the routing is handled in vue.config.js
        auth_basic "Restricted";
        auth_basic_user_file /etc/nginx/.htpasswd;
        proxy_pass http://dashboard/;
    }

    location = /api { return 302 /api/;}
    location /api {
        proxy_pass http://api;    # how can I request api/links without
                                   # trailing forward slash?( e.g.g. api/links/ <--  )   
   }

我尝试了不同的组合,但无济于事。我不想像预期的那样开始混乱API,因为它的功能正常。

0 个答案:

没有答案