Nginx configures static pages, virtual hosts (IP, port, domain name) and log files based on source code installation

Directory

1. Static page

1. Change page content

2. Change the configuration file

3. Test

2. Virtual host configuration

1. Based on IP

(1) Create a new directory under the html directory to store test files

(2) Modify the nginx.conf file and configure two server modules corresponding to two IPs in the http module

(3) test

2. Port-based

(1) Create a test interface

(2) Also add the server module to the http module, pay attention to the change of the working directory

(3) test

3. Based on domain name

(1) Add a line of server_name based on the previous IP

(2) Modify the /etc/hosts file

(3) test

3. nginx error log and access log configuration

1. Specify the log type and log storage directory in the server module

2. Check after testing both sides


1. Static page

To shut down apache’s httpd

[root@localhost conf]# systemctl status httpd.service
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
     Docs: man:httpd(8)
           man:apachectl(8)

1. Change page content

Find the html directory in your nginx installation directory and modify index.html

[root@localhost html]# pwd
/usr/local/src/nginx-1.22.0/html
[root@localhost html]# cat index.html
<h1>nginx</h1>

2. Change configuration file

The nginx.conf file in the nginx installation directory

[root@localhost conf]# pwd
/usr/local/src/nginx-1.22.0/conf
[root@localhost conf]# vim nginx.conf

Find the block location in serve, change the working directory and specify the html file

server {
        listen 80;
        server_name localhost;

        #charset koi8-r;

        #access_log logs/host.access.log main;

        location / {
            root /usr/local/src/nginx-1.22.0/html; #Your html file storage directory
            index index.html; #If the previous page file has another name, it must be changed here
        }

        #error_page 404 /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
            root html;
        }
}
[root@localhost conf]# systemctl restart nginx

057dca4f870042a99d556089b77306dd.png

3. Test

503b74fcd1a44135b90eed1f872e9003.png

Two. Virtual host configuration

1. Based on IP

(1) Create a new directory under the html directory to store test files

[root@localhost html]# ll
total 8
-rw-r--r-- 1 1001 1001 497 May 24 2022 50x.html
-rw-r--r-- 1 1001 1001 15 Aug 10 10:13 index.html
drwxr-xr-x 2 root root 43 Aug 10 11:53 ip
drwxr-xr-x 2 root root 6 Aug 10 11:51 port
[root@localhost html]# pwd
/usr/local/src/nginx-1.22.0/html
[root@localhost html]# cat ip/index.html ip/index1.html
190
195

(2) Modify the nginx.conf file, and configure two server modules in the http module to correspond to Two IP

server {
                listen 192.168.2.190;
                location / {
                root /usr/local/src/nginx-1.22.0/html/ip;
                index index.html index.htm;
                }
        }
        server {
                listen 192.168.2.195;
                location / {
                root /usr/local/src/nginx-1.22.0/html/ip;
                index index1.html index.htm;
                }
        }
[root@localhost conf]# systemctl restart nginx

(3) Test

882c60b7f0474e34929b7e4d6bcfd0f4.png

1f03213f5bd7406d8baa044b69930e90.png

2. Based on port

(1) Create test interface

[root@localhost html]# cat port/index.html port/index1.html
8090
8099

(2) Also add the server module to the http module, pay attention to the change of the working directory

server {
                listen 8090;
                location / {
                root /usr/local/src/nginx-1.22.0/html/port;
                index index.html index.htm;
                }
        }
        server {
                listen 8099;
                location / {
                root /usr/local/src/nginx-1.22.0/html/port;
                index index1.html index.htm;
                }
        }
[root@localhost conf]# systemctl restart nginx

(3) Test

0cb93d28ad46430284142b21bae21668.png

bdcb4441fb1141058883dc3520a7e4ba.png

3. Based on domain name

(1) Just add a line of server_name based on the previous IP

server {
                listen 192.168.2.190;
                server_name www.aabb.com;
                location / {
                root /usr/local/src/nginx-1.22.0/html/ip;
                index index.html index.htm;
                }
        }
        server {
                listen 192.168.2.195;
                server_name www.llss.com;
                location / {
                root /usr/local/src/nginx-1.22.0/html/ip;
                index index1.html index.htm;
                }
        }
[root@localhost conf]# systemctl restart nginx

(2) Modify /etc/hosts file

Linux

[root@localhost conf]# vim /etc/hosts
192.168.2.190 www.aabb.com
192.168.2.195 www.llss.com

windows

Use notepad to modify the hosts file through powershell

Windows PowerShell
Copyright (C) Microsoft Corporation. all rights reserved.

Install the latest PowerShell to learn about new features and improvements! https://aka.ms/PSWindows

PS C:\WINDOWS\system32> cd .\drivers\etc\

PS C:\WINDOWS\system32\drivers\etc> notepad .\hosts
PS C:\WINDOWS\system32\drivers\etc>

c046652771624e63941ac79430d6f945.png

(3) Test

a5732732f14c404db2712b4703ed4996.png

e6e984ec688e4813b278e03ee9658a43.png

Three. nginx error log and access log configuration

1. Just specify the log type and log storage directory in the server module

2. Check after testing both sides

[root@localhost logs]# cat access.log
192.168.2.2 - - [10/Aug/2023:14:06:00 + 0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML , like Gecko) Chrome/115.0.0.0 Safari/537.36 Edg/115.0.1901.200"
192.168.2.2 - - [10/Aug/2023:14:06:00 + 0800] "GET /favicon.ico HTTP/1.1" 404 555 "http://192.168.2.190/" "Mozilla/5.0 (Windows NT 10.0 ;Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36 Edg/115.0.1901.200"
[root@localhost logs]# cat error.log
2023/08/10 14:06:00 [error] 59751#59751: *1 open() "/usr/local/src/nginx-1.22.0/html/ip/favicon.ico" failed (2: No such file or directory), client: 192.168.2.2, server: www.aabb.com, request: "GET /favicon.ico HTTP/1.1", host: "192.168.2.190", referrer: "http://192.168.2.190 /"
[root@localhost logs]# pwd
/usr/local/src/nginx-1.22.0/logs

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Network skill treeHomepageOverview 37220 people are studying systematically