Nginx about the configuration of the personality level two domain name

Source: Internet
Author: User
Tags subdomain

In many places, such as blog sites. We can often use the configuration features provided by the website to achieve personalized two-level domain names belonging to individuals, such as http://cevin15.oschina.net. Of course, the OSC blog does not yet have this feature.

Recently encountered a similar demand, on the Internet to see the realization of others, writing is not very clear, the first time even if you understand, look back to the second time estimate still do not understand. Test it yourself, then record it. So play the local nginx, study how to achieve this personality two-level domain name.

The first time to think of is through the Nginx rewrite to achieve. The configuration is as follows:

server {        listen       ;        server_name  *.mydomain.com;        if ($host ~* (\b! www\b) (\w+) \.\w+\.\w+) {            set $subdomain $        ;        } Location/{            rewrite ^/$ http://www.mydomain.com/$subdomain last;        }    }

where if, is the variable value used to get a level two domain name.

Heavy-duty Nginx configuration, testing. The jump is found to be normal, but the URL of the browser also becomes: http://www.mydomain.com/$subdomain.

This is useless, can't get the cool level two domain name function that we need.

The URL of the browser will change because we use the full-path configuration when we use rewrite.

Since the following writing is not possible:

Rewrite ^/$ http://www.mydomain.com/$subdomain last;

then change it back to this type of notation:

Rewrite ^/$/$subdomain last;

plus Nginx's reverse proxy function:

Proxy_pass http://www.mydomain.com/;

you can implement a cool two-level domain name feature.

all configurations are as follows:

server {        listen       ;        server_name  *.mydomain.com;        if ($host ~* (\b! www\b) (\w+) \.\w+\.\w+) {            set $subdomain $        ;        } Location/{            rewrite ^/$/$subdomain last;            Proxy_pass http://www.mydomain.com/;        }    }

This is the current thought of the implementation of the two-level domain name, the idea is actually very simple, the defect of course, is the rewrite above. If the URL has multiple path rules, it needs to be configured one at a.

If there is a better way to implement it later, update it. Cond......

Nginx about the configuration of the personality level two domain name

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.