简体   繁体   中英

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?

Use:

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

See this document for details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM