Table of Contents
Nginx security configuration: Only access to index.php files is allowed
Scenarios and requirements
Configuration plan
Home Backend Development PHP Tutorial How to configure only allow access to index.php files in Nginx?

How to configure only allow access to index.php files in Nginx?

Apr 01, 2025 am 07:57 AM
css nginx

How to configure only allow access to index.php files in Nginx?

Nginx security configuration: Only access to index.php files is allowed

This article describes how to configure Nginx, only allow access to index.php files, and deny access to all other files or specific PHP files. This enhances server security and prevents unauthorized access.

Scenarios and requirements

Assuming that there are multiple PHP files (such as index.php and test.php ) in the server directory, we need to make sure that only index.php is accessible and other PHP files and other resources are blocked.

Configuration plan

We will provide two configuration solutions to meet different needs:

Scheme 1: Only access to /index.php is allowed, all other requests are denied

This solution is the most stringent, and any other requests except /index.php will be denied. This is suitable for scenarios with extremely high safety requirements.

 server {
    listen 80;
    server_name 192.168.16.86;
    root /home/wwwroot/web;

    include enable-php.conf;

    location = /index.php {
        # Process index.php request try_files $uri $uri/ /index.php?$query_string;
    }

    location / {
        deny all;
    }

    # ... Other location blocks (such as static resource processing) can be retained or removed as needed...
}
Copy after login

Solution 2: Allow access to /index.php and static resources, and reject other PHP files

This solution allows access to static resources (such as pictures, CSS, JS, etc.), and only allows access to index.php , denying access to other PHP files. This is more common in practical applications.

 server {
    listen 80;
    server_name 192.168.16.86;
    root /home/wwwroot/web;

    include enable-php.conf;

    location / {
        # Handle static resource request try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        deny all;
    }

    location = /index.php {
        # Process index.php request try_files $uri $uri/ /index.php?$query_string;
    }

    # ... Other location blocks (such as static resource cache) can be retained or adjusted as needed...

}
Copy after login

Configuration instructions:

  • location = /index.php : Exactly matches the /index.php path and only handles requests to the file.
  • location ~ \.php$ : Use regular expressions to match all files ending in .php .
  • deny all : All requests are rejected.
  • try_files : Try to find a file or directory, and perform subsequent operations if it is not found.

Which option you choose depends on your specific security needs. Solution 1 is more secure, but the restrictions are stricter; Solution 2 is both security and functionality. Please select and adjust the configuration according to the actual situation. Be sure to test the configuration to make sure it meets your expectations. Remember, safe configuration needs to be cautious and it is recommended to test it in a test environment before applying it to the production environment.

The above is the detailed content of How to configure only allow access to index.php files in Nginx?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1662
14
PHP Tutorial
1262
29
C# Tutorial
1235
24
HTML: The Structure, CSS: The Style, JavaScript: The Behavior HTML: The Structure, CSS: The Style, JavaScript: The Behavior Apr 18, 2025 am 12:09 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

How to start containers by docker How to start containers by docker Apr 15, 2025 pm 12:27 PM

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

How to check the name of the docker container How to check the name of the docker container Apr 15, 2025 pm 12:21 PM

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

Solve caching issues in Craft CMS: Using wiejeben/craft-laravel-mix plug-in Solve caching issues in Craft CMS: Using wiejeben/craft-laravel-mix plug-in Apr 18, 2025 am 09:24 AM

When developing websites using CraftCMS, you often encounter resource file caching problems, especially when you frequently update CSS and JavaScript files, old versions of files may still be cached by the browser, causing users to not see the latest changes in time. This problem not only affects the user experience, but also increases the difficulty of development and debugging. Recently, I encountered similar troubles in my project, and after some exploration, I found the plugin wiejeben/craft-laravel-mix, which perfectly solved my caching problem.

How to create containers for docker How to create containers for docker Apr 15, 2025 pm 12:18 PM

Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]

How to optimize website performance: Experiences and lessons learned from using the Minify library How to optimize website performance: Experiences and lessons learned from using the Minify library Apr 17, 2025 pm 11:18 PM

In the process of developing a website, improving page loading has always been one of my top priorities. Once, I tried using the Miniify library to compress and merge CSS and JavaScript files in order to improve the performance of the website. However, I encountered many problems and challenges during use, which eventually made me realize that Miniify may no longer be the best choice. Below I will share my experience and how to install and use Minify through Composer.

How to start nginx How to start nginx Apr 14, 2025 pm 01:06 PM

Question: How to start Nginx? Answer: Install Nginx Startup Nginx Verification Nginx Is Nginx Started Explore other startup options Automatically start Nginx

How to generate html by sublime How to generate html by sublime Apr 16, 2025 am 09:03 AM

There are two ways to generate HTML code in Sublime Text: Using the Emmet plugin, you can generate HTML elements by entering an abbreviation and pressing the Tab key, or use a predefined HTML file template that provides basic HTML structure and other features such as code snippets, autocomplete functionality, and Emmet Snippets.

See all articles