In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "what are the ways to configure Nginx virtual hosts", the content is simple and easy to understand, and the organization is clear. I hope it can help you solve your doubts. Let Xiaobian lead you to study and learn "what are the ways to configure Nginx virtual hosts".
Nginx supports three ways to configure virtual hosts: IP-based virtual host configuration, port-based virtual host configuration, and domain-based virtual host configuration.
Nginx Domain Name Based Virtual Hosting Configuration
Domain name based virtual hosting is a popular way to configure multiple domain names on the same IP and all accessed via port 80.
3.1 Suppose the server has an IP address of 192.168.2.155
[root@localhost ~]# ifconfig ens33:5 192.168.2.155/24 up[root@localhost ~]# ifconfigens33:5: flags=4163 mtu 1500 inet 192.168.2.155 netmask 255.255.255.0 broadcast 192.168.2.255 ether 00:0c:29:16:90:ae txqueuelen 1000 (Ethernet)
3.2 The domain name corresponding to 192.168.2.155 is as follows. Configure the host file of the host to facilitate testing.
[root@localhost ~]# vim /etc/hosts[root@localhost ~]# cat /etc/hosts|grep 192.168.2.155192.168.2.155 www.oa.com192.168.2.155 www.bbs.com192.168.2.155 www.test.com
3.3 Create the root directory of the virtual host to store the web page, and create the home page file index.html
[root@localhost ~]# cd /data/www/[root@localhost www]# mkdir www.oa.com[root@localhost www]# mkdir www.bbs.com[root@localhost www]# mkdir www.test.com[root@localhost www]# echo www.oa.com > www.oa.com/index.html[root@localhost www]# echo www.bbs.com > www.bbs.com/index.html[root@localhost www]# echo www.test.com > www.test.com/index.html
3.4 Modify nginx.conf to include virtual host configuration files 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 to the end of the nginx.conf file
#Find the following in the http section and delete the "#" log_format main '$remote_addr - $remote_user [$time_local] "$request" ' before each line '$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
3.5 Edit the profile for each domain name (profile for each virtual host)
[root@localhost conf]# cd vhost/[root@localhost vhost]# cat www.oa.com.conf server { listen 192.168.2.155:80; server_name www.oa.com; access_log /data/logs/www.oa.com.log main; error_log /data/logs/www.oa.com.error.log; location / { root /data/www/www.oa.com; index index.html index.htm; } }[root@localhost vhost]# cat www.bbs.com.conf server { listen 192.168.2.155:80; server_name www.bbs.com; access_log /data/logs/www.bbs.com.log main; error_log /data/logs/www.bbs.com.error.log; location / { root /data/www/www.bbs.com; index index.html index.htm; } }[root@localhost vhost]# cat www.test.com.conf server { listen 192.168.2.155:80; server_name www.test.com; access_log /data/logs/www.test.com.log main; error_log /data/logs/www.test.com.error.log; location / { root /data/www/www.test.com; index index.html index.htm; } }[root@localhost vhost]# cat /data/www/www.oa.com/index.htmlwww.oa.com[root@localhost vhost]# cat /data/www/www.bbs.com/index.htmlwww.bbs.com[root@localhost vhost]# cat /data/www/www.test.com/index.htmlwww.test.com
3.6 Create a log file, otherwise nginx cannot be started
[root@localhost /]# mkdir -p /data/logs[root@localhost /]# touch /data/logs/www.oa.com.log[root@localhost /]# touch /data/logs/www.oa.com.error.log[root@localhost /]# touch /data/logs/www.bbs.com.log[root@localhost /]# touch /data/logs/www.bbs.com.error.log[root@localhost /]# touch /data/logs/www.test.com.log[root@localhost /]# touch /data/logs/www.test.com.error.log[root@localhost /]# ls /data/logs/www.oa.com.error.log www.bbs.com.error.log www.test.com.error.logwww.oa.com.log www.bbs.com.log www.test.com.log
3.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#Initiate nginx[root@localhost sbin]# ./ nginx
3.8 test file
[root@localhost vhost]# curl http://www.oa.comwww.oa.com[root@localhost vhost]# curl http://www.bbs.comwww.bbs.com[root@localhost vhost]# curl http://www.test.comwww.test.com
Attachment: Problems in the configuration process
1. Problems that occurred during the final test
[root@localhost ~]# curl http://www.oa.comcurl: (7) Failed connect to www.oa.com:80; Denied connection
Solution:
Check to see if Nginx is listening on the appropriate port.
[root@localhost ~]# netstat -lntActive Internet connections (only servers)Proto Recv-Q Send-Q Local Address Foreign Address Statetcp 0 0 0.0.0.0:111 0.0.0.0:* LISTENtcp 0 0 192.168.2.155:80 0.0.0.0:* LISTENtcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTENtcp 0 0 0.0.0.0:22 0.0.0.0:* LISTENtcp 0 0 127.0.0.1:25 0.0.0.0:* LISTENtcp6 0 0 :::111 :::* LISTENtcp6 0 0 :::22 :::* LISTENtcp6 0 0 :::23 :::* LISTENtcp6 0 0 ::1:25 :::* LISTEN
1. When configuring the virtual host file, add the IP address of the monitor, and each virtual host configuration file is the same.
listen 192.168.2.155:80;
2. Restart the server after the configuration is completed
The above is "What are the ways to configure Nginx virtual hosting" All the contents of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.