First type: Nginx’s own error page
Nginx accesses a static html page. When this page does not exist, Nginx throws 404. So how to return 404 to the client?
Look at the configuration below. In this case, there is no need to modify any parameters to achieve this function.
server { listen 80; server_name www.test.com; root /var/www/test; index index.html index.htm; location / { }# 定义错误页面码,如果出现相应的错误页面码,转发到那里。error_page 404 403 500 502 503 504 /404.html;# 承接上面的location。location = /404.html {# 放错误页面的目录路径。root /usr/share/nginx/html; } }
Second: Reverse proxy error page
If the background Tomcat processing error throws 404, you want to call Nginx to feedback this status to the client or redirect to a certain connection. The configuration is as follows:
upstream www { server 192.168.1.201:7777 weight=20 max_fails=2 fail_timeout=30s; ip_hash; } server { listen 80; server_name www.test.com; root /var/www/test; index index.html index.htm; location / {if ($request_uri ~* ‘^/$’) { rewrite .* http://www.test.com/index.html redirect; }# 关键参数:这个变量开启后,我们才能自定义错误页面,当后端返回404,nginx拦截错误定义错误页面proxy_intercept_errors on; proxy_pass http://www; proxy_set_header HOST $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-FOR $proxy_add_x_forwarded_for; } error_page 404 /404.html; location = /404.html { root /usr/share/nginx/html; } }
Third type: Nginx parses the error page of php code
If the backend is parsed by php, you need to add a variable
Add it in the http section A variable fastcgi_intercept_errors on will do the trick.
Specify an error page:
error_page 404 /404.html; location = /404.html { root /usr/share/nginx/html; } 指定一个url地址: error_page 404 /404.html; error_page 404 = http://www.test.com/error.html;
The above is the detailed content of How to customize exclusive 404 page in Nginx. For more information, please follow other related articles on the PHP Chinese website!

NGINX improves performance through its event-driven architecture and asynchronous processing capabilities, enhances scalability through modular design and flexible configuration, and improves security through SSL/TLS encryption and request rate limiting.

NGINX is suitable for high concurrency and low resource consumption scenarios, while Apache is suitable for scenarios that require complex configurations and functional extensions. 1.NGINX is known for handling large numbers of concurrent connections with high performance. 2. Apache is known for its stability and rich module support. When choosing, it must be decided based on specific needs.

NGINXisessentialformodernwebapplicationsduetoitsrolesasareverseproxy,loadbalancer,andwebserver,offeringhighperformanceandscalability.1)Itactsasareverseproxy,enhancingsecurityandperformancebycachingandloadbalancing.2)NGINXsupportsvariousloadbalancingm

To ensure website security through Nginx, the following steps are required: 1. Create a basic configuration, specify the SSL certificate and private key; 2. Optimize the configuration, enable HTTP/2 and OCSPStapling; 3. Debug common errors, such as certificate path and encryption suite issues; 4. Application performance optimization suggestions, such as using Let'sEncrypt and session multiplexing.

Nginx is a high-performance HTTP and reverse proxy server that is good at handling high concurrent connections. 1) Basic configuration: listen to the port and provide static file services. 2) Advanced configuration: implement reverse proxy and load balancing. 3) Debugging skills: Check the error log and test the configuration file. 4) Performance optimization: Enable Gzip compression and adjust cache policies.

Nginx cache can significantly improve website performance through the following steps: 1) Define the cache area and set the cache path; 2) Configure the cache validity period; 3) Set different cache policies according to different content; 4) Optimize cache storage and load balancing; 5) Monitor and debug cache effects. Through these methods, Nginx cache can reduce back-end server pressure, improve response speed and user experience.

Using DockerCompose can simplify the deployment and management of Nginx, and scaling through DockerSwarm or Kubernetes is a common practice. 1) Use DockerCompose to define and run Nginx containers, 2) implement cluster management and automatic scaling through DockerSwarm or Kubernetes.

The advanced configuration of Nginx can be implemented through server blocks and reverse proxy: 1. Server blocks allow multiple websites to be run in one instance, each block is configured independently. 2. The reverse proxy forwards the request to the backend server to realize load balancing and cache acceleration.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Chinese version
Chinese version, very easy to use

WebStorm Mac version
Useful JavaScript development tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft