如何使用Nginx将url参数提供给proxy_pass?

时间:2020-07-02 15:25:32

标签: node.js nginx

我有一个在localhost:3000上工作的节点前端服务器和一个在localhost:5000上工作的Express服务器

/位置,我的配置是

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位置,该位置将在/api/之后使用URL参数,并将其放在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; 
}

任何帮助将非常感谢,谢谢

0 个答案:

没有答案
相关问题