Nginx service deploys virtual website host

foreword

Each server only runs one website, which sometimes causes a waste of resources. At this time, the virtual host function can be used to use the physical server as multiple "virtual servers"; IP address, host domain name or port number, providing technology for multiple websites to provide external access services at the same time.

On the basis of the experiment, Nginx needs to be installed first. No matter what method is used to install it, the virtual host can be configured. For the installation service, please refer to the previous article "Deploying and Installing Nginx Service Instances " .

Install it here by compiling

server IP Serve
CentOS7 192.168.116.166 nginx1.22.1

1. Compile and install Nginx

The installation steps here are relatively fast. If Nginx has already been installed, skip this directory directly.

wget http://nginx.org/download/nginx-1.22.1.tar.gz
tar xf nginx-1.22.1.tar.gz
#安装所需依赖包
[root@localhost ~]# yum install openssl openssl-devel gcc  -y
[root@localhost ~]# cd nginx-1.22.1
#配置需要到的组件
[root@localhost ~]#./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-pcre
[root@localhost nginx-1.22.1]# echo $?
0
#开始编译
[root@localhost nginx-1.22.1]# make && make install && echo "OK"

Nginx compiles and installs very quickly, and when you see OK on the screen, it means that it has been installed.
insert image description here
Then you can make a soft connection so that the nginx command can be used in any path.

[root@localhost nginx]# ln -sv sbin/nginx /usr/sbin/
"/usr/sbin/nginx" -> "sbin/nginx"
[root@localhost nginx]# ll /usr/sbin/nginx 
lrwxrwxrwx. 1 root root 10 328 15:45 /usr/sbin/nginx -> sbin/nginx

2. Configure the virtual host

Configuring a virtual host requires adding content to the Nginx configuration file.

[root@localhost nginx]# ln -sv /usr/local/nginx/sbin/nginx /usr/sbin/nginx
"/usr/sbin/nginx" -> "/usr/local/nginx/sbin/nginx"
[root@localhost nginx]# ll /usr/sbin/nginx 
lrwxrwxrwx. 1 root root 27 328 15:48 /usr/sbin/nginx -> /usr/local/nginx/sbin/nginx

If you don't know where the configuration file is, you can use the find command to find it.

2.1 Parameters of the configuration file

nginx consists of modules, which are controlled by directives specified in configuration files. Instructions are divided into simple instructions and block instructions. Simple directives consist of a name and parameters, separated by spaces and ;terminated with a semicolon. A block directive has the same structure as a simple directive, but it {}ends with a set of additional directives surrounded by curly braces instead of semicolons. If a block directive can contain other directives within curly braces, it is called a context (examples: events, http, server, and location).
We will need to deploy virtual website hosts next, so we only need to know a few.

    server {
    
        #http server块,用来配置监听端口和域名的地方
        listen       80;    #监听端口
        server_name  localhost;   #服务器名称,url要访问的域名或IP

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
    
        #匹配url规则
            root   html;    	#网站网页存放路径
            index  index.html index.htm;   #文件后缀,如果是php的后缀,可以在这里加上index.php
        }

If you want to know more, you can check it from the nginx documentation on the official website—click here
Then we will start to configure it.

2.2 Port-based access

Port access is relatively easy, you only need to modify the listening port, we first create a directory path to be used later to store the index.html file displayed on the web page.

[root@localhost nginx]# mkdir -p /data/html80  /data/html81
[root@localhost nginx]# echo "use 80 port to visit" > /data/html80/index.html
[root@localhost nginx]# echo "use 81 port to visit" > /data/html81/index.html

2.2.1 Modify the configuration file

You only need to modify the configuration port listen的80, then copy server httpthe module to line 80, change port 80 to 81; and you need to modify the directory path of index.html in the file.

