首页 > 解决方案 > 如何使用 nginx 将 url 参数提供给 proxy_pass?

问题描述

我有一个正在运行的节点前端服务器和正在运行的localhost:3000快速服务器localhost:5000

/我的配置位置

proxy_pass http://localhost:3000; #whatever port your app runs on
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;
                proxy_cache_bypass $http_upgrade;

localhost:5000当我向我发送请求时(failed)net::ERR_CONNECTION_REFUSED出现错误,我认为 Nginx 阻止我执行请求

所以我想给出一个/api位置,将 URL 参数/api/放在后面并将其放在前面localhost:5000

例如:当我去domain.com/api/admin/news请求将被发送到localhost:5000/api/admin/news

我正在尝试使用此配置来解决此问题,但无法使其正常工作

location /~* ^/api/(.*){
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $host;
    proxy_http_version 1.1;
    proxy_pass http://localhost:5000/$1$is_args$args; 
}

任何帮助将不胜感激,谢谢

标签: node.jsnginx

解决方案


推荐阅读