Nginx+php often need to use pseudo static, generally we are manually set. Is there any way to make nginx automatically fill the full path?
These two days have been a long time to achieve such a function:
Request/A/B/C
If the file does not exist, look for/a/b/index.php,/c as Path_info;
If the file does not exist, look for/a/index.php,/b/c as Path_info;
If the file does not exist, look for/index.php,/a/b/c as Path_info;
Returns 404 if the file does not exist.
While this loss-performance behavior is not suitable for deployment, it can be handy in the case of native debugging:)
The server side should have the following code, the other parts using their own configuration:
index index.php index.html index.htm;
Location/{set $path $request _uri;
Set $path _info "";
Try_files $uri $uri/@404;
} location @404 {if ($path ~ ^ (. *) (/.+) $) {set $path $1/index.php;
Set $path _info $;
Rewrite. * $path last;
return 404;
} location ~. +.php ($|/) {fastcgi_split_path_info ^ (. +.php) (/.+) $;
if ($path _info!~. *) {set $path _info $fastcgi _path_info;
Try_files $fastcgi _script_name @404php;
Fastcgi_param path_info $path _info;
Fastcgi_index index.php;
Include fastcgi.conf;
Fastcgi_pass Unix:/usr/local/var/run/php-fpm.sock;
Fastcgi_connect_timeout 60;
Fastcgi_send_timeout 300;
Fastcgi_read_timeout 300;
} location @404php {if ($path =/index.php) {return 404;
} if ($path ~ ^ (. *) (/.+)/index.php$) {set $path _info $;
Set $path $1/index.php;
Rewrite. * $path last;
return 404; }
The flags of the rewrite
last-basically all with this flag.
Break-Abort Rewirte, no continue match
Redirect-returns the temporarily redirected HTTP status 302
Permanent-Returns HTTP status for permanent redirection 301
Rules:
Generally in the location configuration rewrite, are used by the break, and the root of the location use last is better, because if the fastcgi or proxy access to the JSP file, the root location under the break is not access to
Pattern matching in the form of regular expressions, such as ~* and ~
- ' ~ ' indicates a case sensitive match
- ' ~* ' indicates a case-insensitive match (for example, "Firefox" Strings can successfully match "Firefox")
- The!~ and!~* represent the opposite of the regular matching rule, representing the following processing statements that cannot match the string execution of the current regular expression rule
- Use the-f parameter and the!-f parameter to detect whether a file exists
- Use the-d parameter and the!-d parameter to detect whether a directory (path) exists
- Use-E and!-e to detect whether a file exists, a directory, or a symbolic link.
- Use-X and!-x to detect whether a file is executable
- Part of the regular expression can be nested, and the part following the expression can be represented by a $ variable if the preceding expression is used.