Home Backend Development PHP Tutorial nginx 404页面处理以及pathInfo和隐藏index.php总述

nginx 404页面处理以及pathInfo和隐藏index.php总述

Jun 23, 2016 pm 01:49 PM
404 index.php nginx hide page

今天开发公司官网:http://www.zstime.com/,遇到一个问题,如何在nginx下设置pathInfo以及如何隐藏index.php

这里分别来讲解一下:


一、隐藏index.php

隐藏index.php需要修改nginx的配置文件,如果你是使用vhost的,需要修改如conf/vhost/你的文件名.conf

文件,整个文件如下

server {        listen       80;        server_name  www.zstime.com;        index index index.html index.htm index.php;        root /alidata/www/ZStimeBro/;        error_page 404 /error.html;        location / {                        if (!-e $request_filename){                        rewrite ^/(.*)$ /index.php/$1 last;                        }                }        location ~ \.php($|/)                {                        fastcgi_pass   127.0.0.1:9000;                        fastcgi_index  index.php;                        fastcgi_split_path_info ^(.+\.php)(.*)$;                        fastcgi_param   PATH_INFO $fastcgi_path_info;                        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;                        include        fastcgi_params;                }                location ~.*\.(gif|jpg|jpeg|png|bmp|swf)$ {                        expires 30d;                }                location ~.*\.(js/css)?$ {                        expires 1h;                }        #α???????        include /alidata/server/nginx/conf/rewrite/default.conf;        access_log  /alidata/log/nginx/access/default.log;}
Copy after login

其中添加了:

location / {                        if (!-e $request_filename){                        rewrite ^/(.*)$ /index.php/$1 last;                        }                }
Copy after login
将请求链接重定向,加上一个index.php,从而实现了去除index.php的效果

二、添加pathInfo支持

nginx本身不支持pathInfo,所有我们需要自己解析URL,将index.php

location ~ \.php($|/)                {                        fastcgi_pass   127.0.0.1:9000;                        fastcgi_index  index.php;                        fastcgi_split_path_info ^(.+\.php)(.*)$;                        fastcgi_param   PATH_INFO $fastcgi_path_info;                        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;                        include        fastcgi_params;                }
Copy after login

注意:添加pathInfo方法不止一种,但是这种方法相对简单。

其中

~ \.php($|/)
Copy after login

是匹配请求路径中包含.php的url。

三、添加404

404页面的添加折腾了一天,首先需要在nginx.conf中开启

fastcgi_intercept_errors on;
Copy after login

切记要开启这一项,不然不能自定义404页面。

之后修改vhost对应的配置文件,添加:

error_page 404 /error.html;
Copy after login
切记不能加“=”号。


如此理论上来说是结束了,但是浏览器中试了一下发现还是不行,始终显示 状态吗200!一天的时间就花在这里!!最后查看了很久,才发现不是配置文件的问题了,而是自己使用的框架的问题,我使用的时broPHP,这个小框架居然没有做页面404处理,好吧,参考了http://www.csdn123.com/html/exception/693/693838_693845_693847.htm,搞定了!


总结:有时候,你觉得出问题的地方可能是安全的,你觉得很安全的地方往往出问题!


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)

How to configure cloud server domain name in nginx How to configure cloud server domain name in nginx Apr 14, 2025 pm 12:18 PM

How to configure an Nginx domain name on a cloud server: Create an A record pointing to the public IP address of the cloud server. Add virtual host blocks in the Nginx configuration file, specifying the listening port, domain name, and website root directory. Restart Nginx to apply the changes. Access the domain name test configuration. Other notes: Install the SSL certificate to enable HTTPS, ensure that the firewall allows port 80 traffic, and wait for DNS resolution to take effect.

How to check whether nginx is started How to check whether nginx is started Apr 14, 2025 pm 01:03 PM

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.

How to check nginx version How to check nginx version Apr 14, 2025 am 11:57 AM

The methods that can query the Nginx version are: use the nginx -v command; view the version directive in the nginx.conf file; open the Nginx error page and view the page title.

How to create a mirror in docker How to create a mirror in docker Apr 15, 2025 am 11:27 AM

Steps to create a Docker image: Write a Dockerfile that contains the build instructions. Build the image in the terminal, using the docker build command. Tag the image and assign names and tags using the docker tag command.

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

How to start nginx server How to start nginx server Apr 14, 2025 pm 12:27 PM

Starting an Nginx server requires different steps according to different operating systems: Linux/Unix system: Install the Nginx package (for example, using apt-get or yum). Use systemctl to start an Nginx service (for example, sudo systemctl start nginx). Windows system: Download and install Windows binary files. Start Nginx using the nginx.exe executable (for example, nginx.exe -c conf\nginx.conf). No matter which operating system you use, you can access the server IP

How to run nginx apache How to run nginx apache Apr 14, 2025 pm 12:33 PM

To get Nginx to run Apache, you need to: 1. Install Nginx and Apache; 2. Configure the Nginx agent; 3. Start Nginx and Apache; 4. Test the configuration to ensure that you can see Apache content after accessing the domain name. In addition, you need to pay attention to other matters such as port number matching, virtual host configuration, and SSL/TLS settings.

How to check whether nginx is started? How to check whether nginx is started? Apr 14, 2025 pm 12:48 PM

In Linux, use the following command to check whether Nginx is started: systemctl status nginx judges based on the command output: If "Active: active (running)" is displayed, Nginx is started. If "Active: inactive (dead)" is displayed, Nginx is stopped.

See all articles