Enables the Nginx server to support. Htaccess Method _nginx

Source: Internet
Author: User
Tags nginx server

Probably many friends are commonly used nginx not to support. htaccess, only Apache support. htaccess file, this is the wrong view Nginx is also supported. Hatccess Oh, let me give you a summary of the configuration method.

In fact, Nginx and. htaccess a little relationship is not, just a lot of people by the impact of Apache think Nginx should also support. htaccess function. Include the. htaccess file directly in the Nginx configuration

Include/Site Directory/.htaccess;

How simple, but more people laugh and cry is that most people do not know Nginx and Apache is two different webserver procedures, so many people do: in Nginx include. htaccess file, To upload the. htaccess of the Apache pseudo static rule to the site root directory ... No words. In fact, Nginx can include any file, not just. htaccess.

In fact, nginx such a reference. Htaccess is different from Apache:

1, Nginx is to launch the introduction, because the Nginx special attention to efficiency, so start the time will be all the configuration files into memory, and then start a. If a syntax error is found, the startup fails. Apache, however, is introduced dynamically, and each visit to the site is loaded back into the configuration.

2, the include in Nginx is manually added, specify the directory. Apache has taken. htaccess as a characteristic mechanism, and all subdirectories of the support station can have. htaccess files.

The actual development experience, many people in the early days are more virtual space, and the early virtual space is basically Apache do webserver, so users do not need to contact the space business can do their own pseudo static, and open source program is more convenient to expand this function (Ecshop,wordpress, PHPCMS, etc.). and maintenance is also convenient, as long as the cover. Htaccess is good.

In fact, not a few sites often to modify. htaccess, and now basically use VPS mostly.

Nginx can also use. htaccess files to implement multiple functions.

The. htaccess file is introduced in the server{} of nginx/nginx.conf:

include/vhosts/quany.info/.htaccess;

Nginx can fully support. htaccess files, do the following:

1, create a new. htaccess file, enter the rules in the inside, such as the rules of the site:

# nginx Rewrite rule
rewrite ^ (. *?) /article/.*?-(d+)-(d+). html$  $1/show.html?id=$2&page=$3 break   ;
Rewrite ^ (. *?) /category/.*?-(d+)-(d+). 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 Web site.

2, modify the virtual host configuration

# ee/usr/local/etc/nginx/nginx.conf 
#freebsd 7.2 Default nginx configuration file installed

Introduce the. htaccess file into the server block in the virtual machine where you need to add pseudo static.

Include/usr/www/.htaccess
#请把这个改成你 the location of the. htaccess file,

Save, exit reboot Nginx

#/usr/local/etc/rc.d/nginx Restart

Refresh see if the normal device from the proof of your rewrite rule syntax is correct

Nginx Rewrite rules can be placed directly in the host configuration or in separate files with Includeinru, when saving as a separate file, save to a new file (the file name can be arbitrarily taken, but it is recommended to use the name of the. HT (Note that there is a "dot"). Because this server has the default filtering of external file access to. HT:

Location ~/.ht {
  deny all;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.