Use nginx to forward access to different domain names

Source: Internet
Author: User

Use nginx to forward access to different domain names

 

After the company's image server went online, the webpage link was written to death, so it was resolved only to the IP address of China Netcom, resulting in a slow solution for telecom users to open images using the telecom domain name.
1. A better way is to use smart DNS servers. Resolve the IP addresses of China Telecom or China Netcom to the IP addresses of China Telecom and China Netcom to accelerate user access. Currently, smart DNS can be used in two ways:
(1) the current free smart DNS server is used, but the problem is that it cannot guarantee stability and it is not very convenient to make adjustments, because it is a free service, I don't know what it will do.
(2) Setting up the company's own smart DNS, which is easy to control, but requires the company to have DNS servers, machines and later maintenance. However, once set up, the application and modification are still relatively convenient.
2. Make a judgment in the program to determine the requested domain name or source IP address. However, such a complicated judgment will inevitably put a lot of pressure on Tomcat. We communicated with developers before, I feel that code control is not easy to implement, and the Development workload is heavy.
3. Try to use a web server such as nging to better support regular expressions.
(1) Anti-leech protection:
Valid_referers none blocked * .xxx.cn; ---- the Domain Name of the specified source is not a forward request from xxx.cn for anti-leech processing. Generally, the domain names are xxx.cn or xxx.net.cn, therefore, you can transfer requests from xxx.net.cn and other domain names.
If ($ invalid_referer ){
{
Rewrite "^/(. *)" http://pic.xxx.net.cn/#1 permanent;
} ---- The above if statement rewrites the request to complete request forwarding.
Anti-leech is a good way to easily forward access requests from the xxt.net.cn domain name to pic.xxt.net.cn, you only need to set up one more VM during processing, which is a good method. During the test, this method was the first to pass the test and the experiment was successful.
(2) Use the variable $ http_referer defined in nginx:
If ($ http_referer ~ * Xxx.net.cn) ----- first determine the server on which the request is forwarded. The variable $ http_referer extracts the domain name from which the local request is forwarded. If the local request is forwarded from club.xxx.net.cn, then $ http_referer is club.xxx.net.cn, if it is forwarded from club.xxx.cn, It is club.xxx.cn;
{
Rewrite "^/(. *)" http://pic.xxx.net.cn/#1 permanent;
} ----- Use rewrite for forwarding to complete request conversion;
This method is also a good method, although some detours have been encountered during the test of the judgment condition in the IF statement, however, it is still relatively smooth. In addition, the rational use of $ http_referer reduces the number of forwarded requests. In addition, anti-leech protection can be performed on both virtual hosts to speed up access.
Of course, the above two methods are because:
(1) There is no smart DNS, so it is impossible to implement the unified forwarding scheme of the Telecom and China Netcom domain names. If intelligent DNS can be implemented, it is recommended to use intelligent DNS.
(2) If the control in the Code is a waste of resources and the number of development code is relatively large, it is proposed that after the image server is launched, it can only use the Netcom domain name.
(3) nginx supports regular expressions. This is very important. Otherwise, it will not be able to handle similar situations. In Apache, this domain name-based forwarding will not be possible. Therefore, the research on nginx should be continuously studied in the application process.

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.