Three ways to configure Nginx virtual host (2) (based on port)

Posted by chanchelkumar on Tue, 05 May 2020 03:35:56 +0200

2. Port based virtual host configuration of Nginx

If a server has only one IP or needs to access different virtual hosts through different ports, port based virtual host configuration can be used.

2.1 suppose the server has an IP address of 192.168.2.154

[root@localhost conf]# ifconfig ens33:4 192.168.2.154/24 up
[root@localhost conf]# ifconfig
ens33:4: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.2.154  netmask 255.255.255.0  broadcast 192.168.2.255
        ether 00:0c:29:16:90:ae  txqueuelen 1000  (Ethernet)

2.2 the virtual hosts to be configured are 7081, 8081 and 9081 respectively. The host file of the host is easy to test.

[root@localhost conf]# vim /etc/hosts
[root@localhost conf]# cat /etc/hosts|grep 192.168.2.154
192.168.2.154 www.test154.com

2.3 establish the root directory where the virtual host stores the web page, and create the homepage file index.html

[root@localhost conf]# cd /data/www/
[root@localhost www]# mkdir port
[root@localhost www]# cd port/
[root@localhost port]# mkdir 7081 8081 9081
[root@localhost port]# ls
7081  8081  9081
[root@localhost port]# echo "port 7081" > 7081/index.html
[root@localhost port]# echo "port 8081" > 8081/index.html
[root@localhost port]# echo "port 9081" > 9081/index.html

2.4 modify nginx.conf to include the virtual host configuration file in the main file

[root@localhost /]# cd /usr/local/nginx/conf/
[root@localhost conf]# ls
fastcgi.conf          fastcgi_params          koi-utf  mime.types          nginx.conf          scgi_params          uwsgi_params          win-utf
fastcgi.conf.default  fastcgi_params.default  koi-win  mime.types.default  nginx.conf.default  scgi_params.default  uwsgi_params.default
[root@localhost conf]# vim nginx.conf

Add the following configuration at the end of nginx.conf file

# stay http Find the following in the section and delete the“#"
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

# Add the following statement before the last "}" at the end of the configuration file, as follows
include vhost/*.conf

2.5 edit the configuration file of each port

[root@localhost vhost]# vim www.test154.7081.conf
[root@localhost vhost]# cat www.test154.7081.conf
   server {
        listen       192.168.2.154:7081;
        # Configure as the actual domain name. The domain name of the configuration file of each virtual host is the same
        #server_name  www.test.com;

        access_log   /data/logs/www.test154.7081.log main;
        error_log    /data/logs/www.test154.7081.error.log;

        location / {
            root   /data/www/port/7081;
            index  index.html index.htm;
        }
    }

[root@localhost vhost]# vim www.test154.8081.conf
[root@localhost vhost]# cat www.test154.8081.conf
   server {
        listen       192.168.2.154:8081;
        # Configure as the actual domain name. The domain name of the configuration file of each virtual host is the same
        #server_name  www.test.com;

        access_log   /data/logs/www.test154.8081.log main;
        error_log    /data/logs/www.test154.8081.error.log;

        location / {
            root   /data/www/port/8081;
            index  index.html index.htm;
        }
    }

[root@localhost vhost]# vim www.test154.9081.conf
[root@localhost vhost]# cat www.test154.9081.conf
   server {
        listen       192.168.2.154:9081;
        # Configure as the actual domain name. The domain name of the configuration file of each virtual host is the same
        #server_name  www.test.com;

        access_log   /data/logs/www.test154.9081.log main;
        error_log    /data/logs/www.test154.9081.error.log;

        location / {
            root   /data/www/port/9081;
            index  index.html index.htm;
        }
    }

2.6 create log file, otherwise nginx cannot be started

[root@localhost /]# mkdir -p /data/logs
[root@localhost /]# touch /data/logs/www.test154.7081.log
[root@localhost /]# touch /data/logs/www.test154.7081.error.log
[root@localhost /]# touch /data/logs/www.test154.8081.log
[root@localhost /]# touch /data/logs/www.test154.8081.error.log
[root@localhost /]# touch /data/logs/www.test154.9081.log
[root@localhost /]# touch /data/logs/www.test154.9081.error.log
[root@localhost /]# ls /data/logs/
www.test154.7081.error.log www.test154.8081.error.log www.test154.9081.error.log
www.test154.7081.log       www.test154.8081.log       www.test154.9081.log

2.7 test the configuration file before starting nginx

[root@localhost /]# cd /usr/local/nginx/sbin/
[root@localhost sbin]# ./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
# start nginx 
[root@localhost sbin]# ./nginx

2.8 test documents

[root@localhost ~]# curl http://www.test154.com:7081
port 7081
[root@localhost ~]# curl http://www.test154.com:8081
port 8081
[root@localhost ~]# curl http://www.test154.com:9081
port 9081

Appendix: problems in configuration process

1. Problems in the final test

[root@localhost sbin]# curl http://www.test154.com:7081
curl: (7) Failed connect to www.test154.com:7081; connection denied
[root@localhost sbin]# curl 192.168.2.154:7081
curl: (7) Failed connect to 192.168.2.154:7081; connection denied

resolvent:

1.1 use the following command to check whether Nginx is listening to the corresponding port

[root@localhost conf]#  netstat -lnt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN
tcp        0      0 192.168.2.153:80        0.0.0.0:*               LISTEN
tcp        0      0 192.168.2.152:80        0.0.0.0:*               LISTEN
tcp        0      0 192.168.2.151:80        0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN
tcp        0      0 192.168.2.154:8081      0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN
tcp        0      0 192.168.2.154:9081      0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN
tcp        0      0 192.168.2.154:7081      0.0.0.0:*               LISTEN
tcp6       0      0 :::111                  :::*                    LISTEN
tcp6       0      0 :::22                   :::*                    LISTEN
tcp6       0      0 :::23                   :::*                    LISTEN
tcp6       0      0 ::1:25                  :::*                    LISTEN

1.2 if Nginx does not listen to the corresponding port, restart the Nginx service, and then restart the server

Topics: Nginx curl vim