N3evin / AmiiboAPI

A RESTful API for amiibo.
https://www.amiiboapi.com/
MIT License
496 stars 112 forks source link

Trailing Slash issue with ngnix configuration #53

Open N3evin opened 3 years ago

N3evin commented 3 years ago

I will be posting my Nginx configuration to see if anyone can help solve this issue. Seems like it will add a trailing slash when and cause a redirect loop.

server {
    listen 80;
    listen [::]:80;
    server_name amiiboapi.com www.amiiboapi.com;

    location / {
        add_header 'Access-Control-Allow-Origin' '*' always;
        return 301 https://$server_name$request_uri;
    }
}

server {
    listen 443 ssl;
    server_name amiiboapi.com www.amiiboapi.com;

    location /api(.*)$ {
        limit_req zone=mylimit;
        proxy_pass http://client:8080/api$1$is_args$args;
        proxy_redirect     off;
        proxy_set_header   Host $host;
        proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Host $server_name;
    }

    location / {
        proxy_pass http://client:8080;
        proxy_redirect     off;
        proxy_set_header   Host $host;
        proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Host $server_name;
    }

    ssl_certificate /etc/letsencrypt/live/certification.pem;
    ssl_certificate_key /etc/letsencrypt/live/key.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}
Swinkid commented 2 years ago

Could add rewrite ^/(.*)/$ /$1 permanent; to your server block if this is still an issue?

N3evin commented 2 years ago

Could add rewrite ^/(.*)/$ /$1 permanent; to your server block if this is still an issue?

Just tried that, seems to not work. Kinda cause the site to crash too.

Below are the 2 different methods I tried with the rewrite.

  1. Added on both server blocks.
  2. Added only to 443 listener.
AbandonedCart commented 1 year ago

Here are the full details of that solution

and here is a method for working with and without a trailing slash

N3evin commented 1 year ago

with and without a trailing slash

Had tried the 1st method, above comments :) But will look into the 2nd method. Thank you