Nginx Reverse proxy (Request forwarding-url matching rule)

Source: Internet
Author: User
Tags nginx reverse proxy

Reverse proxies are used in many situations, and load balancing is the most common usage.

Nginx , as one of the most popular Web servers, can easily implement reverse proxy.

nginx Reverse Proxy Official document: Nginx REVERSE Proxy

When multiple different Web servers are deployed on a single host, and you need to be able to access these Web servers simultaneously on port 80, you can use the nginx reverse proxy function: Use nginx to listen for all requests on port 80 and according to the forwarding rules ( It is more common to forward the URI to the corresponding Web server.

For example, there are webmail , webcom and webdefault three servers running in portmail , portcom , PORTD, respectively Efault port, to enable simultaneous access to the three Web servers from port 80, you can run nginxon port 80 and /mail forward the next request to the webmail server, /com The request is forwarded to the webcom server, and all other requests are forwarded to the webdefault server.

Assuming the server domain name is example.com, the corresponding nginx HTTP configuration is as follows:

 
HTTP {    server {            server_name example.com;            location/mail/{                    proxy_pass http://example.com:protmail/;            }            location/com/{                    proxy_pass http://example.com:portcom/main/;            }            Location/{                    proxy_pass http://example.com:portdefault;}}    }

  

 
    • The above configuration forwards the request ( GET and the request is forwarded) in accordance with the following rules POST :
    • http://example.com/mail/forward the next request to thehttp://example.com:portmail/
    • http://example.com/com/forward the next request to thehttp://example.com:portcom/main/
    • Forward all other requests tohttp://example.com:portdefault/

It is important to note that in the above configuration, the proxy settings forWebdefault are not specified, and the proxy settings for webmail and webcom are specified by the URI ( / and /main/ ).
If the proxy server address is prefixed with a URI, this URI replaces the location matching URI portion.
If the proxy server address is not prefixed with a URI, the full request URL is forwarded to the proxy server.

Official Document Description:

If The URI is specified along with the address, it replaces the part of the request URI that matches the location Paramete R.
If the address is specified without a URI, or it's not possible to determine the part's URI to being replaced, the full req Uest URI is passed (possibly, modified).

Example of forwarding for the above configuration:

      • http://example.com/mail/index.html-http://example.com:portmail/index.html
      • http://example.com/com/index.html-http://example.com:portcom/main/index.html
      • http://example.com/mail/static/a.jpg-http://example.com:portmail/static/a.jpg
      • http://example.com/com/static/b.css-http://example.com:portcom/main/static/b.css
      • http://example.com/other/index.htm-http://example.com:portdefault/other/index.htm

Transferred from: http://blog.csdn.net/tobacco5648/article/details/51099426

Nginx Reverse proxy (Request forwarding-url matching rule)

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.