Nginx proxy_pass to $ host

Source: Internet
Author: User
Today, when configuring a location, you want to use a variable such as $ host to indicate the nginx Proxy: locationtest {proxy_passhttp: $ host;}. If you do not think of it, this configuration cannot be used, view error. log, and the output information cannot help solve the problem. But in the same case, the root tag works well: loca

Configuring a lo todayCatIon, you want to use a variable such as $ host to indicate the nginx Proxy:

Location/test /{
Proxy_pass http: // $ host;
}

If you don't think of it, this configuration cannot be used. You can view error. log and the output information cannot help solve the problem.

But in the same case, the root tag works well:

Locatin/test /{
Root/dev/shm/$ host;
}

What is incredible. Is it estimated that this is a bug of nginx or an ambiguous logic?

Change the above error configuration

Location/test /{
Proxy_pass http: // $ host /;
}

Or

Set$ Vhost test.SuDone.com;
Location/test /{
Proxy_pass http: // $ vhost;
}

The problem cannot be solved. In the end, a strange configuration method is used to work ......

Location/test /{
Proxy_pass http: // 127.0.0.1: 80;
Proxy_set_header Host $ host;
}

This configuration can work normally, but the above configuration only supports the backend of A Single ip address, can it support ups?TrWhat about eam?

Change the test

Upstream backend.sudone.com {
Server 127.0.0.1: 80;
}

Location/test /{
Proxy_pass http://backend.sudone.com; # fixed write back-end upstream
Proxy_set_header Host $ host; # $ host = test.sudone.com;
}

Hmm? Usable!

With this method, I do not need to mount the/test/location dozens of times in dozens of host configurations. In addition, it supports the following syntax:

Location/test /{
Proxy_pass http: // 127.0.0.1: 80;
Proxy_set_header Host fetch. $ host;
}

You can also useRegular ExpressionThe expression matches the host:

Set $ vhost $ host;
If $ host ~ * ([^.] +) .Sudone.com {
Set $ vhost $ 1.xxx.com;
}

Location/test /{
Proxy_pass http: // 127.0.0.1: 80;
Proxy_set_header Host fetch. $ host;
}

Wide Range of availability.

---------------------------------------------------

Supplement:

In this configuration, the backend of my nginx proxy is also nginx. It is not clear whether other web servers, such as apache, can work normally under this configuration.

Related Article

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.