Getting started with nginx configuration

This article is in ubuntu (20), the English original text is implemented under ubuntu16. It is to configure 2 virtual hosts to access different WEB home directories with different domain names.

The two domain names are example.com and test.com. You don't need to have these two domain names, they are just for testing.

Reference original text: https://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-server-blocks-virtual-hosts-on-ubuntu-16-04

Prerequisites

The content of this article does not include the installation of nginx, you can refer to: Nginx installation on Ubuntu, testing

In fact, the installation is:

sudo apt update
sudo apt install nginx

This article requires you to have a non-root user, but belong to the sudo group. If not, do the following under the root user:

adduser sammy
usermod -aG sudo sammy

Create a new Web root directory

By default, Ubuntu's Web root directory is /var/www/html, which is the situation after nginx is installed.

Now create the Web root directories of two domain names, namely example.com and test.com.

-p means to create the necessary parent directory.

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

Change the owner and group of the directory 

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

Change directory permissions:

sudo chmod -R 755 /var/www

Create 2 sample pages

Create index.html for the first domain name

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

The content is as follows:

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

The content is very simple is to display The example.com server block is working.

Create index.html for the second domain name

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

The content is as follows:

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

 Our goal is to visit different domains and display different web pages.

Server block configuration

Configure the first domain name server block:

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

The content is:

server {
        listen 80;
        listen [::]:80;

        root /var/www/example.com/html;
        index index.html index.htm index.nginx-debian.html;

        server_name example.com www.example.com;

        location / {
                try_files $uri $uri/ =404;
        }
}

Configure the second domain name server block:

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

The content is:

server {
        listen 80;
        listen [::]:80;

        root /var/www/test.com/html;
        index index.html index.htm index.nginx-debian.html;

        server_name test.com www.test.com;

        location / {
                try_files $uri $uri/ =404;
        }
}

Enable configuration and restart nginx

The configuration file under /etc/nginx/sites-available/ will not participate in the configuration and must be linked to the /etc/nginx/sites-enable/ directory

The enabling operation is as follows:

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

In order to avoid the memory problem of the hash bucket that may be caused by adding other server names, we adjust a single value in the /etc/nginx/nginx.conf file. open a file:

sudo nano /etc/nginx/nginx.conf

Find the server_names_hash_bucket_size item in the file, and cancel the previous comment.

http {
    . . .

    server_names_hash_bucket_size 64;

    . . .
}

Save and close the file.

Next, test whether the entire configuration is grammatically correct.

sudo nginx -t

In fact, it can also:

sudo nginx -T

Capital T checks the syntax and displays the configuration content. Display the contents of the main configuration file, the contents of the 2 server configuration blocks, and the contents of the include in the configuration file.

For configuration errors, you can look at the contents of the /etc/nginx/sites-enbale/ directory, delete it if it is not necessary, maybe it is the error caused by it.

If the check is correct and no error is reported, we will restart nginx:

sudo systemctl restart nginx

Modify the local hosts file for testing

This must be the address of the browser client where you are accessing the host.

The domain name resolution is first to use the local hosts file, and then to the Internet. The address of this file is /etc/hosts under linux, and C:\Windows\System32\drivers\etc under windows.

You can view the English link: https://www.howtogeek.com/howto/27350/beginner-geek-how-to-edit-your-hosts-file/ or my blog  DNS domain name resolution protocol and local buffer

The content of the file plus the resolution of our 2 domain names:

127.0.0.1   localhost
. . .

203.0.113.5 example.com www.example.com
203.0.113.5 test.com www.test.com

Special attention here: 203.0.113.5 is the sample address, you must change the ip address of your host, otherwise the access is wrong.

Test Results

Type in the browser: example.com

It should display as follows:

Type in the browser: test.com

It should display as follows:

 

If this test is successful, our experiment is complete. The actual domain name does not need to be resolved by hosts, but you need to purchase it. You can set up multiple virtual hosts on a linux host.

Guess you like

Origin blog.csdn.net/leon_zeng0/article/details/108820539