Nginx real case -Rewrite rules

1. Rewrite Rules

Rewrite rules can be implemented to rewrite the url, and redirect

Action scenes:

  • Jump URL access, support the development of design, such as page jumps, the compatibility support, showing effects
  • SEO optimization
  • Maintenance: background maintenance, traffic forwarding, etc.
  • Safety

2. Redirect

2.1 What is redirection

A jump address is the address to B.

Redirection (the Redirect) by various methods that various network requests to re-set a direction other locations (eg: redirecting page redirection, the domain name, but also to change the routing of data packets via a path weight directional)

2.2 When do you need to redirect

  • Site adjustment (such as changing the page directory structure)
  • Pages are moved to a new address
  • Extension pages change (such as application need to .phpbe changed .html)

In this case, if not redirect, the user favorites, or search engine database in the old address will only make customers get access to a 404 page error message, loss of access to flow in vain; Also some registered multiple domain site also need to make access to these domain names to redirect users automatically jump to the main site, etc.

3.301 and 302

3.1 What are 301 and 302

An example to understand it:

curl -I www.taobao.com
curl -I taobao.com

Here Insert Picture Description

301 redirect: 301 represents the permanent transfer (PermanentlyMoved)
302 redirect: 302 represents the temporary transfer (TemporarilyMoved)

3.2 commonalities and differences

  • Common:
    301 and 302 status codes indicate redirection, that the browser after the server returns this status code will automatically jump to a new URL address, the address can be obtained effects (seen by the user from the header Location response he is the input address a moment into another address B)
  • difference:
    A 301 represents the old address resource has been permanently removed (this resource can not be visited), search engines crawl at the same time new content will also exchange the old URL to URL after redirection; 302 represents the old address A resources are still (still access), this is only a temporary redirect from the old address to jump a to address B, the search engine will crawl new content and save the old URL

3.3 when to jump 301 or 302

  • When a site or page 24/48 hour temporarily moved to a new location, this time it should be 302 jumps.
    An analogy: I have a house, but recently went to visit relatives and relatives who lives in the two days I was back.
  • The use of 301 jumps before the site is the scene for some reason need to be removed off, and then go to the new address access is permanent.
    In fact, you like on the set of the house is rented, now lease is up, you found a house in another place, before a rented house live

3.4 301 jump scene

  • Do not want to renew expired domain name (or find a more suitable site's domain name), I want to change the domain name.
  • Search results appear in the search engine's domain name without the www and the www domain name and has not included this time you can use a 301 redirect tells the search engine domain name which is our goal
  • Space server instability, the time for space

3.5 to make use of 301 Jump

for example:

Do from the site A (site rotten) a 302 jump to site B (searches very front rank), this time sometimes search engines will use the content of the site B, but A contains the address of the site. So unwittingly, to contribute to the site B in Site A, A site's ranking on the front

The 302 redirect search engine can easily be mistaken for the use of multiple domain names point to the same website, then your website will be sealing of charges of "use duplicate content to interfere with the site's ranking in Google search results."

4. www.westos.org—https://www.westos.org

Modify the configuration file:

vim /usr/local/nginx/conf/nginx.conf

121 server {
122         listen 80;
123         server_name www.westos.org;
124         rewrite ^/(.*)$ https://www.westos.org/$1;     ##$1表示用户在这里输入的内容保留,只会重定向$1前面的内容
125         #rewrite ^/(.*)$ https://www.westos.org/$1;    ##永久重定向(可以缓存,临时的不允许缓存)

nginx -t			#语法检测
nginx -s reload		#在不暂停服务的情况下重新加载

Here Insert Picture Description
test:

搜索www.westos.org	页面跳转到https://www.westos.org
搜索www.westos.org/test.html 跳转到https://www.westos.org/test.html

note:
1.404: Although there is no test.html this file below it, but it will still be redirected to https://www.westos.org/test.html(ie, the contents of $ 1 means that the user enter here reservations, only to redirect $ 1 previous content)

2. ^/(.*)$match all, i.e. matching www.westos.org;

Here Insert Picture Description

5. Access to -https bbs www.westos.org and ending: //bbs.westos.org:

Modify the configuration file:

vim /usr/local/nginx/conf/nginx.conf


116         location / {
117             root   /bbs;
118            index  index.html index.htm;
119         }  
120     }   
121 server {
122         listen 80;
123         server_name www.westos.org;
124         #rewrite ^/(.*)$ https://www.westos.org/$1;     ##$1表示用户在这里输入的内容保留,只会重定向$1前面的内容
125         #rewrite ^/(.*)$ https://www.westos.org/$1;     ##永久重定向(可以缓存,临时的不允许缓存)
126         rewrite ^/bbs$ https://bbs.westos.org/index.html permanent;     #表示访问www.westos.org并且以bbs    结尾的,都定向到https://bbs.westos.org

Here Insert Picture Description

mkdir /bbs
vim /bbs/index.html
cat /bbs/index.html
nginx -t			#语法检测
nginx -s reload		#在不暂停服务的情况下重新加载

Here Insert Picture Description
Add resolved in the real machine in:

vim /etc/hosts

Here Insert Picture Description

test:

bbs.westos.org #查看到的是server1,这是因为我们在配置文件中写好的默认发布页面内容为server1
https://bbs.westos.org/ #添加证书后,查看到/bbs里的默认发布内容
www.westos.org/bbs #页面会自动跳转到https://bbs.westos.org/

Here Insert Picture Description
Here Insert Picture Description

Here Insert Picture Description

Published 175 original articles · won praise 11 · views 6047

Guess you like

Origin blog.csdn.net/weixin_45775963/article/details/104587764