In fact, nginx has nothing to do with. htaccess, but many people are deeply influenced by apache and feel that nginx should also support. htaccess. Simply include the. htaccess file in nginx configuration.
Include/site directory/. htaccess;
What's so simple, but what makes people laugh is that most people don't even know that nginx and apache are two different webserver programs, so many people do this: include in nginx. after the htaccess file. upload htaccess to the root directory of the site... speechless. In fact, nginx can include any file, not just. htaccess.
In fact, nginx reference. htaccess is different from apache in this way:
1. nginx is introduced at startup, because nginx pays special attention to efficiency, so all configuration files will be read into the memory during startup, and then start one. If a syntax error is found, the startup fails. Apache is a dynamic introduction, and the configuration will be re-loaded every time the site is accessed.
2. The include in nginx is manually added to specify the directory. Apache uses. htaccess as a special mechanism and supports. htaccess files in all subdirectories of the website.
In the early days, many developers used a large number of virtual spaces. In the early days, apache was used as the web server for virtual space. Therefore, users could do their own pseudo-static jobs without contacting the space provider, the open-source program is more convenient to expand this function (ecshop, wordpress, phpcms, etc ). It is also easy to maintain, as long as it overwrites. htaccess.
In fact, few sites often modify. htaccess, and most of them now use vps.
Nginx can also use the. htaccess file to implement multiple functions.
Introduce the. htaccess file in server {} of nginx/nginx. conf:
Include/vhosts/quany.info/.htaccess;
Nginx supports. htaccess files. Follow these steps:
1. Create a new. htaccess file and enter rules in it, such as the rules on this site:
The code is as follows: |
Copy code |
# Nginx rewrite rule Rewrite ^ (.*?) /Article /.*? -(D00000000-(d00000000.html $1/show.html? Id = $2 & page = $3 break; Rewrite ^ (.*?) /Category /.*? -(D1_0000-(d1_0000.html $1/list.html? Id = $2 & page = $3 break; Rewrite ^ (.*?) /Tag/([^/] + )/? $1/tag.html? W = $2 break; Rewrite ^ (.*?) /Tag/([^/] +)/(d +) $1/tag.html? W = $2 & page = $3 break; # End nginx rewrite rule |
Save the file and upload it to the root directory of the website.
2. Modify VM configurations
The code is as follows: |
Copy code |
# Ee/usr/local/etc/nginx. conf |
# Freebsd 7.2 default nginx configuration file installed by default
Introduce the. htaccess file to the server block of the virtual machine you want to add pseudo-static.
The code is as follows: |
Copy code |
Include/usr/www/. htaccess |
# Change this to the location of your. htaccess file,
Save, exit, and restart nginx
The code is as follows: |
Copy code |
#/Usr/local/etc/rc. d/nginx restart
|
Refresh and check whether the rewrite rule syntax is correct.
Nginx rewrite rules can be placed directly in the host configuration or in an independent file with includeinru. When the file is saved as an independent file, it is saved to a new file (the file name can be retrieved at will, however, we recommend that you use it. the name of the file starting with ht (note that there is a "dot"), because this server filters out external pairs by default. access to files starting with ht:
The code is as follows: |
Copy code |
Location ~ /. Ht { Deny all; } |