Nginx the next 301 ways to redirect domain names _nginx

Source: Internet
Author: User
Tags browser cache nslookup nslookup command domian
Linux Nginx 301 Redirect Jump method Summary, there is a need for friends to refer to.

The first case: access to the AAAAAAA station directed to the BBBBBBBBBBB station
Copy Code code as follows:

server {
Server_naaaaaaame www.aaaaaaa.com;
Rewrite ^ (. *) http://www.bbbbbbbbbbb.com$1 permaaaaaaanent;
}


In the second case: not all redirects to the AAAAAAA station are redirected to the specified page

Copy Code code as follows:

server {
Server_naaaaaaame www.aaaaaaa.com;
if ($host!= ' aaaaaaa.com ')
{Rewrite ^/(. *) $ http://www.bbbbbbbbbbb.com/$1 permaaaaaaanent;}}

will also be redirected if write is used for IP access in the first server segment

A third type of jump with www or without www

Copy Code code as follows:

Server
{
Server_naaaaaaame c.net
Rewrite ^/(. *) $ http://www.c.net/$1 permaaaaaaanent;
}


The following is a nginx rule jump:

First, in your domain name management inside the definition of c.com and www.c.com point to your host IP address, we can use the nslookup command test: Direct input nslookup c.com and Nslookup have a record point to IP.

Second, we can configure rewrite rules within the nginx. Open nginx.conf File
Find your server configuration segment: "The following is my server configuration segment"

Copy Code code as follows:
Server
{
Listen 80;
server_name www.c.com c.com;
if ($host!= ' www.c.com ') {
Rewrite ^/(. *) $ http://www.c.com/$1 permanent;
}

This is the user directly access c.com direct jump www.c.com. That is, let the domain name without www jump to the domain name with www.

Third, we can be more than two domain names, three-level domain name can jump at will, or let them all jump to blog.c.com this domain name
Agree to add the following statement:

Copy Code code as follows:
Server
{
Listen 80;
server_name blog.c.com wgkgood.gicp.net;
if ($host = ' wgkgood.gicp.net ') {
Rewrite ^/(. *) $ http://blog.c.com/$1 permanent;
}


Why do I use 301 redirection
Web site in the construction of the need to redirect a lot of things: such as the structure of the page directory changes, Web page rename, the extension of the Web page changes, site domain name changes. If you do not redirect, the user's collection and the old address in the search engine database can only allow visitors to get a 404 error message page, the access traffic lost in vain. Not only that, all the previous accumulation of the page (such as PR value) is in vain.
301 redirects can not only make the page to achieve automatic jump, for search engines, may also be able to pass the PR value.

--------------------------------------------------------------------------------
Detailed introduction to Nginx redirection rules

Rewrite command
Nginx's rewrite is the equivalent of Apache's rewriterule (in most cases, the original Apache rewrite rules can be used directly with quotes), it can be used in the server,location and if condition judgment block, The command format is as follows:
Rewrite a regular expression to replace the target flag tag
Flag tags can be in the following formats:
last– basically use this flag.
break– abort Rewirte, do not continue to match
redirect– returns the temporarily redirected HTTP status 302
permanent– returns HTTP status for permanent redirection 301
For example, the following setting Nginx redirects a file under a directory to another directory, and the corresponding string in the second bracket (. *) is $:
location/download/{
Rewrite ^ (/download/.*)/m/(. *) \. *$ $1/nginx-rewrite/$2.gz break;
}
Judgment of If condition for nginx redirection
In the case of server and location two, you can use the Nginx if condition to determine the following conditions:
Regular expressions
Such as:
Match judgment
~ to match case sensitivity;!~ does not match case sensitivity
~* matches case-insensitive;!~ does not match case insensitive
For example, the following settings Nginx are redirected to the/nginx-ie directory using the user's use of IE:
if ($http _user_agent ~ msie) {
Rewrite ^ (. *) $/nginx-ie/$1 break;
}
File and directory Judgments
-F and!-f to determine whether a file exists
-D and!-d to determine whether a directory exists
-E and!-e to determine whether a file or directory exists
-X and!-x determine if the file is executable
For example, the following setting Nginx redirects when files and directories do not exist:
if (!-e $request _filename) {
Proxy_pass HTTP://127.0.0.1/;
}
Return
Return HTTP code, such as setting the Nginx anti-theft chain:
Location ~* \. (gif|jpg|png|swf|flv) $ {
Valid_referers none blocked http://www.jefflei.com/http://www.leizhenfang.com/;
if ($invalid _referer) {
return 404;
}
}
Set
Set Nginx variables
301 Redirect Method

301 redirects were made, merging Www.jb51.net and Jb51.net, and merging the previous domain names. There are two implementations, the first of which is to judge the Nginx core variable host (the old version is Http_host):
server {
server_name www.jb51.net jb51.net;
if ($host!= ' www.jb51.net ') {
Rewrite ^/(. *) $ http://www.jb51.net/$1 permanent;
}
...
}
The second method:
server {
server_name jb51.net;
Rewrite ^/(. *) http://www.jb51.net/$1 permanent;
}

Testing the first method OK, in both of these methods, permanent is the key, detailed description see nginx redirection rule description.

last– basically use this flag.
break– abort Rewirte, do not continue to match
redirect– returns the temporarily redirected HTTP status 302
permanent– returns HTTP status for permanent redirection 301

OK, now you can check the results, and here you can see the HTTP header information returned:

Http://www.seoconsultants.com/tools/headers.asp

The second method did not test successfully ...

Whether the test was directed successfully

http://qinfy.net/301-redirect-for-nginx/

Input instruction ~

/usr/local/nginx/sbin/nginx-t
Tips:
The configuration file/usr/local/nginx/conf/nginx.conf syntax is OK
Configuration file/usr/local/nginx/conf/nginx.conf test is successful

Test Success ~ Restart nginx~ input Instructions ~

/usr/local/nginx/sbin/nginx-s Reload
After the reboot test to see if the success of the completion of the set! Input instruction ~

Curl-i imcat.tk


Will output:

http/1.1 Moved Permanently
server:nginx/0.7.65
Date:tue Aug 01:12:37 GMT
Content-type:text/html
content-length:185
Connection:keep-alive
location:http://qinfy.net/

Nginx rewrite pseudo-static configuration parameters detailed description (turn)

Nginx rewrite pseudo-static configuration parameters and examples attached with regular use instructions

Regular expression matches, where:

* ~ to Match case sensitivity
* ~* matching for case-insensitive case
*!~ and!~* are case insensitive and case-insensitive mismatches, respectively


File and directory matching, where:

*-F and!-f are used to determine whether a file exists
*-D and!-d used to determine whether a directory exists
*-E and!-e used to determine whether a file or directory exists
*-X and!-x are used to determine whether a file is executable
Flag tags are:

* Last equivalent to the [L] tag in Apache, which means complete rewrite
* Break termination match, no longer match the following rule
* REDIRECT Return 302 temporary redirect Address bar will show the address after the jump
* Permanent return 301 Permanent redirect Address bar will show the address after the jump
Some of the global variables available can be used as conditional judgments (pending completion)

$args
$content _length
$content _type
$document _root
$document _uri
$host
$http _user_agent
$http _cookie
$limit _rate
$request _body_file
$request _method
$remote _addr
$remote _port
$remote _user
$request _filename
$request _uri
$query _string
$scheme
$server _protocol
$server _addr
$server _name
$server _port
$uri
Combined with the qeephp example

if (!-d $request _filename) {
Rewrite ^/([a-z-a-z]+)/([a-z-a-z]+)/? (. *) $/index.php?namespace=user&controller=$1&action=$2&$3 last;
Rewrite ^/([a-z-a-z]+)/?$/index.php?namespace=user&controller=$1 last;
Break
Multi-Directory conversion parameters
ABC.DOMIAN.COM/SORT/2 => abc.domian.com/index.php?act=sort&name=abc&id=2

if ($host ~* (. *) \.domain\.com) {
Set $sub _name $;
Rewrite ^/sort\/(\d+) \/?$/index.php?act=sort&cid= $sub _name&id=$1 last;
}
Directory swap
/123456/xxxx->/xxxx?id=123456

Rewrite ^/(\d+)/(. +)//$2?id=$1 last;
For example, the following settings Nginx are redirected to the/nginx-ie directory using the user's use of IE:

if ($http _user_agent ~ msie) {
Rewrite ^ (. *) $/nginx-ie/$1 break;
}
Directory automatically add "/"

if (-D $request _filename) {
Rewrite ^/(. *) ([^/]) $ http://$host/$1$2/permanent;
}
Prohibit htaccess

Location ~/\.ht {
Deny all;
}
Prohibit multiple directories

Location ~ ^/(cron|templates)/{
Deny all;
Break
}
Prohibit files that start with/data
Can prohibit/data/under the multi-level directory. Log.txt and other requests;

Location ~ ^/data {
Deny all;
}
Prohibit a single directory
cannot be forbidden. Log.txt can request

location/searchword/cron/{
Deny all;
}
Prohibit individual files

Location ~/data/sql/data.sql {
Deny all;
}
Set expiration time for Favicon.ico and robots.txt;
Here for Favicon.ico 99 days, robots.txt 7 days does not record 404 error log

Location ~ (favicon.ico) {
Log_not_found off;
Expires 99d;
Break
}

Location ~ (robots.txt) {
Log_not_found off;
Expires 7d;
Break
}
Sets the expiration time for a file; This is 600 seconds, and the access log is not logged

Location ^~/html/scripts/loadhead_1.js {
Access_log off;
Root/opt/lampp/htdocs/web;
Expires 600;
Break
}
File anti-hotlinking and set expiration time
The return 412 here is a custom HTTP status code, the default is 403, to easily find the right hotlinking request
"Rewrite ^/yun_qi_img/leech.gif;" Display an anti-theft chain picture
"Access_log off;" Do not log access logs to reduce stress
"Expires 3d" browser cache for all files 3 days

Location ~* ^.+\. (JPG|JPEG|GIF|PNG|SWF|RAR|ZIP|CSS|JS) $ {
Valid_referers none blocked *.c1gstudio.com *.c1gstudio.net localhost 208.97.167.194;
if ($invalid _referer) {
Rewrite ^/yun_qi_img/leech.gif;
return 412;
Break
}
Access_log off;
Root/opt/lampp/htdocs/web;
Expires 3d;
Break
}
Only fixed IP access to the site, plus password

root/opt/htdocs/www;
Allow 208.97.167.194;
Allow 222.33.1.2;
Allow 231.152.49.4;
Deny all;
Auth_basic "C1g_admin";
Auth_basic_user_file htpasswd;
Turn the files in a multilevel directory into a file to enhance the SEO effect
/job-123-456-789.html Point to/job/123/456/789.html

Rewrite ^/job-([0-9]+)-([0-9]+)-([0-9]+) \.html$/job/$1/$2/jobshow_$3.html last;
Point a folder in the root directory to level 2 directory
such as/shanghaijob/point to/area/shanghai/
If you change the last to permanent, then the browser address bar is/location/shanghai/

Rewrite ^/([0-9a-z]+) job/(. *) $/area/$1/$2 last;
The problem with the example above is that it will not match when accessing the/shanghai

Rewrite ^/([0-9a-z]+) job$/area/$1/last;
Rewrite ^/([0-9a-z]+) job/(. *) $/area/$1/$2 last;
This/shanghai can also be accessed, but the relative links in the page are not available.
such as./list_1.html real address is/area/shanghia/list_1.html will become/list_1.html, lead to inaccessible.

Then I'll go with the automatic jump.
(-D $request _filename) It has a condition that is required for the real directory, and my rewrite is not, so no effect

if (-D $request _filename) {
Rewrite ^/(. *) ([^/]) $ http://$host/$1$2/permanent;
}
Know the reason to do it, let me manually jump it

Rewrite ^/([0-9a-z]+) job$/$1job/permanent;
Rewrite ^/([0-9a-z]+) job/(. *) $/area/$1/$2 last;
redirect When files and directories do not exist:

if (!-e $request _filename) {
Proxy_pass HTTP://127.0.0.1/;
}
Domain Jump

Server
{
Listen 80;
server_name jump.c1gstudio.com;
Index index.html index.htm index.php;
root/opt/lampp/htdocs/www;
Rewrite ^/http://www.c1gstudio.com/;
Access_log off;
}
Multi-domain Direction

server_name http://www.c1gstudio.com/http://www.c1gstudio.net/;
Index index.html index.htm index.php;
Root/opt/lampp/htdocs;
if ($host ~ "C1gstudio\.net") {
Rewrite ^ (. *) http://www.c1gstudio.com$1/permanent;
}
Level three domain jump

if ($http _host ~* "^ (. *) \.i\.c1gstudio\.com$") {
Rewrite ^ (. *) http://top.yingjiesheng.com$1/;
Break
}
Domain name Mirror to

Server
{
Listen 80;
server_name mirror.c1gstudio.com;
Index index.html index.htm index.php;
root/opt/lampp/htdocs/www;
Rewrite ^/(. *) http://www.c1gstudio.com/$1 last;
Access_log off;
}
A subdirectory is used as a mirror to

Location ^~/zhaopinhui {
Rewrite ^.+ http://zph.c1gstudio.com/last;
Break
}
Discuz Ucenter Home (uchome) rewrite

Rewrite ^/(space|network)-(. +) \.html$/$1.php?rewrite=$2 last;
Rewrite ^/(space|network) \.html$/$1.php last;
Rewrite ^/([0-9]+) $/space.php?uid=$1 last;
Discuz 7 Rewrite

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;
Configure the domain name separately for discuz

server_name bbs.c1gstudio.com news.c1gstudio.com;

Location =/{
if ($http _host ~ news\.c1gstudio.com$) {
Rewrite ^.+ http://news.c1gstudio.com/forum-831-1.html last;
Break
}
}
Discuz ucenter Avatar Rewrite optimization

Location ^~/ucenter {
Location ~. *\.php?$
{
#fastcgi_pass Unix:/tmp/php-cgi.sock;
Fastcgi_pass 127.0.0.1:9000;
Fastcgi_index index.php;
Include fcgi.conf;
}

Location/ucenter/data/avatar {
Log_not_found off;
Access_log off;
Location ~/(. *) _big\.jpg$ {
Error_page 404/ucenter/images/noavatar_big.gif;
}
Location ~/(. *) _middle\.jpg$ {
Error_page 404/ucenter/images/noavatar_middle.gif;
}
Location ~/(. *) _small\.jpg$ {
Error_page 404/ucenter/images/noavatar_small.gif;
}
Expires 300;
Break
}
}
Jspace rewrite

Location ~. *\.php?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
Fastcgi_pass 127.0.0.1:9000;
Fastcgi_index index.php;
include fcgi.conf;
}

Location ~* ^/index.php/
{
Rewrite ^/index.php/(. *)/index.php?$1 break;
Fastcgi_pass 127.0.0.1:9000;
Fastcgi_index index.php;
include fcgi.conf;
}

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.