Multi-SERVER_NAME sequence of nginx Virtual Hosts

Source: Internet
Author: User
Tags mediawiki
The order of multiple SERVER_NAME for nginx virtual hosts is large | medium | small [| by banquet] [Article banquet Article version: V1.0 last modified: 2008.11.28 reprinted please note the original article link: http://blog.zyan.cc/post/382/]

Today, I found a problem in configuring nginx + PHP + mediawiki: The nginx virtual host where mediawiki is located is bound to multiple domain names, but no matter what domain name is used to access the mediawiki homepage, will be redirected to one of the domain names. The nginx configuration file does not have relevant rewrite jump rules, so it should be a jump made by the PHP program of mediawiki. However, after traversing all the files in the mediawiki directory and querying every table in the MySQL database, no such domain name is found. Later, by checking the source code, we found that mediawiki was based on the jump of $ _ server ['server _ name'] and found the following problems:

In an nginx virtual host, you can bind multiple SERVER_NAME, for example:
  

The order of SERVER_NAME is different. It may affect the use of $ _ server ["SERVER_NAME"] Or getenv ('server _ name') in PHP programs to obtain server domain names:
  

  

$ _ Server ["SERVER_NAME"] Or getenv ('server _ name') always gets the first domain name in the nginx SERVER_NAME configuration, which requires attention during program development. The first domain name is equivalent to servername in the Apache Virtual Host Configuration, and the domain name is equivalent to Apache serveralias.

PS: The following is a message from the netizen Daze, hoping to help readers in this article.
Reference in some cases (For details, refer to wiki.nginx.org), nginx internal redirection rules will be started. For example, when the URL points to a directory and does not contain "/" at the end, nginx automatically performs a 301 redirection internally. There are two situations:
1. server_name_in_redirect on (default), URL redirection: The first domain name in SERVER_NAME + directory name + /;
2. server_name_in_redirect off. URL redirection: domain name + directory name +/in the original URL /.

When you have multiple domain names pointing to the same virtual host, and you write 301 redirection rules to merge them into a specific domain name, the situation is more complicated:
First, nginx checks the URL. If the condition is met, use this rule (which you wrote) for the first redirection. Then, check the newly generated URL. If the condition of internal automatic redirection is met, use the rules mentioned above to perform another redirection.

In PHP, $ _ server ["SERVER_NAME"] is provided by the nginx variable $ SERVER_NAME by default. In this case, it has no relationship with redirection, it is always the first domain name in the SERVER_NAME setting, but this can be changed. In your nginx configuration, find the fastcgi_param section and modify
Fastcgi_param SERVER_NAME $ SERVER_NAME;
Is
Fastcgi_param SERVER_NAME $ host;
However, you must note that the $ _ server ["SERVER_NAME"] at this time will be affected by the rewrite rules you write and nginx.

Now it is clear that if mediawiki uses $ _ server ["SERVER_NAME"] to process the URL itself, in the default environment of nginx + PHP, it will always be the first domain name in the SERVER_NAME setting, so "no matter what domain name is used to access the mediawiki homepage, it will be redirected to one of the domain names. ", This is not caused by nginx redirection. Although server_name_in_redirect is on by default, the impact of this command is only the internal redirection rules of nginx. Therefore, when you use a multi-domain virtual host in the nginx + PhP environment and use $ _ server ["SERVER_NAME"] extensively in your PHP library, framework, and code, you may:
1. Set fastcgi_param SERVER_NAME $ host;
2. Set server_name_in_redirect off, so that nginx does not use the first domain name set in SERVER_NAME by default when handling internal redirection;
3. Do not use nginx rewrite rules to redirect or merge multiple domain names.
Of course, the last two items are completely optional, provided that you know what you are doing and be careful with the $ _ server ["SERVER_NAME"], it may be better to maintain fastcgi_param SERVER_NAME $ SERVER_NAME;, and then use $ _ server ["SERVER_NAME"] and $ _ server ["http_host"] reasonably.

This issue is indeed very subtle. Maybe I still don't fully understand it. Fortunately, there is still curl, And I have studied it slowly. :-)

In versions earlier than p.s. nginx 0.7.x, the command optimize_server_names affects internal redirection rules.

Multi-SERVER_NAME sequence of nginx Virtual Hosts

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.