Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The configuration thought and process of Nginx and PHP

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "the configuration idea and process of Nginx and PHP". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "the configuration ideas and process of Nginx and PHP".

How to configure Nginx+PHP correctly

Suppose we implement a front-end controller in PHP, or to put it bluntly, a unified entry: send PHP requests to the same file, and then route by parsing "REQUEST_URI" in this file.

It is generally configured like this.

At this point, many tutorials will teach you to configure Nginx+PHP like this:

Server {listen 80; server_name foo.com; root / path; location / {index index.html index.htm index.php; if (!-e $request_filename) {rewrite. / index.php last;}} location ~ / .php$ {include fastcgi_params; fastcgi_param SCRIPT_FILENAME / path$fastcgi_script_name; fastcgi_pass 127.0.0.1 php$ 9000; fastcgi_index index.php;}}

There are a lot of mistakes, or at least bad smells, let's see how many can be found.

It is necessary to first look at the inheritance of instructions in the Nginx configuration file:

The Nginx configuration file is divided into many blocks, and the common ones from outside to inside are "http", "server", "location", and so on. The default inheritance relationship is from outside to inside, that is, the inner block automatically takes the value of the outer block as the default.

Let's start with the "index" instruction.

In the problem configuration, it is defined in "location":

Location / {index index.html index.htm index.php;}

Once a new "location" needs to be added in the future, there will inevitably be repeatedly defined "index" directives. This is because multiple "location" are level relations and there is no inheritance. At this time, "index" should be defined in "server". With the help of inheritance relations, "index" instructions can take effect in all "location".

Next, take a look at the "if" instruction.

It is no exaggeration to say that it is the most misunderstood Nginx instruction:

If (!-e $request_filename) {rewrite. / index.php last;}

Many people like to do a series of checks with the "if" directive, but this is actually the responsibility of the "try_files" directive:

Try_files $uri $uri/ / index.php

In addition, beginners tend to think of the "if" instruction as a kernel-level instruction, but it is actually part of the rewrite module, plus the Nginx configuration is actually declarative rather than procedural, so when it is mixed with instructions from non-rewrite modules, the result may not be what you want.

Let's look at the "fastcgi_params" configuration file.

Include fastcgi_params

Nginx has two fastcgi configuration files, "fastcgi_params" and "fastcgi.conf", which are not very different, except that the latter has one more line of "SCRIPT_FILENAME" definition than the former:

Fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name

Note: there is no / between $document_root and $fastcgi_script_name.

Originally, Nginx only had "fastcgi_params". Later, it was found that many people used hard coding when defining "SCRIPT_FILENAME", so "fastcgi.conf" was introduced in order to standardize usage.

However, this raises a question: why is it necessary to introduce a new configuration file instead of modifying the old one? This is because the "fastcgi_param" instruction is an array, the same as the normal instruction: the inner layer replaces the outer layer; unlike the ordinary instruction, when used multiple times at the same level, it is added rather than replaced. In other words, if "SCRIPT_FILENAME" is defined twice at the same level, they will both be sent to the back end, which can lead to some potential problems, and to avoid this situation, a new configuration file has been introduced.

In addition, we need to consider a security issue: when PHP turns on "cgi.fix_pathinfo", PHP may parse the wrong file type as a PHP file. If Nginx and PHP are installed on the same server, the easiest solution is to filter once with the "try_files" directive:

Try_files $uri = 404

The improved version

According to the previous analysis, is an improved version much more refreshing than the original version:

Server {listen 80; server_name foo.com; root / path; index index.html index.htm index.php; location / {try_files $uri $uri/ / index.php$is_args$args;} location ~ / .php$ {try_files $uri = 404; include fastcgi.conf; fastcgi_pass 127.0.0.1 path; index index.html index.htm index.php; location 9000 }} at this point, I believe you have a deeper understanding of "the configuration ideas and process of Nginx and PHP". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report