"Go" How to set Nginx virtual host on Ubuntu 14.04 LTS

Source: Internet
Author: User
Tags aliases nginx server root access vps server

Introduction

Transfer from http://www.pandacademy.com/%E5%A6%82%E4%BD%95%E5%9C%A8ubuntu-14-04-lts%E4%B8%8A%E8%AE%BE%E7%BD%AEnginx%E8%99 %9a%e6%8b%9f%e4%b8%bb%e6%9c%ba/#i-3

When using an Nginx Web server, you can use server blocks (similar to a virtual host in Apache) to encapsulate configuration details and host multiple domains for a single server.

In this tutorial, we'll discuss how to configure a server block in Nginx on an Ubuntu 14.04 server.

Prerequisites

In this tutorial, we will use a non-root user with sudo permissions. If you do not have such a user configured, you can generate a user by following step 1-4 in the Ubuntu 14.04 Initial Server Setup tutorial.

You also need to install Nginx on the server. If you need to have the entire lemp (Linux,nginx,mysql and PHP) stack on the server, you can follow the tutorial on setting up a lemp stack in Ubuntu 14.04. If you only need Nginx, type the following to complete:

updatesudo apt-get install nginx

Once these requirements are met, you can continue to use this tutorial.

For demonstration purposes, we will use the Nginx server to set up two domains. The domain names we use in this tutorial are example.com and test.com.

The first step is to set the new document root directory

By default, Nginx on Ubuntu 14.04 enables a server block. It is configured to provide documentation from a directory in the following location:

/usr/share/nginx/html

We will not use default values because we are more likely to work with content in the/var/www directory. By default, Ubuntu's Nginx package does not use/var/www as its document root directory.

Because we are the user and not the package maintainer, we can tell Nginx this is where the document root is needed. Specifically, we need to create a directory for each site in the/var/www directory, and we need to have a directory named HTML underneath these directories to hold the actual files.

You first need to create the necessary directories. We can use the following command to do this. The-p flag enables mkdir to create any required parent directories in the following ways:

sudo mkdir -p /var/www/example.com/htmlsudo mkdir -p /var/www/test.com/html

Now that the catalog is created, we need to transfer all rights to regular users. Use $USER environment variables to replace the currently logged-in user account, which allows users to create files in this directory without allowing visitors to create content.

sudo chown -R $ USER:$ USER /var/www/example.com/htmlsudo chown -R $ USER:$ USER /var/www/test.com/html

If the user does not modify its Umask value, the Web root's permissions should be correct, but you can also type the following to ensure its correctness:

755 /var/www

Now that you have configured the directory structure, you can continue with the operation.

The second step is to create a sample page for each site

Now that the directory structure is set up, you can create a default page for each site so that you can display some content.

Create a index.html file in the first domain:

nano /var/www/example.com/html/index.html

Create a really basic file inside the file that indicates the site you are currently visiting. It will be as follows:

<html>    <head>        <title>Welcome to Example.com!</title> </head> <body> <h1>Success! The example.com server block is working!</h1> </body></html>

Save and close the file when you are finished.

Because the second Web site's files are basically the same, you can copy them to the second document root as follows:

cp /var/www/example.com/html/index.html /var/www/test.com/html/

You can now open and modify a new file in the editor so that it references the second field:

nano /var/www/test.com/html/index.html
<html>    <head>        <title>Welcome to Test.com!</title> </head> <body> <h1>Success! The test.com server block is working!</h1> </body></html>

Save and close the file when you are finished. Some pages will now be displayed to two domain visitors.

The third step is to create the server blocks (server block) file for each domain

Now that we have what we want to serve, we also need to actually create the server block, which will tell Nginx how to do it.

By default, Nginx contains a server block named default, which we can use as a configured template. First, you design the server block for the first domain, then copy it for the second domain and make the necessary modifications.

Create a first server block file

Create the first server block configuration file by copying the default file as described above:

sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/example.com

Now open the new file created in the text editor with root permissions:

sudo nano /etc/nginx/sites-available/example.com

Ignoring the commented lines, the file will resemble the following:

server {    listen 80 default_server;    listen [::]:80 default_server ipv6only=on; root /usr/share/nginx/html; index index.html index.htm; server_name localhost; location / { try_files $uri $uri/ =404; }}

The first thing to note is the Listen directive. Only one server block can have the Default_server specification. If the requested server_name does not match any of the available server blocks, it specifies which block should be used for the server request.

