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

What are the ways to configure Nginx virtual host based on IP?

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

Share

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

This article is to share with you about the implementation of Nginx virtual host configuration based on IP. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

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.

1. Virtual host configuration based on IP

If you have multiple IP on the same server, you can use a virtual host configuration based on IP to bind different services to different IP.

Assuming that the server has an IP address of 192.168.2.150, first use ifconfig to bind the other three IP on the same network interface.

[root@localhost ~] # ifconfigens33: 1 192.168.2.151 ifconfigens33 24 up [root@localhost ~] # ifconfigens33: 2 192.168.2.152 ifconfigens33 24 up [root@localhost ~] # ifconfigens33: 3 192.168.2.153 ifconfigens33 24 up [root@localhost ~] # ifconfigens33: flags=4163 mtu 1500 inet 192.168.2.106 netmask 255.255.255.0 broadcast 192.168.2.255 inet6 fe80::2a8d:be6:a4a8:ea0 prefixlen 64 scopeid 0x20 ether 00:0c:29 16:90:ae txqueuelen 1000 (Ethernet) RX packets 1220 bytes 87955 (85.8 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 206bytes 23755 (23.1KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0ens33:1: flags=4163 mtu 1500 inet 192.168.151 netmask 255.255.0 broadcast 192.168.2.255 ether 00:0c:29:16:90:ae txqueuelen 1000 (Ethernet) ens33:2: flags=4163 mtu 1500 inet 192.168.2.152 netmask 255.255. 255.0 broadcast 192.168.2.255 ether 00:0c:29:16:90:ae txqueuelen 1000 (Ethernet) ens33:3: flags=4163 mtu 1500 inet 192.168.2.153 netmask 255.255.255.0 broadcast 192.168.2.255 ether 00:0c:29:16:90:ae txqueuelen 1000 (Ethernet) lo: flags=73 mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6:: 1 prefixlen 128 scopeid 0x10 loop txqueuelen 1 (Local Loopback) RX packets 72 bytes 6252 (6. 1 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 72 bytes 6252 (KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

1.23 domain names corresponding to IP are as follows. Configure the host file of the host to facilitate testing.

[root@localhost ~] # vim / etc/hosts [root@localhost ~] # cat / etc/hosts127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4::1 localhost localhost.localdomain localhost6 localhost6.localdomain6192.168.2.151 www.test151.com192.168.2.152 www.test152.com192.168.2.153 www.test153.com

You can simulate the implementation of DNS polling.

Attachment: after setting up the hosts file, you must remember to execute the following command to make it effective

1. Enter the command line with cmd under windows

C:\ Users\ 1234 > ipconfig / flushdnsWindows IP configuration successfully flushed the DNS resolution cache.

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

[root@localhost /] # mkdir-p / data/www [root@localhost /] # cd / data/www [root@localhost www] # mkdir 151 [root@localhost www] # mkdir 152 [root@localhost www] # mkdir 153 [root@localhost www] # echo "192.168.2.151" > 151/index.html [root@localhost www] # echo "192.168.2.152" > 152/index.html [root@localhost www] # echo "192.168.2.153" > 153/index.html [root@localhost www] # ls151 152 153

1.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] # 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 below}

Edit the configuration file for each IP (the profile for each virtual host)

[root@localhost conf] # mkdir-p vhost [root@localhost conf] # cd vhost/ [root@localhost vhost] # cat www.test151.conf server {listen 192.168.2.151 vhost 80; # 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.test151.com.log main; error_log / data/logs/www.test151.com.error.log Location / {root / data/www/151; index index.html index.htm;}} [root@localhost vhost] # cat www.test152.conf server {listen 192.168.2.152 data/www/151; index index.html index.htm; 80; # is configured as the 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.test152.com.log main; error_log / data/logs/www.test152.com.error.log Location / {root / data/www/152; index index.html index.htm;}} [root@localhost vhost] # cat www.test153.conf server {listen 192.168.2.153data/www/152; index index.html index.htm; 80; # is configured as the 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.test153.com.log main; error_log / data/logs/www.test153.com.error.log Location / {root / data/www/153; index index.html index.htm;}}

1.6 create a log file, otherwise you cannot start nginx

[root@localhost /] # mkdir-p / data/logs [root@localhost /] # touch / data/logs/www.test151.com.log [root@localhost /] # touch / data/logs/www.test151.com.error.log [root@localhost /] # touch / data/logs/www.test152.com.log [root@localhost /] # touch / data/logs/www.test152.com.error.log [root@localhost /] # touch / data/logs/www.test153.com.log [root @ localhost /] # touch / data/logs/www.test153.com.error.log [root@localhost /] # ls / data/logs/www.test151.com.error.log www.test152.com.error.log www.test153.com.error.logwww.test151.com.log www.test152.com.log www.test153.com.log

1.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

1.8 Test Files

[root@localhost sbin] # curl www.test151.com192.168.2.151 [root@localhost sbin] # curl www.test152.com192.168.2.152 [root@localhost sbin] # curl www.test153.com192.168.2.153

Attachment: problems in the process of configuration

1. Problems in testing configuration files

[root@localhost sbin] #. / nginx-tnginx: [emerg] unexpected "}" in / usr/local/nginx/conf/nginx.conf:122nginx: configuration file / usr/local/nginx/conf/nginx.conf test failed

Solution: the following statement forgot to add a semicolon

Include vhost/*.conf

2. Always access the same result when using curl www.test*.com test

Solution: do not write the IP address after server_name, only the domain name can be added after server_name.

Thank you for reading! This is the end of this article on "what are the ways to configure Nginx virtual hosts based on IP?". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

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