P
P
part_os2020-02-07 21:51:02
Nginx
part_os, 2020-02-07 21:51:02

How to configure nginx to not have a trailing slash?

hello, tell me please, I can’t figure out the nginx config, I seem to have made a redirect with a trailing slash, but all search engines consider the site a duplicate. https://photo-gileva-olga.ru/ and not https://photo-gileva-olga.ru, when I copy the url from the browser, it also adds a slash at the end

server {
    server_name www.photo-gileva-olga.ru photo-gileva-olga.ru;
    return 301 https://photo-gileva-olga.ru$request_uri;
}

server {
    listen 443 ssl;
    server_name www.photo-gileva-olga.ru;
    ssl_certificate /etc/nginx/ssl/photo-gileva-olga.ru.crt;
    ssl_certificate_key  /etc/nginx/ssl/photo-gileva-olga.ru.key;
    return 301 https://photo-gileva-olga.ru$request_uri;
}

server {
    listen 80;
    listen 443 ssl;
    server_name photo-gileva-olga.ru;
    root /var/www/photo-gileva-olga/public;

    ssl_certificate /etc/nginx/ssl/photo-gileva-olga.ru.crt;
    ssl_certificate_key  /etc/nginx/ssl/photo-gileva-olga.ru.key;

    ssl_session_cache shared:SSL:100m;
    ssl_session_timeout 60m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

    gzip on;
    gzip_disable "msie6";
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript;

    client_max_body_size 40m;

    if (!-d $request_filename) {
        rewrite     ^/(.+)/$ /$1 permanent;
    }

    if ($request_uri ~* "^(.*/)index\.php$") { # делаем редирект с index.php на корень сайта
             return 301 $1;
    }

    if ($request_uri ~ ^(?P<left>[^?]*?)//+(?P<right>[^?]*)) {
        rewrite ^ $left/$right permanent;
    }

    location / {
        try_files $uri /index.php$is_args$args;
    }

    location ~ ^/index\.php(/|$) {
        fastcgi_pass php:9000;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
        internal;
    }

    location ~ \.php$ {
        return 404;
    }

    location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
                    access_log off;
                    log_not_found off;
                    expires max; # кеширование статики
    }

    error_log /var/log/nginx/photo-gileva-olga_error.log;
    access_log /var/log/nginx/photo-gileva-olga_access.log;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vreitech, 2020-02-08
@part_os

slash in https://example.com/ or any other site - the root path in the URL, it cannot be removed.
FYI: https://en.wikipedia.org/wiki/URL#URL_Structure

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question