We will eventually disable the default server block configuration, so you can put the Default_server option in this server block, or in one of the other sites. The Default_server option is enabled in this server block, but users can choose the option that best suits them.

Next you need to adjust the document root, which is specified by the root directive. Point it to the document root of the user-created web site:

root /var/www/example.com/html;

NOTE: Each Nginx statement must end with a semicolon (;) and, if a problem is encountered, check each line of statements.

Next, modify the server_name to match the request for the first domain. We can add any aliases that we want to match. A www.example.com alias is added below to demonstrate:

server_name example.com www.example.com;

When you are finished, the file will look like this:

server {    listen 80 default_server;    listen [::]:80 default_server ipv6only=on; root /var/www/example.com/html; index index.html index.htm; server_name example.com www.example.com; location / { try_files $uri $uri/ =404; }}

The above is the basic configuration required. Save, and close to exit the file.

Create a second server block file

Now that we have the initial server block configuration, we can use it as a basis for the second file. Copy it to create a new file:

sudo cp /etc/nginx/sites-available/example.com /etc/nginx/sites-available/test.com

To open a new file in the editor with root privileges:

sudo nano /etc/nginx/sites-available/test.com

In this new file, we are going to look at the listen directive again. If the Default_server option is enabled in the last file, it must be removed in this file. In addition, the user must get rid of the ipv6only = on option because it can only be specified once for each address/port combination:

listen 80;listen [::]:80;

Adjust the document root directive to point to the document root of the second domain:

root /var/www/test.com/html;

Adjust the server_name to match the second field and any aliases:

server_name test.com www.test.com;

After the change, the file should look like this:

server {    listen 80;    listen [::]:80; root /var/www/test.com/html; index index.html index.htm; server_name test.com www.test.com; location / { try_files $uri $uri/ =404; }}

When you are finished, save and close the file.

Step fourth enable the server block and restart Nginx

The server blocks are now created, and then you need to enable them.

By creating symbolic links from these files to the sites-enabled directory that nginx reads from during startup to complete the boot.

Type the following to create these links:

/etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/sudo ln -s /etc/nginx/sites-available/test.com /etc/nginx/sites-enabled/

These files are now located in the enabled directory. However, the default server block file that we use as a template is also enabled and conflicts with files that have the Default_server parameter set.

Disable the default server block file by removing the symbolic link. It can still be referenced in the sites-available directory, but it will not be read by Nginx at startup:

sudo rm /etc/nginx/sites-enabled/default

We also need to really quickly adjust a setting in the default Nginx configuration file. Type the following to open:

sudo nano /etc/nginx/nginx.conf

Just cancel one line of comments to find and delete this comment:

server_names_hash_bucket_size 64;

Restart Nginx to enable the changes. Type the following:

sudo service nginx restart

Nginx should now serve two domain names.

Fifth step setting up the local host file (optional)

If the user does not use their own domain name, but instead uses the virtual value, you can modify the configuration of the local computer to allow temporary testing of the Nginx server block configuration.

This will prevent other visitors from viewing the user's site correctly, but it will allow the user to independently access each site and test its configuration. This is basically done by intercepting requests that usually go to DNS to resolve domain names. Conversely, when requesting a domain name, users can also set the IP address that they want the local computer to access.

Make sure to operate these steps on the local computer instead of on the VPS server. The user needs to have root access, or become a member of the administrative group, or do this by editing the system files.

If you are using a Mac or a Linux computer at home, type the following to edit the file you want:

sudo nano /etc/hosts

The user needs the public IP address of their server and the domain to be routed to the server. Assuming the server's public IP address is 111.111.111.111, the line that will be added to the file looks like this:

127.0.0.1 localhost127.0.0.1 guest-desktop111.111.111.111 example.com111.111.111.111 test.com

This intercepts any requests for example.com and test.com and sends them to the user's server, which is the desired result if the user does not actually own the domain being used.

Save and close the file when you are finished.

Sixth Step test results

is now set up, the user should test whether the server block is working properly. Complete by accessing the domain in the Web browser:

http://example.com

The page should appear as follows:

If you want to access a second domain name, you should see a slightly different Web site:

http://test.com

If all two sites are working, you have successfully configured two separate server blocks using Nginx.

At this point, if the user adjusts the host files for testing on the local computer, you may need to delete the rows that you added.

Conclusion

Users should now be able to create server blocks for each domain that they want to host from the same server, so long as their hardware can handle traffic, there is no real limit to the number of server blocks that can be created.

"Go" How to set Nginx virtual host on Ubuntu 14.04 LTS

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.