I. Understanding Nginx rewrite:
Regular expression matching, where:
* ~ For case-sensitive matching
* ~* for case-insensitive matching
*!~ and!~* are case insensitive and case insensitive
File and directory matching, where:
*-F and!-f to determine if a file exists
*-D and!-d to determine if a directory exists
*-E and!-e to determine if a file or directory exists
*-X and!-x to determine if the file is executable
Flag flags are:
* Last equivalent to the [L] mark in Apache, indicating completion of rewrite
* Break terminates the match and no longer matches the following rule
* REDIRECT Returns 302 the temporary redirect Address bar displays the address after the jump
* Permanent return 301 Permanent redirect Address bar will show the address after the jump
Some of the available global variables can be used as conditional judgments (to be complete)
$args
$content _length
$content _type
$document _root
$document _uri
$host
$http _user_agent
$http _cookie
$limit _rate
$request _body_file
$request _method
$remote _addr
$remote _port
$remote _user
$request _filename
$request _uri
$query _string
$scheme
$server _protocol
$server _addr
$server _name
$server _port
$uri
Instance:
1. File anti-hotlinking and set expiration time
The return 412 here is a custom HTTP status code with a default of 403, which makes it easy to find the correct hotlinking request.
"Rewrite ^/http://http://374400.blog.51cto.com/addblog.jpg;" Display an anti-theft chain picture
"Access_log off;" Reduce stress by not logging access logs
"Expires 3d" All files 3-day browser cache
Location ~* ^.+/. (JPG|JPEG|GIF|PNG|SWF|RAR|ZIP|CSS|JS) $ {
Valid_referers none blocked *.c1gstudio.com *.c1gstudio.net localhost 208.97.167.194;
if ($invalid _referer) {
Rewrite ^/http://374400.blog.51cto.com/addblog.jpg;
return 412;
Break
}
Access_log off;
Root/opt/htdocs/web;
Expires 3d;
Break
}
2. Only allow fixed IP access to the website, plus password
root/opt/htdocs/www;
Allow 208.97.167.194;
Allow 222.33.1.2;
Allow 231.152.49.4;
Deny all;
Auth_basic "C1g_admin";
Auth_basic_user_file htpasswd;
3. Redirects when files and directories do not exist:
if (!-e $request _filename) {
Proxy_pass http://127.0.0.1;
}
Two. Nginx Location matching sequence
The prototype for location matching is this: location [=|~|~*|^~|@]/uri/{...}
"=" is an exact match
"@" is a named location that is not used in a normal location match and is used only in internal jumps.
"~" is a case-sensitive match
"~*" is a case-insensitive match
"^~" means abort regular match (this is not very noticeable at ordinary times)
Matching steps:
1. Advanced line matching with "=" prefix, if found, aborts the lookup.
2. All other location for non-regular matching, find the most exact match, if matched to the "^~" prefix, then abort the lookup.
3. Regular lookups, which are located in the location order configured in our configuration file.
4. If the regular lookup match succeeds, use this regular matching location, otherwise, use the result of the second step lookup.
The difference between "=" and "^~" should be explained in particular here:
"=" matches the location with "=" when matching. "^~", however, matches all non-regular location of non-"=" and only takes effect after confirming that it is the most exact match.
Three. CI Framework pseudo-static
CI in APACHE. htaccess file Configuration
Rewriteengine on
Rewritecond $!^ (index\.php|system\.php|images|js|swfupload|robots\.txt)
Rewriterule ^ (. *) $/wj/index.php/$1 [L]
Note: rewriterule ^ (. *) $/wj/index.php/$1 [L] in WJ is your CI program directory
Nginx Server pseudo-static settings first need to set Nginx open Path_info,nginx mode default is not supported Path_info mode
1. PathInfo, a pseudo-static usage,
1.1. Let Apache support PathInfo (Apache version: 2.2.13)
In the configuration file, add
<files *.php>
Acceptpathinfo on
</Files>
This allows Apache to support PathInfo for PHP files.
1.2, PathInfo mode needs php.ini to open the following parameter
Cgi.fix_pathinfo=1
Path_info mode: http://www.xxx.com/index.php/Module/method
1.3, let Nginx support PathInfo
Add in config file
Location ~ \.php {
Fastcgi_pass 127.0.0.1:9000;
Fastcgi_index index.php;
Set $path _info "";
Set $real _script_name $fastcgi _script_name;
if ($fastcgi _script_name ~ "^ (. +?\.php) (/.+) $") {
Set $real _script_name $;
Set $path _info;
}
}
Fastcgi_param script_filename $document _root$real_script_name;
Fastcgi_param script_name $real _script_name;
Fastcgi_param path_info $path _info;
Include conf/fastcgi.conf;
}
Points:
1.~ \.php cannot have $ in order to match URLs in all *.php/* forms
2. Change Script_filename by setting
Cat fastcgi.conf
#fastcgi_param script_filename $document _root$fastcgi_script_name; #注释掉的
Fastcgi_param query_string $query _string;
Fastcgi_param Request_method $request _method;
Fastcgi_param Content_Type $content _type;
Fastcgi_param content_length $content _length;
#fastcgi_param script_name $fastcgi _script_name; #注释掉的
Fastcgi_param Request_uri $request _uri;
Fastcgi_param Document_uri $document _uri;
Fastcgi_param document_root $document _root;
Fastcgi_param server_protocol $server _protocol;
Fastcgi_param HTTPS $https If_not_empty;
Fastcgi_param Gateway_interface cgi/1.1;
Fastcgi_param server_software nginx/$nginx _version;
Fastcgi_param remote_addr $remote _addr;
Fastcgi_param Remote_port $remote _port;
Fastcgi_param server_addr $server _addr;
Fastcgi_param server_port $server _port;
Fastcgi_param server_name $server _name;
# PHP only, required if PHP is built with--enable-force-cgi-redirect
Fastcgi_param Redirect_status 200;
2. CI Rewrite configuration
2.1
Location/{
Index index.php;
if (!-e $request _filename) {
Rewrite ^/(. *) $/index.php/$1 last;
Break
}
}
2.2
Let's say our subdirectory name is WJ
location/wj/{
root/var/www/html/;
Index index.html index.htm index.php;
if ($request _filename!~* (index\.php|system\.php|images|js|swfupload|robots\.txt)) {
Rewrite ^/(. *) $/index.php?$1 last;
}
Also attached to the Master directory pseudo-static rules
#rewrite ^/$/index.php last;
#一下是防止某些文件夹被直接访问
#rewrite ^/(?! Index\.php|images|robots\.txt|js|css|upimg|artdialog|style) (. *) $/index.php/$1 last;
This article is from the "Willard_sa" blog, make sure to keep this source http://374400.blog.51cto.com/364400/1590134
Nginx rewrite php ci (codeigniter) framework