Nginx rewrite URI address rewrite

Source: Internet
Author: User
Tags nginx load balancing

Introduced


People often encounter such demand, an active page URL address is very long, promote product operation and user feedback is not easy to remember

Not beautiful, but temporarily no way to modify the code, this time can be used to rewrite. Or you now have a website upgrade to HTTPS now some users are still accessing HTTP you want to unify HTTPS, which can be implemented with rewrite rules.

Rewrite overrides the user's request address, but does not modify the argument after the question mark, and the Httpcode returned to the user is 302.

Syntax: rewrite regex replacement [flag]; scope: server, location, if
Rewrite_log on |        Off #rewrite日志开启和关闭set $user _pro "false"; Define area "main" Uninitialized_variable_warn on |          Off Controls whether a variable that warns of uninitialized is logged. Enabled by default;


**nginx and Apache rewrite simple comparison * *


We used to write these rules in the ". htaccess" file when using Apache as a shared host, but Nginx is not.

* * Example 1:**

        apache's rewrite rules          RewriteCond  %{HTTP_HOST}  example.org         RewriteRule   (. *)           http:// www.example.org$1                The rewrite rule of  nginx:        server {             listen       80;             server_name  www.example.org   example.org;            if  ($http _host  = example.org)  {                 rewrite   (. *)   http://www.example.org$1;             }

But this kind of writing is not recommended, preferably each different domain name is different server{}


Example 2:


Nginx will all match to the "/code/" directory beginning of the request jump to "http://192.168.1.128:8080/" but not modified? The following parameters.

Location ~ ^/code/{rewrite (. *) http://192.168.1.128:8080/; }

Example 3:


Regular match Content "^ (/download/.*)/audio/(. *) \. *$ ", each of the preceding". * "Maps to the destination address is $1,$2,$3 ....

Rewrite ^ (/download/.*)/audio/(. *) \.        *$ $1/mp3/$2.ra last;            return 403; #返回httpcode 403 Rewrite ^ (/media/.*)/audio/(. *) \. *$ Http://new.nginxs.net/$1/mp3/$2.ra last;


Rewrite mark


Description: If you use a regular match for a request address, the URI will change to the specified address and sequentially execute the instructions that appear in the configuration file,

But if you want to change the address and then jump out, you can use flag to handle the following last,break,redirect,permanent four tags


Last #在搜索到相应的URI和location之后完成rewrite指令; stop processing the current ngx_http_rewrite_module instruction set and start looking for a new location to change the URI match;         Break # completes the rewrite command to handle the direct break; redirect #返回302临时重定向 if the replacement replacement part is started by http://, it will be applied. Permanent #返回301代码永久重定向


Nginx Tutorials


Chapter One Nginx installation basic boot and process signal


02-nginx IO Model


03-nginx Load Balancing


04-nginx Root and alias differences


05-nginx Limit_req and Limit_conn_zone


06-nginx access control "Restrict IP", "Restrict user name"


07-nginx Geo Zone access control


08-nginx If statement condition judgment


10-nginx internal variables and custom variables


11-nginx rewrite URI address rewrite


This article is from the "Nginxs Small white" blog, please be sure to keep this source http://nginxs.blog.51cto.com/4676810/1775980

Nginx rewrite URI address rewrite

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.