Quantcast
Channel: Nginx Forum - How to...
Viewing all articles
Browse latest Browse all 4759

How do I remove trailing slashes from domains I pass_proxy using nginx rewrite statements?

$
0
0
I want to set pass_proxy in nginx as follows. (I used rewrite statem.)


example.com/api/ -> example.com/api

exmaple.com/api ->example.com/api


Below is my code.
==============================================
server {
listen 80;
server_name example.com;


location / {
root html;
index index.html index.htm;
}

location /api {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header HOST $host;
proxy_set_header X-NginX-Proxy true;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

rewrite ^/api(.*)$ $1?$args break;
# rewrite ^/api(.*)/$ /$1 break;
# rewrite ^/(.*)/$ /$1 permanent;

proxy_pass http://api.com;
proxy_redirect off;


}
}
========================================



Both should be moved to the site set in pass_proxy.

but, makes it http://example.com/api/ via 301 redirect.

I want /api, without trailing slash to go to another location.



Here's what I tried:

rewrite ^/(.*)/$ /$1 permanent;



In the following document, when proxy_pass is used,
it is said that a slash is automatically added at the end, but is it impossible to get rid of this?
https://nginx.org/en/docs/http/ngx_http_core_module.html#location

Viewing all articles
Browse latest Browse all 4759

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>