Nginx supports three ways to configure virtual hosts: IP-based virtual host configuration, port-based virtual host configuration, and domain name-based virtual host configuration. Detailed explanation of three ways to configure Nginx virtual hosts (based on IP) https://www.jb51.net/article/14974.htm Detailed explanation of three ways to configure Nginx virtual hosts (based on domain names) https://www.jb51.net/article/14978.htm 2. Nginx port-based virtual host configuration If a server has only one IP or needs to access different virtual hosts through different ports, you can use port-based virtual host configuration. 2.1 Assume that 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 that need to be configured are 7081, 8081 and 9081. Configuring the host file of the host is convenient for testing. [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 Create a root directory for the virtual host to store web pages 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 and include the virtual host configuration file into 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 to the end of the nginx.conf file # Find the following content in the http section and delete the "#" in front of each line 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 shown below include vhost/*.conf 2.5 Edit the configuration file for 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 to the actual domain name. The domain name of each virtual host configuration file 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 to the actual domain name. The domain name of each virtual host configuration file 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 to the actual domain name. The domain name of each virtual host configuration file 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 a 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 Files [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 during configuration 1. Problems that occurred during the final test [root@localhost sbin]# curl http://www.test154.com:7081 curl: (7) Failed connect to www.test154.com:7081; Connection refused [root@localhost sbin]# curl 192.168.2.154:7081 curl: (7) Failed connect to 192.168.2.154:7081; Connection refused Solution: 1.1 Use the following command to check whether Nginx is listening on 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 is not listening to the corresponding port, restart the Nginx service. If it still doesn't work, restart the server The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
>>: How to install mysql database in deepin 2014 system
Table of contents 1. Auxiliary functions 2. Examp...
Table of contents Variable Scope The concept of c...
Of course, it also includes some personal experien...
NERDTree is a file system browser for Vim. With t...
Installation environment: CAT /etc/os-release Vie...
NC's full name is Netcat (Network Knife), and...
In the previous article, I introduced the detaile...
This article shares a digital clock effect implem...
Docker error 1. Check the cause docker logs nexus...
Organize the MySQL 5.5 installation and configura...
This article shares the specific code of JavaScri...
The first parameter passing method is dynamic rou...
After the form input box input is set to the disa...
Many people have read this book: "Grow as a ...
This article shares with you a practical web navi...