The Nginx server uses rewrite to rewrite the URL to implement a pseudo static example _nginx

Source: Internet
Author: User
Tags nginx server

After searching and testing on the Internet, it is found that the rewrite rules of Nginx and the Rewite rules of Apache are not very different, and can be used almost directly. Like writing rules like this in Apache.

Rewrite ^/([0-9]{5}). html$/viewthread.php?tid=$1 last;

Writing in Nginx is impossible to start, and the solution is to add two double quotes:

Rewrite "^/([0-9]{5}). html$"/viewthread.php?tid=$1 last;

At the same time, the rewriterule is rewrite, basically realizes the nginx rewrite rule to the Apache Rewite rule conversion.

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

The rewrite of WordPress
in fact, under the Nginx to configure the rewrite or WordPress is relatively simple, in the location/{.....} Inside join

if (!-f $request _filename) {
rewrite (. *)/index.php;
}

can be achieved.

The following is a complete vhost configuration file

server {
listen;
server_name ccvita.com www.ccvita.com;
Location/{
    index index.html index.htm index.php;
    root/www/wwwroot/ccvita.com;
    if (-f $request _filename/index.html) {
        rewrite (. *) $1/index.html break
    ;
    if (f $request _filename/index.php) {
        rewrite (. *) $1/index.php;
    }
    if (!-f $request _filename) {
        rewrite (. *)/index.php;
    }

}
Location ~ \.php$ {
    include fastcgi_params;
    Fastcgi_index index.php;
    Fastcgi_pass 127.0.0.1:8787;
    Fastcgi_param script_filename/www/wwwroot/ccvita.com$fastcgi_script_name;
    }
Location/ccvita-status {
    stub_status on;
    Access_log off;
    }


Discuz! 's rewrite
the following rewrite has a transfer character "\" in front of the hundred semicolon, which is needed in Apache and is not needed in Nginx.

Rewrite ^/thread-([0-9]+)-([0-9]+)-([0-9]+) \.html$/viewthread.php?tid=$1&extra=page\%3d$3&page=$2 last;

The right one should be

Rewrite ^/thread-([0-9]+)-([0-9]+)-([0-9]+) \.html$/viewthread.php?tid=$1&extra=page%3d$3&page=$2 last;

This error is essentially present in all the use of Nginx as a server and has opened on the rewrite Web site. Including discuz! official, has now given cnteacher feedback.
Nginx Instance Code

server {Listen 80;

  server_name www.ccvita.com ccvita.com;
     Location/{index index.html index.htm index.php;
     root/www/www.ccvita.com;
     Rewrite ^ (. *)/archiver/((Fid|tid)-[\w\-]+\.html) $ $1/archiver/index.php?$2 last;
     Rewrite ^ (. *)/forum-([0-9]+)-([0-9]+) \.html$ $1/forumdisplay.php?fid=$2&page=$3; Rewrite ^ (. *)/thread-([0-9]+)-([0-9]+) – ([0-9]+) \.html$ $1/viewthread.php?tid=$2&extra=page%3d$4&page=$3
     Last
     Rewrite ^ (. *)/profile-(USERNAME|UID)-(. +) \.html$ $1/viewpro.php?$2=$3 last;
     Rewrite ^ (. *)/space-(USERNAME|UID)-(. +) \.html$ $1/space.php?$2=$3 last;

  Rewrite ^ (. *)/tag-(. +) \.html$ $1/tag.php?name=$2 last;
        } location ~ \.php$ {include fastcgi_params;
        Fastcgi_index index.php;
        Fastcgi_pass 127.0.0.1:8694;
    Fastcgi_param Script_filename/www/www.ccvita.com$fastcgi_script_name;
     } location/www.ccvita.com-status {stub_status on;
  Access_log off;
 }
}

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.