Nginx - 反向代理所有以关键字开头的请求

Nginx - Reverse proxy all requests starting with a keyword

我想将所有以“mydomain/_dash”开头的请求重定向到“mydomain:8050/_dash”,以便“mydomain/_dash-component-suites/”重定向到“mydomain:8050” /_破折号-component-suites/"。我添加了以下指令,但它不起作用。另外,我还想维护每个请求的headers。

location /_dash(.*)$ {
    proxy_set_header Host $http_host;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Scheme $scheme;

    client_max_body_size 0;
    
    proxy_pass http://analytics:8050/_dash(*);
  }

您需要使用正则表达式:

location ~ /_dash(.*)$ {
    proxy_set_header Host $http_host;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Scheme $scheme;

    client_max_body_size 0;
    
    proxy_pass http://analytics:8050/_dash;
  }