Welcome! Log In Create A New Profile

Advanced

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

Posted by starnge_proxy 
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



Edited 1 time(s). Last edit at 12/07/2022 11:18PM by starnge_proxy.
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 238
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready