Nginx uses mod_proxy to implement reverse proxy configuration

Source: Internet
Author: User

Because the project needs to implement a domain name proxy function
For example:

The code is as follows: Copy code
A.com/a.html=> B .com/ B .html

That is to say, I used a.com to proxy the B .com website request. After a friend introduced it, I used apache's reverse proxy to achieve this goal. It was really nice to have a try. configuration is also super convenient.

(Note that the following section only describes reverse proxy and forward proxy. I will introduce it later)

1. Start the mod_proxy module of apache.

The code is as follows: Copy code

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so

Note: mod_proxy_http.so. During the test, the agent fails if I didn't add this module. After I add this module, the problem disappears.

2. Implement proxy statements

The code is as follows: Copy code


ProxyPass/mirror/foo/http://backend.example.com/

All requests under the current host/mirror/foo/directory are distributed to backend.example.com.
According to my requirements:
Add

The code is as follows: Copy code
Http:// B .com/ProxyPass/

Everything OK

3. After domain name proxy, note the following:
3.1: it is best to use relative paths for URLs on websites. If absolute paths are used, the proxy is easily bypassed, and the hosts behind the proxy server are easily exposed.
3.2: note that when referencing the global variable $ __server ['server _ name'], the domain name of the current server_name is obtained. if you want to obtain the proxy domain name, you need to get the proxy domain name through $ __server ['http _ X_FORWARDED_FOR. note: it is generally used for operations such as cookie writing.

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.