Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use rewrite to configure pseudo-static in nginx

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article shows you how to use rewrite in nginx to configure pseudo-static, the content is concise and easy to understand, it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Regular expressions match, where:

* ~ for case-sensitive matching

* ~ * for case-insensitive matching

*! ~ and! * are case-sensitive mismatch and case-insensitive mismatch, respectively

File and directory match, where:

*-f and!-f are used to determine whether a file exists.

*-d and!-d are used to determine whether a directory exists.

*-e and!-e are used to determine whether a file or directory exists

*-x and!-x are used to determine whether the file is executable or not

The flag tags are:

* last is equivalent to the [L] mark in Apache, indicating that the rewrite is completed

* break terminates the match and no longer matches the following rules

* redirect returns 302 temporary redirect address column will display the address after the jump

* the address bar returned by permanent for permanent redirection will display the address after the jump

Some of the available global variables are, which can be used for conditional judgment (to be completed)

The copy code is as follows:

$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 example of QeePHP

The copy code is as follows:

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 parameter

The copy code is as follows:

Abc.domian.com/sort/2 = > abc.domian.com/index.php?act=sort&name=abc&id=2

If ($host ~ * (. *)\ .domain\ .com) {

Set $sub_name $1

Rewrite ^ / sort\ / (\ d+)\ /? $/ index.php?act=sort&cid=$sub_name&id=$1 last

}

Directory swap

The copy code is as follows:

/ 123456/xxxx-> / xxxx?id=123456

Rewrite ^ / (\ d +) /. + / / $2?id=$1 last

For example, the following sets nginx to be redirected to the / nginx-ie directory when users use ie:

The copy code is as follows:

If ($http_user_agent ~ MSIE) {

Rewrite ^ (. *) $/ nginx-ie/$1 break

}

The directory is automatically added with "/"

The copy code is as follows:

If (- d $request_filename) {

Rewrite ^ / (. *) ([^ /]) $http://$host/$1$2/ permanent

}

Prohibit htaccess

The copy code is as follows:

Location ~ /\ .ht {

Deny all

}

Multiple directories are prohibited

The copy code is as follows:

Location ~ ^ / (cron | templates) / {

Deny all

Break

}

Prohibit files that begin with / data

Requests such as .log.txt under multi-level directory under / data/ can be prohibited.

The copy code is as follows:

Location ~ ^ / data {

Deny all

}

Prohibit a single directory

Cannot prohibit .log.txt from requesting

The copy code is as follows:

Location / searchword/cron/ {

Deny all

}

Prohibit a single file

The copy code is as follows:

Location ~ / data/sql/data.sql {

Deny all

}

Set expiration time for favicon.ico and robots.txt

Here, favicon.ico is 99 days, robots.txt is 7 days and 404 error logs are not recorded.

The copy code is as follows:

Location ~ (favicon.ico) {

Log_not_found off

Expires 99d

Break

}

Location ~ (robots.txt) {

Log_not_found off

Expires 7d

Break

}

Set the expiration time of a file; here it is 600 seconds, and no access log is recorded.

The copy code is as follows:

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

Access_log off

Root / opt/lampp/htdocs/web

Expires 600

Break

}

File anti-hotlink and set expiration time

Return 412 here is a custom http status code. The default is 403. It is easy to find the correct request for hotlink.

"rewrite ^ / https://cache.yisu.com/upload/information/20210309/289/48136.jpg;" displays a hotlink protection picture

"access_log off;" does not record access logs to reduce stress

Browser cache of "expires 3D" for all files for 3 days

The copy code is as follows:

Location ~ * ^. +\. (jpg | jpeg | gif | png | swf | rar | zip | css | js) ${

Valid_referers none blocked * .ZZVIPS.com * .ZZVIPS.net localhost 208.97.167.194

If ($invalid_referer) {

Rewrite ^ / https://cache.yisu.com/upload/information/20210309/289/48136.jpg;

Return 412

Break

}

Access_log off

Root / opt/lampp/htdocs/web

Expires 3d

Break

}

Only allow fixed ip access to the website with a password

The copy code is as follows:

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

Convert the files in multi-level directories into one file to enhance the seo effect

The copy code is as follows:

/ job-123-456-789.html points 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 under the root directory to a level 2 directory

For example, / shanghaijob/ points to / area/shanghai/

If you change last to permanent, the browser address bar says / location/shanghai/

The copy code is as follows:

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

One problem with the above example is that accessing / shanghai will not match

The copy code is as follows:

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

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

This way / shanghai can also be accessed, but the relative links in the page cannot be used

For example, the real address of. / list_1.html is / area/shanghia/list_1.html will become / list_1.html, leading to inaccessible.

So it's not good for me to add automatic jump.

(- d $request_filename) it has a condition that it must be a real directory, but my rewrite is not, so it has no effect.

The copy code is as follows:

If (- d $request_filename) {

Rewrite ^ / (. *) ([^ /]) $http://$host/$1$2/ permanent

}

It will be easy when I know the reason. Let me jump manually.

The copy code is as follows:

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

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

Redirect when files and directories do not exist:

The copy code is as follows:

If (!-e $request_filename) {

Proxy_pass http://127.0.0.1;

}

Domain name jump

The copy code is as follows:

Server

{

Listen 80

Server_name jump.ZZVIPS.com

Index index.html index.htm index.php

Root / opt/lampp/htdocs/www

Rewrite ^ / http://www.ZZVIPS.com/;

Access_log off

}

Multi-domain name shift

The copy code is as follows:

Server_name www.ZZVIPS.com www.ZZVIPS.net

Index index.html index.htm index.php

Root / opt/lampp/htdocs

If ($host ~ "ZZVIPS\ .net") {

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

}

Three-level domain name jump

The copy code is as follows:

If ($http_host ~ * "^ (. *)\ .I\ .ZZVIPS\ .com $") {

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

Break

}

Domain name mirror direction

The copy code is as follows:

Server

{

Listen 80

Server_name mirror.ZZVIPS.com

Index index.html index.htm index.php

Root / opt/lampp/htdocs/www

Rewrite ^ / (.*) http://www.ZZVIPS.com/$1 last

Access_log off

}

A subdirectory acts as a mirror

The copy code is as follows:

Location ^ ~ / zhaopinhui {

Rewrite ^. + http://zph.ZZVIPS.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 last

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 a domain name separately for a certain section of discuz

The copy code is as follows:

Server_name bbs.ZZVIPS.com news.ZZVIPS.com

Location = / {

If ($http_host ~ news\ .ZZVIPS.com $) {

Rewrite ^. + http://news.ZZVIPS.com/forum-831-1.html last

Break

}

}

Rewrite optimization of discuz ucenter avatar

The copy code is as follows:

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

The copy code is as follows:

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

}

The above is how to use rewrite to configure pseudo-static in nginx. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report