Nginx rewrite Pseudo-static address auto-completion method implementation

Source: Internet
Author: User

Pseudo-static is often required when using nginx + php, which is usually set manually. Is there a way to enable nginx to automatically complete the path?

These two days have been a long time before such a function is realized:
Request/a/B/c
If the file does not exist, search for/a/B/index. php and/c as PATH_INFO;
If the file does not exist, find/a/index. php,/B/c as PATH_INFO;
If the file does not exist, search for/index. php and/a/B/c as PATH_INFO;
If the file does not exist, 404 is returned.

Although this kind of performance loss is not suitable for deployment, it can still bring convenience during local debugging :)

The server should have the following code, and other parts should use their own configuration:

Index. php index.html index.htm;

Location/{set $ path $ request_uri; set $ path_info ""; try_files $ uri/@ 404;} location @ 404 {if ($ path ~ ^ (. *)(/. +) $) {Set $ path $1/index. php; set $ path_info $2; 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 $2; set $ path $1/index. php; rewrite. * $ path last;} return 404 ;}




In Linux, nginx supports the. htaccess file pseudo-static method.

I tried it according to nginx rules and found that nginx fully supports the. htaccess file!

The method is as follows:

1. Create a new. htaccess file in the directory where you need to use the. htaccess file,
For example, in my Discuz Forum directory:

Vim/var/www/html/168 pc/bbs/. htaccess

2. Enter a rule in it. Here I will enter the pseudo static rule of Discuz:

# Nginx rewrite rule
Rewrite ^ (. *)/archiver/(fid | tid0000-0000w-00000000.html) $1/archiver/index. php? $2 last;
Rewrite ^ (. *)/forum-(%0-9%%%%%-(%0-9%%%%%%.html $1/forumdisplay. php? Fid = $2 & page = $3 last;
Rewrite ^ (. *)/thread-(%0-9%%%%%-(%0-9%%%%%%%%%%.html $1/viewthread. php? Tid = $2 & extra = page % 3D $4 & page = $3 last;
Rewrite ^ (. *)/profile-(username | uid+-(.20.20..html $1/viewpro. php? $2 = $3 last;
Rewrite ^ (. *)/space-(username | uid+-(.20.20..html $1/space. php? $2 = $3 last;
Rewrite ^ (. *)/tag-(.20.20..html $1/tag. php? Name = $2 last;
# End nginx rewrite rule

Wq is saved and exited.

3. Modify the nginx configuration file:

Vim/etc/nginx. conf

4. Introduce the. htaccess file to the server {} of the virtual host that requires adding pseudo-static hosts, as shown in the figure:

Include/var/www/html/168 pc/bbs/. htaccess; (change this to the specific location of your. htaccess file)

Wq is saved and exited.

5. Reload the nginx configuration file:

/Etc/init. d/nginx reload

Open the webpage again and check whether the pseudo-static state is normal. This proves that your rewrite rule syntax is correct.


Normal, complete!

Supplement: I found a web page on the Internet that can automatically convert Apache Rewrite Pseudo-static rules to Nginx Rewrite Web pages online. You can try it.

Http://www.anilcetin.com/convert-apache-htaccess-to-nginx/

The content in this address contains the functions that can be slightly modified as described above. The rules in. htaccess are automatically converted to available rules under nginx.

Summary :. the htaccess file is a distributed configuration file dedicated to apache. It provides a method to change the configuration of each directory, that is, to place a file containing commands in a specific directory, the commands act on this directory and all its subdirectories. In fact, nginx can also use the. htaccess file to implement multiple functions. The implementation of pseudo-static is only one of the purposes of. htaccess. htaccess can also be used for many purposes, such as filtering access IP addresses, setting web directory access permissions, and passwords.

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.