TrinityTuts Tips

Ignore or Exclude specific files or complete folder in NGINX

In this post, I will show you how we can Ignore or exclude specific files or folder from NGINX configuration.

Prerequisites

Before we start updating the configuration file I hope you have good knowledge of the NGINX server because if something goes wrong in the config file your site goes down, and to update configuration file you need complete access of server.

Steps to update the NGINX Configuration file

Step 1. Login to the server via terminal or putty.

Step 2. Go to cd /etc/nginx location and open you site configuration file nano xyz.mysite.com.

Step 3. Open file is open for the edit you can ready to change the setting. Now if you only want to allow specific files to ignore then you can change setting like this.

location / {
	rewrite ^/((?!manifest.json|pwabuildersw.js).*)$ /index.php?slug=$1 last;
}

In my case, I want to ignore the manifest.json file and pwabuildersw.js file so I can update my configuration as shown in the above code.

Step 4. If you want to ignore a specific folder you can do like this.

location /assets {
  # Nothing here
}

In my case, I want to ignore assets folder.

Step 5. If you want to ignore both folder and specific file then you can do like this as shown in the below code.

server {
	listen 80;
	listen [::]:80;

	root /var/www/html/test/sample;

	# Add index.php to the list if you are using PHP
	index index.php index.html index.htm index.nginx-debian.html;

	server_name MY_SERVER_ADDRESS;

	location /assets {
        # Nothing here
    }

	location / {
		rewrite ^/((?!manifest.json|pwabuildersw.js).*)$ /index.php?slug=$1 last;
	}

	# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
	#
	location ~ \.php$ {
		include snippets/fastcgi-php.conf;
		fastcgi_pass unix:/run/php/php7.2-fpm.sock;
	}

	location ~ /\.ht {
		deny all;
	}
}

Step 6. Save your file by pressing ctrl+o and exit ctrl+x.

Step 7. Reload your NGINX configuration sudo systemctl reload nginx.

That’s it now you can check your site. I hope you understand how to Ignore specific files and folders in NGINX server.