Detailed explanation of three ways to configure Nginx virtual hosts (based on ports)

Detailed explanation of three ways to configure Nginx virtual hosts (based on ports)

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:
  • Detailed explanation of three ways to configure Nginx virtual hosts (based on IP)
  • Detailed explanation of nginx virtual host configuration example
  • Three ways to configure Nginx virtual hosts (based on domain names)

<<:  Problems and solutions encountered when using v-model to two-way bind the values ​​of parent-child components in Vue

>>:  How to install mysql database in deepin 2014 system

Recommend

Solution to 1045 error when navicat connects to mysql

When connecting to the local database, navicat fo...

HTML embed tag usage and attributes detailed explanation

1. Basic grammar Copy code The code is as follows...

MySQL installation diagram summary

MySQL 5.5 installation and configuration method g...

How to display a small icon in front of the browser URL

When you browse many websites, you will find that ...

How to detect file system integrity based on AIDE in Linux

1. AIDE AIDE (Advanced Intrusion Detection Enviro...

VUE implements timeline playback component

This article example shares the specific code of ...

Web Design Experience: 5 Excellent Web Design Concepts Full Analysis (Pictures)

Unlike other types of design, web design has been ...

Where is the project location deployed by IntelliJ IDEA using Tomcat?

After IntelliJ IDEA deploys a Javaweb project usi...

Markup Language - Phrase Elements

Click here to return to the 123WORDPRESS.COM HTML ...

How to set up ssh password-free login to Linux server

Every time you log in to the test server, you alw...

Vue ElementUI Form form validation

Form validation is one of the most commonly used ...

Website front-end performance optimization: JavaScript and CSS

I have read an article written by the Yahoo team ...