Nginx Server to configure a two-level domain name and a number of domain names to explain the example _nginx

Source: Internet
Author: User
Tags auth nginx server subdomain

Personality level Two domain name
personalized two-level domain name under configuration.
Effect:
URL to access http://custom.jb51.net
The actual URL Http://www.jb51.net/auth/custom

Our approach is to convert the URL of the access to the actual URL through the server configuration

Here we will use the Nginx configuration. The configuration is as follows:

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

The IF, is used to filter some special level two domain names, such as www, and then get the level two domain name variable value.

Rewrite into the corresponding directory

Rewrite ^/$/auth/$subdomain last;

Plus Nginx's reverse proxy function:

Proxy_pass http://www.jb51.net/;
can be achieved.

After this configuration, in addition to the two-level domain name filtered in if, the other two-level domain name {sudomain}.jb51.net, for the server, its true path is Www.jb51.net/auth/{sudomain}.

If the URL has multiple path rules, it needs to be configured.

Multi-domain name configuration
Nginx binding multiple domain names can be a number of domain name rules to write a configuration file, but also to establish a number of domain name configuration files, I generally for the convenience of management, each domain name to build a file, some similar domain name can also be written in a general configuration file.
One, each domain name one file's wording
First open the Nginx domain name configuration file storage directory:/usr/local/nginx/conf/servers, such as to bind the domain name www.jb51.net A file is built in this directory: www.your-domain.conf then write the rules in this file, such as: Server

{

listen
;
server_name
www.jb51.net;
 #绑定域名

index
 index.htm index.html index.php; #默认文件

root
 /home/www/jb51.net; #网站根目录

include
 location.conf #调用其他规则, can also be removed

}

Then restart the Nginx server, the domain name is bound successfully nginx Server reboot command:/etc/init.d/nginx restart
Second file multiple domain names
A file to add more than one domain name rule is the same, as long as the above single domain name repeatedly write down on the OK, such as: Server

{

listen
;

server_name

bbs.jb51.net;
 #绑定域名

index
 index.htm index.html index.php; #默认文件

root
 /home/www/bbs.jb51.net;
 #bbs目录

include
 location.conf #调用其他规则, can also be removed

}server

{

listen
;

server_name

www.jb51.net;
 #绑定域名

index
 index.htm index.html index.php; #默认文件

root
 /home/www/www.jb51.net;
 #网站根目录

include
 location.conf; #调用其他规则, can also be removed

}

Third, do not take the WWW domain name plus 301 Jump
If you do not take the WWW domain name to add 301 jump, that is the same as binding domain name, first bound without the WWW domain name, just do not write the site directory, but for 301 jump, such as:

Server

{

listen
;

server_name

jb51.net;

Rewrite
 ^/(. *) http://www.jb51.net/$1 permanent;

}

Four, add 404 Web pages

Add 404 pages, can be added directly inside, such as:

Server

{

listen
;

server_name
 www.jb51.net #绑定域名

index
 index.htm index.html index.php; #默认文件

root
 /home/www/ Jb51.net; #网站根目录

include
 location.conf #调用其他规则,

error_page 404/404.html can also be removed

;



Learn the above four kinds of rules and methods, the basic can independently solve Nginx multiple domain name configuration problem

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.