Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nginx: remove path from proxy_pass on named location

I have this nginx config to serve a rails app:

  location ^~ /api/ {
    alias /srv/www/rails/public/;
    try_files $uri @unicorn;
  }

  location @unicorn {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://127.0.0.1:2007;
  }

I want to remove /api/ from the start of path before passing it to rails app, but since it's a named location I can't add "/" at the end of proxy_pass directive, how can I remove /api/ before passing request to rails?

like image 531
Lluís Avatar asked Jan 28 '26 02:01

Lluís


1 Answers

Use:

location @unicorn {
    rewrite ^/api(.*)$ $1 break;
    ...
}

See this document for details.

like image 76
Richard Smith Avatar answered Jan 30 '26 20:01

Richard Smith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!