This article uses Nginx to achieve URL rewrite, this article uses Nginx and static page with rewrite URL.

1. Preparation.

For this scenario, you need to install Nginx.

1.1 For details on installing Nginx on Linux, see my article (Portal).

2. What is URL rewriting?

URL rewriting is the process of rewriting a URL request into a different URL that the web site can handle. Say that may not be very good understanding, for example, to explain in development may often meet such requirements, such as http://localhost:8080/getUser? via a browser request Id = 1, but need to SEO optimization, and so on reasons, need to rewrite the request address URL for http://localhost:8080/getUser/1, so as to conform to the requirements or better be website read.

When this happens, you need to use UrlRewrite or some Gateway route such as SpringCloud’s Gateway, Zuul, or Nginx to do this.

This article introduces Nginx to implement URL rewriting.

3. Introduction to usage

Using URL rewriting in Nginx requires the use of the —–rewrite directive, which has the following syntax:

rewrite regex replacement [flag]; 
Copy the code
  • Regex: You can use a re or a string to represent a matching address.
  • Replacement: Indicates an address that can be used for redirection.
  • Flag: The flag is used to control whether to check subsequent rewrite rules after matching the rewrite rule.

The values of flag are as follows:

  • Last: Stops processing the current rewrite set of directives and then reinitiates the request using the rewritten rules, leaving the browser URL unchanged.
  • Break: Like the break directive, it stops processing other rewrite module instructions in the current context.
  • Redirect: If the replacement string does not start with “http://”, “https://” or “$scheme”, return the temporary redirect with 302 code. The browser address will display the redirect URL.
  • Permanent: Returns the permanent redirection of the 301 code. The URL is displayed in the address bar of the browser.

Such as:

server { ... rewrite ^(/download/.*)/media/(.*)\.. * $The $1/mp3/$2.mp3 last; rewrite ^(/download/.*)/audio/(.*)\.. * $The $1/mp3/$2.ra  last;
    return403; . }Copy the code

In addition, it can be judged according to if, and the instruction is as follows:

if (condition) { ... }
Copy the code

Where condition is the condition, if true, the contents of braces are executed:

  • Use “=” and “! The = “operator compares variables and strings;
  • Use “~” (for case-sensitive matching) and “~”The “(for case-insensitive matching) operator matches variables with regular expressions. Regular expressions can be included for later use in $1.. Capture of reuse in the $9 variable. The negative operator “! ~ “and”! ~“Can also be used. If the regular expression contains “} “or”;” Character, the entire expression should be enclosed in single or double quotation marks.
  • Use “-f” and “! The -f “operator checks whether the file exists;
  • Use -d and! The -d “operator checks whether the directory exists;
  • Use “-e” and “! The -e “operator checks whether a file, directory, or symbolic link exists;
  • Use “-x” and “! The -x “operator checks the executable.

Such as:

if ($http_user_agent ~ MSIE) {
    rewrite ^(.*)$ /msie/The $1 break;
}

if ($http_cookie~ *"id=([^;] (+)? :; | $)") {
    set $id The $1;
}

if ($request_method = POST) {
    return 405;
}

if ($slow) {
    limit_rate 10k;
}

if ($invalid_referer) {
    return 403;
}
Copy the code

4. Nginx configuration

This example simply redirects the request to the homepage of my blog, http://www.dalaoyang.cn/, as follows:

worker_processes  1;

events {
    worker_connections  1024;
}

http {

   server {
       listen       10000;
       server_name  localhost;

       If the host is not dalaoyang.cn, go to www.dalaoyang.cn
       if ( $host! ="dalaoyang.cn" ){
         rewrite ^/(.*)$ https://www.dalaoyang.cn/The $1last; }}}Copy the code

5. Test

Test is more simple, visit http://localhost:10000/about in the browser, automatically jump to https://www.dalaoyang.cn/about, as shown.

6. Summary

Most of the content is taken from the Nginx website, where examples are very detailed.

Nginx ngx_http_rewrite_module API- portal