[root@localhost nginx]# vim /usr/local/nginx/conf/nginx.conf
 35     server {
    
    
 36         listen       80;    
 37         server_name  192.168.116.166;
 38 
 39         #charset koi8-r;
 40 
 41         #access_log  logs/host.access.log  main;
 42 
 43         location / {
    
    
 44             root   /data/html80;    #目录路径
 45             index  index.html index.htm;
 46         }

 80      server {
    
    
 81          listen      81;
 82          server_name  192.168.116.166;
 83 
 84          #charset koi8-r;
 85 
 86          #access_log  logs/host.access.log  main;
 87 
 88          location / {
    
    
 89              root   /data/html81;
 90              index  index.html index.htm;
 91          }
 92 }

Note: If you want to type the server module information by hand, remember to add a semicolon after each line for the content in the middle, otherwise an error will be reported.
Direct use nginx -tcan also know whether the configuration file is wrong;

[root@localhost nginx]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

Then start nginx access. Note that I made a soft link before. If the command is not found in this step, you can make a soft link of the command, and you need to use the full path.

ln -sv /usr/local/nginx/sbin/nginx /usr/sbin/nginx

Enter nginx directly on the terminal to start the service

[root@localhost nginx]# nginx

2.2.2 Access Effect

Use curl to access directly in the terminal

[root@localhost nginx]# curl 192.168.116.166
use 80 port to visit
[root@localhost nginx]# curl 192.168.116.166:81
use 81 port to visit

To access on the webpage, you need to turn off the firewall and selinux before you can access successfully.
insert image description here
insert image description here

2.3 IP-based access

Based on IP access, multiple IPs are required. On the physical machine, you need to apply for an external network IP. Of course, we are using vmware now, so adding a temporary IP can be used for experiments.

[root@localhost nginx]# ifconfig ens32:0 192.168.116.167/24
[root@localhost nginx]# hostname -I
192.168.116.166 192.168.116.167 192.168.122.1 

The directory file that needs to be accessed is recreated here;

[root@localhost nginx]# mkdir -p /data/html166 /data/html167
[root@localhost nginx]# echo "<h1>this is ip 166</h1> " > /data/html166/index.html
[root@localhost nginx]# echo "<h1>this is ip 167</h1> " > /data/html167/index.html

2.3.1 Modify configuration file

The part that needs to be modified based on IP access is that server_namebecause it is a different IP, you need to add a server module, and write server_nameanother IP in it. locationThe url access path under the rule also needs to be modified to the previously created directory file.

[root@localhost nginx]# vim /usr/local/nginx/conf/nginx.conf
 35     server {
    
    
 36         listen       80;
 37         server_name  192.168.116.166;
 38 
 39         #charset koi8-r;
 40 
 41         #access_log  logs/host.access.log  main;
 42 
 43         location / {
    
    
 44             root   /data/html166;
 45             index  index.html index.htm;
 46         }

 80      server {
    
    
 81          listen      80;
 82          server_name  192.168.116.167;
 83 
 84          #charset koi8-r;
 85 
 86          #access_log  logs/host.access.log  main;
 87 
 88          location / {
    
    
 89              root   /data/html167;
 90              index  index.html index.htm;
 91          }
 92 }

After modification, it is still used nginx -tto check whether the file has error information.

[root@localhost nginx]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

restart service

[root@localhost nginx]# nginx -s reload

2.3.2 Access results

Access the IP in the terminal

[root@localhost nginx]# curl 192.168.116.166
<h1>this is ip 166</h1> 
[root@localhost nginx]# curl 192.168.116.167
<h1>this is ip 167</h1> 

Visit on the web
insert image description here

insert image description here

2.4 Access based on domain name

Based on the domain name access, the domain name needs to be resolved first. Since it is in the virtual machine, it is directly resolved on the intranet here.

[root@localhost nginx]# vim /etc/hosts
[root@localhost nginx]# tail -2 !$
tail -2 /etc/hosts
192.168.116.166 www.itwhoami.top
192.168.116.166 blog.itwhoami.top

After parsing, create a new directory here

[root@localhost nginx]# mkdir /data/html_www/ /data/html_blog/
[root@localhost nginx]# echo "this is www index" > /data/html_www/index.php
[root@localhost nginx]# echo "this is blog index" > /data/html_blog/index.php

2.4.1 Modify configuration file

Access based on the domain name needs to be modified server_name, location规则的rootthe path. `

 35     server {
    
    
 36         listen       80;
 37         server_name  www.itwhoami.top;
 38 
 39         #charset koi8-r;
 40 
 41         #access_log  logs/host.access.log  main;
 42 
 43         location / {
    
    
 44             root   /data/html_www;
 45             index  index.php index.html index.htm;
 46         }

 80      server {
    
    
 81          listen      80;
 82          server_name  blog.itwhoami.top;
 83      
 84          #charset koi8-r;
 85      
 86          #access_log  logs/host.access.log  main;
 87      
 88          location / {
    
    
 89              root   /data/html_blog;
 90              index  index.php index.html index.htm;
 91          }
 92 }

Use again nginx -tto view the configuration file

[root@localhost nginx]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

Restart the nginx service

[root@localhost nginx]# nginx -s reload

Sometimes restarting does not take effect, you can also directly close the service and then reopen it.

[root@localhost nginx]# nginx -s stop
[root@localhost nginx]# nginx

2.4.2 Access results

access in terminal

[root@localhost nginx]# curl www.itwhoami.top
this is www index
[root@localhost nginx]# curl blog.itwhoami.top
^C
[root@localhost nginx]# vim /etc/hosts
[root@localhost nginx]# curl blog.itwhoami.top
this is blog index

When accessing the webpage, it cannot be accessed on the local server here, and it needs to be accessed on the virtual Firefox.
insert image description here
For the PHP page, Firefox downloads it by default, and we can view it directly in the file.
insert image description here
insert image description here


Summarize

The three virtual hosts have been configured here, which is quite interesting. You can also practice it yourself. If you think the content is okay, you can like it and follow it.
insert image description here

Guess you like

Origin blog.csdn.net/rhn_111/article/details/129815660