In web servers, we often encounter situations where specific files or directories cannot be found. At this time, we often use index.html or index.php as the default file by default to solve this problem. In Nginx, index.php is one of the default files.
However, sometimes we don’t want to display index.php in the URL. This has certain significance in beautifying the URL or improving website security. So, how to cancel index.php in Nginx?
1. Modify the Nginx configuration file
If you have installed Nginx and want to cancel index.php, we need to modify the Nginx configuration file. In the Ubuntu system, the path of the Nginx configuration file is "/etc/nginx/sites-available/default".
Find the "index" directive in the file, that is:
index index.html index.htm index.nginx-debian.html;
We need to delete the "index.php" in it and change it to:
index index.html index.htm index.nginx-debian.html;
Or you want Let index.php come to the back, then you can place it at the end:
index index.html index.htm index.nginx-debian.html index.php
2. Configure Nginx virtual host
In Nginx, each website will have a virtual host . A virtual host can provide an independent environment for each different website so that different functions can be set for each website. Here we need to configure and cancel index.php for each virtual host.
1. Open the Nginx virtual host configuration file
sudo nano /etc/nginx/sites-available/default
2. Cancel the index.php in the virtual host
server { listen 80 default_server; listen [::]:80 default_server; root /var/www/html; # 列出默认文件 index index.html index.htm; # 取消index.php,即移除它 location / { try_files $uri $uri/ =404; } # 处理PHP脚本 location ~ .php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php7.4-fpm.sock; } # 处理网站资源 location ~* .(js|css|png|jpg|jpeg|gif|ico)$ { expires 30d; add_header Cache-Control "public, no-transform"; } # 处理网站图标 location /favicon.ico { access_log off; log_not_found off; } }
In the above configuration file, we canceled the index .php and set other default files. At the same time, in the location block, we use the try_files directive to verify whether the URL exists. If it does not exist, a 404 error will be returned.
3. Modify nginx.conf appropriately
In Nginx, you can also cancel the display of index.php by modifying nginx.conf. We need to add a directive in the http block:
http { ... # 将index.php从默认文件列表中删除 index index.html index.htm; # 取消index.php location / { try_files $uri $uri/ /index.php?$query_string; } ... }
In the above configuration, we verify whether the URL exists by removing index.php from the default file list and then pass the try_files directive in the location block, if not If it exists, use the "index.php" file.
4. Cancel index.php through .htaccess
If you are using Nginx as the web server and it does not support .htaccess files, then you can use a similar file: .user. ini. .user.ini is a htaccess file that only supports PHP. Through it, you can set PHP configuration information. Just store the following file as .user.ini:
; PHP settings: cgi.fix_pathinfo = 0 cgi.fix_pathinfo = false cgi.fix_pathinfo = off ; nginx settings: try_files $uri $uri/ /index.php?$query_string; ; User-defined settings: index index.php index.html index.htm
Among them, we verify whether the URL exists by trying the file instruction try_files. If it does not exist, use the "index.php" file. At the same time, we set other default files.
5. Summary
This article introduces four possible methods to cancel index.php in nginx. You can use one or more of these methods to achieve your own goals based on the needs of your own website. Whichever method you use, it's very simple and fast. You can provide a more user-friendly URL for your website to attract more visitors and improve your website's SEO effect.
The above is the detailed content of nginx routing configuration cancel index.php. For more information, please follow other related articles on the PHP Chinese website!

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


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

SublimeText3 English version
Recommended: Win version, supports code prompts!

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

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

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor