Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Explain in detail three ways of Nginx virtual host configuration (port-based)

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

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.

Explain in detail three ways of Nginx virtual host configuration (based on IP) https://www.jb51.net/article/14974.htm

Explain in detail the three ways of Nginx virtual host configuration (based on domain name) 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, a port-based virtual host configuration can be used.

2.1 assume that the server has an IP address of 192.168.2.154

[root@localhost conf] # ifconfigens33:4 192.168.2.154 Ethernet 24 up [root@localhost conf] # ifconfigens33:4: flags=4163 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 respectively, and the host file of the configured host is easy to test.

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

2.3 establish the root directory where the virtual host stores the web page, and create the first page 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] # ls7081 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 master file

[root@localhost /] # cd / usr/local/nginx/conf/ [root@localhost conf] # lsfastcgi.conf fastcgi_params koi-utf mime.types nginx.conf scgi_params uwsgi_params win-utffastcgi.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 the nginx.conf file

# find the following in the http section and delete "#" log_format main'$remote_addr-$remote_user [$time_local] "$request"'$status $body_bytes_sent "$http_referer"''"$http_user_agent"$http_x_forwarded_for" before each line; # add the following statement before the last "}" at the end of the configuration file, as shown in the following 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.154root@localhost vhost 7081; # is configured as an actual domain name, and the configuration file domain name 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.154vim www.test154.8081.conf 8081; # configured as the actual domain name, the configuration file domain name 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.154data/www/port/8081; index index.html index.htm; 9081; # configured as the actual domain name, the configuration file domain name 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 a log file, otherwise you cannot start nginx

[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.logwww.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-tnginx: the configuration file / usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file / usr/local/nginx/conf/nginx.conf test is successful# launch nginx [root @ localhost sbin] #. / nginx

2.8 Test file

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

Attached: problems in the process of configuration

1. Problems that occurred during the final test

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

Solution:

1.1 use the following command to see if Nginx is listening on the appropriate port

[root@localhost conf] # netstat-lntActive Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address Statetcp 0 0 0.0.0 Proto Recv-Q Send-Q Local Address Foreign Address Statetcp 111 0.0.0 only servers * LISTENtcp 0 192.168.2.153 only servers 80 0.0.0 0. 0 LISTENtcp 0 192.168.2 152 80 0.0.0 0 LISTENtcp 0 0192.168.2.151 8 0 0.0 * LISTENtcp 000. 0.0.0LISTENtcp 8080 0.0.0.0LISTENtcp 0192.168.2.154LISTENtcp 0192.168.0.0.0.1LISTENtcp 0192.168.0.0.0.1 LISTENtcp 0192.168.2.154Rd 700.0 LISTENtcp 0 192.168.2.154 .0.0: * LISTENtcp6 00: 111: * LISTENtcp6 00: 22:: * LISTENtcp6 00: 23: * LISTENtcp6 00:: 1:25:: * LISTEN

1.2 restart the Nginx service if the Nginx is not listening on the corresponding port, and no longer restart the server

The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report