Common example of jump configuration in Nginx website

Source: Internet
Author: User
Tags browser cache

I believe that everyone in the daily operation of the work if you use Nginx as the front-end reverse proxy server, you will be the rewrite of nginx and love and hate, love it is because you have done it, completed the developer's jump demand you will feel very cool, feel really strong, Hate it is because when some strange jump demand when you will not have the clue, a variety of debugging, the Internet praying are uncertain when really want to die of heart have, of course, there are many classic examples of nginx rewrite, but I feel that my work is not much help

Here are some examples of rewrite that I have encountered in my work. To share with you

Regular expression matching, where:

    1. * ~ For case-sensitive matching

    2. * ~* for case-insensitive matching

    3. *!~ and!~* are case insensitive and case insensitive

File and directory matching, where:

    1. *-F and!-f to determine if a file exists

    2. *-D and!-d to determine if a directory exists

    3. *-E and!-e to determine if a file or directory exists

    4. *-X and!-x to determine if the file is executable

Flag flags are:

    1. * Last equivalent to the [L] mark in Apache, indicating completion of rewrite

    2. * Break terminates the match and no longer matches the following rule

    3. * REDIRECT Returns 302 the temporary redirect Address bar displays the address after the jump

    4. * Permanent return 301 Permanent redirect Address bar will show the address after the jump

1, change the domain name after the diversion to the new domain name

Need to jump all previous www.a.com domain name traffic to www.b.com
Implementation effect: such as access www.a.com/123.html automatically jump to www.b.com/123.html

We use Nginx to achieve

Cd/etc/nginx/sites-available

VI MySite

Add rewrite command

server {

Listen 80;

server_name www.a.com;

Rewrite ^/(. *) $ http://www.b.com/$1 permanent;

}

2, the main domain to jump to www domain name

For example, the main domain xxx.com jump to www.xxx.com

Cd/etc/nginx/sites-available

VI MySite

#主域名跳转到www域名

server {

Listen 80;

server_name xxx.com;

Rewrite ^/(. *) $ http://www.xxx.com/$1 permanent;

}

server {

Listen 80;

server_name www.xxx.com;

#已省略余下通用配置内容

}

3, main directory jump, sub-directory does not jump

A.com and www.a.com all jumped to www.b.com.
Www.a.com/123 don't jump.

server {

Listen 80;

server_name www.a.us a.us;

#根目录跳转

Location/{

Rewrite. + http://www.b.com/permanent;

}

#非根目录本地执行

Location ~*/.+ {

#已省略余下通用配置内容

}

}

4, 301 Jump settings:

server {
Listen 80;
server_name 123.com;
Rewrite ^/(. *) http://456.com/$1 permanent;
Access_log off;
}

permanent– returns the HTTP status of permanent redirection 301

5, 302 Jump settings:

server {
Listen 80;
server_name 123.com;
Rewrite ^/(. *) http://456.com/$1 redirect;
Access_log off;
}

redirect– returns the HTTP status of a temporary REDIRECT 302

6. Prohibit htaccess

Location ~//.ht {

Deny all;

}

7. Prohibit multiple directories

Location ~ ^/(cron|templates)/{

Deny all;

Break

}

8. Prohibit files beginning with/data
Can prohibit/data/under the multilevel directory. Log.txt and other requests;

Location ~ ^/data {

Deny all;

}

9. Prohibit a single directory
Cannot forbid. Log.txt can request

location/searchword/cron/{

Deny all;

}

10. Prohibit individual files

Location ~/data/sql/data.sql {

Deny all;

}

11, to Favicon.ico and robots.txt set expiration time;
Here is favicon.ico for 99 days, robots.txt for 7 days does not log 404 error logs

Location ~ (favicon.ico) {

Log_not_found off;

Expires 99d;

Break

}

Location ~ (robots.txt) {

Log_not_found off;

Expires 7d;

Break

}

12, set the expiration time of a file, this is 600 seconds, do not log access logs

Location ^~/html/scripts/loadhead_1.js {

Access_log off;

Root/opt/lampp/htdocs/web;

Expires 600;

Break

}

13. File anti-hotlinking and set expiration time
The return 412 here is a custom HTTP status code with a default of 403, which makes it easy to find the correct hotlinking request.
"Rewrite ^/http://leech.c1gstudio.com/leech.gif;" Show a picture of the anti-theft chain
"Access_log off;" Reduce stress by not logging access logs
"Expires 3d" All files 3-day browser cache

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 ^/http://leech.c1gstudio.com/leech.gif;

return 412;

Break

}

Access_log off;

Root/opt/lampp/htdocs/web;

Expires 3d;

Break

}

14, only allow fixed IP access to the site, and add a 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;

15, the Multi-level directory files into a file, enhance the effect of SEO

/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;

16. Point a folder under the root directory to a level 2 directory
such as/shanghaijob/point to/area/shanghai/
If you change last to permanent, then the browser address bar is/location/shanghai/

Rewrite ^/([0-9a-z]+) job/(. *) $/area/$1/$2 last;

One of the problems with the above example is that access to/shanghai will not match

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,
If the real address of the./list_1.html is/area/shanghia/list_1.html will become/list_1.html, which leads to inaccessible

Well, I'm not going to add the auto 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 after the good, let me manually jump

Rewrite ^/([0-9a-z]+) job$/$1job/permanent;

Rewrite ^/([0-9a-z]+) job/(. *) $/area/$1/$2 last;

17. Redirects when files and directories do not exist:

if (!-e $request _filename) {

Proxy_pass http://127.0.0.1;

}

18. 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;

}

19. Multi-Domain Steering

server_name www.c1gstudio.com 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;

}

20, three level domain name jump

if ($http _host ~* "^ (. *)/.i/.c1gstudio/.com$") {

Rewrite ^ (. *) http://top.yingjiesheng.com$1;

Break

}

Common example of jump configuration in Nginx website

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.