


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... }
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... }
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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).

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 configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

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".

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]

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.

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