Ignore or Exclude specific files or complete folder in NGINX

people

Aneh Thakur

. 1 min read

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.


More Stories from

Aneh Thakur
Aneh Thakur.3 min read

Get Started with TypeScript: A Beginner's Guide with Code Samples

TypeScript is a popular, powerful programming language that is a superset of JavaScript. If you're new to TypeScript, this beginner's guide is the perfect place to start. With clear explanations and code samples, you'll learn the basics of TypeScript and

.
Get Started with TypeScript: A Beginner's Guide with Code Samples
Aneh Thakur
Aneh Thakur.2 min read

TypeScript vs TSX: Understanding the Differences

This article delves into the key differences between TypeScript and TSX, two programming languages used for developing complex web applications. From syntax and static typing to React integration and code organization, learn which language is best suited

.
TypeScript vs TSX: Understanding the Differences
Aneh Thakur
Aneh Thakur.2 min read

Learn about the different types of React hooks with code examples

React hooks are a powerful feature that allow you to use state and other React features inside functional components. In this tutorial, you will learn about the different types of hooks available in React, and see code examples of how to use them in your

.
Learn about the different types of React hooks with code examples
Aneh Thakur
Aneh Thakur.2 min read

A step-by-step guide to deploying a ReactJS app online

Learn how to deploy your ReactJS app to the internet with this comprehensive tutorial. From setting up a hosting platform to building and uploading your code, we'll cover all the steps you need to take to get your app live. Whether you're a beginner or an

.
A step-by-step guide to deploying a ReactJS app online
Aneh Thakur
Aneh Thakur.3 min read

What is React context API?

React Context is a way to share values that are considered "global" for a tree of React components, such as the current authenticated user, theme, or preferred language. It allows you to avoid prop drilling, or the passing of props through multiple levels

.
What is React context API?
Built on Koows