Configuration of a single iP address and multiple domain names for one server in nginx

Source: Internet
Author: User
Tags nginx server

When you bind multiple domain names to nginx, you can write multiple domain name rules into one configuration file. You can also create multiple domain name configuration files for ease of management, each domain name creates a file, and some similar domain names can also be written in a general configuration file.
1. One file for each domain name

First, open the directory where the nginx domain name configuration file is stored:/usr/local/nginx/conf/servers. To bind a domain name www.111cn.net, create a file: www.111cn.net. conf and then write rules in this file, such:

The code is as follows: Copy code

Server
{
Listen 80;
Server_name www.111cn.net; # bind a domain name
Index index.htm index.html index. php; # default file
Root/home/www/111cn.net; # Website root directory
Include location. conf; # call other rules, which can also be removed
}

Then restart the nginx server, and the domain name is successfully bound to the nginx server. restart the command:/etc/init. d/nginx restart
2. Writing multiple domain names in one file
The same applies to rules for adding multiple domain names to a file. You only need to write down the previous single domain name repeatedly, for example, server.

The code is as follows: Copy code
{
Listen 80;
Server_name www.111cn.net; # bind a domain name
Index index.htm index.html index. php; # default file
Root/home/www/111cn.net; # Website root directory
Include location. conf; # call other rules, which can also be removed
} Server
{
Listen 80;
Server_name msn.111cn.net; # bind a domain name
Index index.htm index.html index. php; # default file
Root/home/www/mb.111cn.net; # Website root directory
Include location. conf; # call other rules, which can also be removed
}

3. Add 301 redirection for domain names without www
If you want to add a 301 jump to a domain name without www, it is the same as binding a domain name. First, bind a domain name without www. Instead of writing a website directory, you can perform a 301 jump, for example:

The code is as follows: Copy code
Server
{
Listen 80;
Server_name 111cn.net;
Rewrite ^/(. *) http://www.111cn.net/#1 permanent;
}

4. Add 404 web pages

To add 404 web pages, you can add them directly in the page, for example:

The code is as follows: Copy code
Server
{
Listen 80;
Server_name www.111cn.net; # bind a domain name
Index index.htm index.html index. php; # default file
Root/home/www/111cn.net; # Website root directory
Include location. conf; # call other rules, which can also be removed
Error_page 404/404 .html;
}

After learning the above four rules and methods, you can solve the problem of nginx multi-domain configuration independently.

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.