Add slash to the end of every url (need rewrite rule for nginx)

前端 未结 9 844
慢半拍i
慢半拍i 2020-11-28 03:28

I try to get an \"/\" to every urls end:

example.com/art

should

example.com/art/

I use nginx as webserver.

I need the rewrite rule f

9条回答
  •  执笔经年
    2020-11-28 03:42

    If nginx behind proxy with https, this snippet do correct redirect for $scheme

    map $http_x_forwarded_proto $upstream_scheme {
        "https" "https";
        default "http";
    }
    
    server {
        ...
        location / {
            rewrite ^([^.\?]*[^/])$ $upstream_scheme://$http_host$1/ permanent;
        }
        ...
    }
    

    And on the upstream proxy pass the X-Forwarded-Proto header like:

    location / {
        proxy_set_header X-Forwarded-Proto $scheme;
        ...
    }
    

提交回复
热议问题