NGINX is a powerful web server used by many websites, apps and blogs. It is scalable and supports heavy traffic. When you run your website on NGINX, you may notice that every requested URL is accessible whether or not you add a trailing slash to it. In both cases, NGINX will return the same response. This can be confusing and cause a few problems mentioned below. In this article, we will learn how to remove trailing slash in NGINX server.
Why Remove Trailing Slash in NGINX
There are several reasons why you may need to remove trailing slash in URLs of your website. Let us look at some of the main ones.
1. Duplicate Content: By default, search engines consider a URL with and without trailing slash as separate URLs, and crawl them separately. When they discover that both these URLs display the same content, then it is very likely that these URLs are seen as duplicates even though they are the same URL.
2. Link Equity: Some backlinks may point to the URL with trailing slash while others may point to URLs without trailing slash. When both the URLs, with and without slash, are served on your site then the backlink power is split between these two URLs.
3. Crawl Budget: Search engines do not crawl all pages on a website. They crawl only a limited number of pages at a time. It is known as crawl budget. If they end up crawling URLs with and without trailing slash, then it will prevent other important and unique pages from being crawled and indexed.
4. User Experience: Lastly, it is just confusing for users to remember whether your site’s URL has a trailing slash or not. It becomes difficult for them to link to share these URLs with others.
How to Remove Trailing Slash in NGINX
There are several ways to remove trailing slash in NGINX. Let us look at them one by one.
Server-Side Redirects
One of the most common and recommended solutions is to add a permanent 301 redirect from URL with trailing slash to URL without one. It returns a 301 response to search engines indicating the URL has permanently moved. This eliminates the problem of duplicate content.
For this, open NGINX config file using text editor. It is typically located at /etc/nginx/nginx.conf.
$ vi /etc/nginx/nginx.conf
If you use a virtual host, then open its configuration file at /etc/nginx/sites-enabled/
In the server block, add the following line.
rewrite ^/(.*)/$ /$1 permanent;
Let us look at the above command.
^/(.*)/$ – Regular expression that matches any URL with a trailing slash.
/$1 – Format of new URL without trailing slash
permanent – keyword to specify permanent redirection.
Basically, the above directive tells NGINX to permanently redirect any URL that has a trailing slash. Save and close the file. You can add this directive to server, or location blocks. If you add it to a server block, then it will be applicable to all URLs served in the server. If you add it to a location block, then it will be applicable only to the URLs matched by the location block.
server{
...
rewrite ^/(.*)/$ /$1 permanent;
...
}
location / {
...
rewrite ^/(.*)/$ /$1 permanent;
...
}
Test NGINX configuration.
$ sudo nginx -t
Restart NGINX server.
$ sudo service nginx reload
CMS Settings
Popular CMS (Content Management System) allow you to set the permalink format of your web pages, via their admin settings. In this, you can remove the trailing slash thereby removing it from all web pages.
Canonical Tags
Canonical tags help search engines determine the authoritative URL for a web page, in case it is accessible from multiple URLs. In fact, whether your URLs have trailing slashes or not, you must include canonical tag on your web pages. For example, let us say your URL is accessible from https://example.com/about and https://example.com/about/, then you can set a canonical tag as shown below.
<link rel="canonical" href="https://example.com/about" />
Now, search engines know the original URL to be used for search indexing.
Conclusion
In this article, we have learnt why it is important to remove trailing slash from URLs. We have also learnt about the different ways to remove trailing slash in NGINX server. By default, all servers add trailing slashes to URLs. You need to explicitly update its configuration to remove trailing slashes to avoid duplicate content SEO penalty.
Also read:
How to Change Port Number in NGINX
How to Enable CORS in NGINX
How to Increase Request Timeout in NGINX

Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI & software development.