If you need to add a new "location" in the future, the "index" command will be repeatedly defined. This is because multiple "locations" are in a hierarchical relationship and there is no inheritance, in this case, define "index" in "server". With the inheritance relationship, the "index" command takes effect in all "locations.
Next let's take a look at the "if" commandIt is the most misunderstood Nginx command:
if (!-e $request_filename) { rewrite . /index.php last;}Many people prefer to use the "if" command for a series of checks, but this is actually the responsibility of the "try_files" command:
try_files $uri $uri/ /index.php;
In addition, beginners often think that the "if" command is a kernel-level command, but it is actually part of the rewrite module, and Nginx configuration is actually declarative, instead of procedural, when it is mixed with the commands of the non-rewrite module, the results may not be as expected.
Let's take a look at the "fastcgi_params" configuration file:
include fastcgi_params;
Nginx has two fastcgi configuration files: "fastcgi_params" and "fastcgi. conf. The only difference is that the latter has a line of "SCRIPT_FILENAME" more 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 encoding when defining "SCRIPT_FILENAME". Therefore, "fastcgi. conf" was introduced for standard usage 」.
In this case, the question is: why should we introduce a new configuration file instead of modifying the old configuration file? This is because the "fastcgi_param" command is of the array type, which is the same as the common command: The inside layer replaces the outer layer; and the common command is different: when it is used multiple times at the same level, is New rather than replacement. In other words, if two "SCRIPT_FILENAME" statements are defined at the same level, they are all sent to the backend, which may cause some potential problems. To avoid such problems, A new configuration file is introduced.
In addition, we also need to consider a security issue: When "cgi. fix_pathinfo" is enabled in PHP, PHP may parse the wrong file type as a PHP file. If Nginx and PHP are installed on the same server, the simplest solution is to use the "try_files" command for filtering:
try_files $uri =404;
According to the previous analysis, a modified version is provided, which is much easier than the previous 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:9000; }}Free to provide the latest Linux technology tutorial books, for open source technology enthusiasts to do more and better: http://www.linuxprobe.com