详解Nginx 虚拟主机配置的三种方式(基于端口)
Nginx配置虚拟主机支持3种方式:基于IP的虚拟主机配置,基于端口的虚拟主机配置,基于域名的虚拟主机配置。
详解Nginx虚拟主机配置的三种方式(基于IP)https://www.nhooo.com/article/14974.htm
详解Nginx虚拟主机配置的三种方式(基于域名)https://www.nhooo.com/article/14978.htm
2、Nginx基于端口的虚拟主机配置
如一台服务器只有一个IP或需要通过不同的端口访问不同的虚拟主机,可以使用基于端口的虚拟主机配置。
2.1假设服务器有个IP地址为192.168.2.154
[root@localhostconf]#ifconfigens33:4192.168.2.154/24up [root@localhostconf]#ifconfig ens33:4:flags=4163mtu1500 inet192.168.2.154netmask255.255.255.0broadcast192.168.2.255 ether00:0c:29:16:90:aetxqueuelen1000(Ethernet)
2.2需要配置的虚拟主机分别为7081、8081和9081,配置主机的host文件便于测试。
[root@localhostconf]#vim/etc/hosts [root@localhostconf]#cat/etc/hosts|grep192.168.2.154 192.168.2.154www.test154.com
2.3建立虚拟主机存放网页的根目录,并创建首页文件index.html
[root@localhostconf]#cd/data/www/ [root@localhostwww]#mkdirport [root@localhostwww]#cdport/ [root@localhostport]#mkdir708180819081 [root@localhostport]#ls 708180819081 [root@localhostport]#echo"port7081">7081/index.html [root@localhostport]#echo"port8081">8081/index.html [root@localhostport]#echo"port9081">9081/index.html
2.4修改nginx.conf,将虚拟主机配置文件包含进主文件
[root@localhost/]#cd/usr/local/nginx/conf/ [root@localhostconf]#ls fastcgi.conffastcgi_paramskoi-utfmime.typesnginx.confscgi_paramsuwsgi_paramswin-utf fastcgi.conf.defaultfastcgi_params.defaultkoi-winmime.types.defaultnginx.conf.defaultscgi_params.defaultuwsgi_params.default [root@localhostconf]#vimnginx.conf
在nginx.conf文件末尾加入以下配置
#在http段中找到以下内容并删除每行前面的“#” log_formatmain'$remote_addr-$remote_user[$time_local]"$request"' '$status$body_bytes_sent"$http_referer"' '"$http_user_agent""$http_x_forwarded_for"'; #配置文件结尾的最后一个“}”之前加入以下语句,如下所示 includevhost/*.conf
2.5编辑每个端口的配置文件
[root@localhostvhost]#vimwww.test154.7081.conf [root@localhostvhost]#catwww.test154.7081.conf server{ listen192.168.2.154:7081; #配置成实际的域名,每个虚拟主机的配置文件域名都相同 #server_namewww.test.com; access_log/data/logs/www.test154.7081.logmain; error_log/data/logs/www.test154.7081.error.log; location/{ root/data/www/port/7081; indexindex.htmlindex.htm; } } [root@localhostvhost]#vimwww.test154.8081.conf [root@localhostvhost]#catwww.test154.8081.conf server{ listen192.168.2.154:8081; #配置成实际的域名,每个虚拟主机的配置文件域名都相同 #server_namewww.test.com; access_log/data/logs/www.test154.8081.logmain; error_log/data/logs/www.test154.8081.error.log; location/{ root/data/www/port/8081; indexindex.htmlindex.htm; } } [root@localhostvhost]#vimwww.test154.9081.conf [root@localhostvhost]#catwww.test154.9081.conf server{ listen192.168.2.154:9081; #配置成实际的域名,每个虚拟主机的配置文件域名都相同 #server_namewww.test.com; access_log/data/logs/www.test154.9081.logmain; error_log/data/logs/www.test154.9081.error.log; location/{ root/data/www/port/9081; indexindex.htmlindex.htm; } }
2.6创建日志文件,否则无法启动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.logwww.test154.8081.error.logwww.test154.9081.error.log www.test154.7081.logwww.test154.8081.logwww.test154.9081.log
2.7先测试配置文件然后再启动nginx
[root@localhost/]#cd/usr/local/nginx/sbin/ [root@localhostsbin]#./nginx-t nginx:theconfigurationfile/usr/local/nginx/conf/nginx.confsyntaxisok nginx:configurationfile/usr/local/nginx/conf/nginx.conftestissuccessful #启动nginx [root@localhostsbin]#./nginx
2.8测试文件
[root@localhost~]#curlhttp://www.test154.com:7081 port7081 [root@localhost~]#curlhttp://www.test154.com:8081 port8081 [root@localhost~]#curlhttp://www.test154.com:9081 port9081
附:配置过程中的问题
1、最后测试时发生的问题
[root@localhostsbin]#curlhttp://www.test154.com:7081 curl:(7)Failedconnecttowww.test154.com:7081;拒绝连接 [root@localhostsbin]#curl192.168.2.154:7081 curl:(7)Failedconnectto192.168.2.154:7081;拒绝连接
解决方法:
1.1使用以下命令查看Nginx是否在监听相应的端口
[root@localhostconf]#netstat-lnt ActiveInternetconnections(onlyservers) ProtoRecv-QSend-QLocalAddressForeignAddressState tcp000.0.0.0:1110.0.0.0:*LISTEN tcp00192.168.2.153:800.0.0.0:*LISTEN tcp00192.168.2.152:800.0.0.0:*LISTEN tcp00192.168.2.151:800.0.0.0:*LISTEN tcp000.0.0.0:80800.0.0.0:*LISTEN tcp00192.168.2.154:80810.0.0.0:*LISTEN tcp000.0.0.0:220.0.0.0:*LISTEN tcp00192.168.2.154:90810.0.0.0:*LISTEN tcp00127.0.0.1:250.0.0.0:*LISTEN tcp00192.168.2.154:70810.0.0.0:*LISTEN tcp600:::111:::*LISTEN tcp600:::22:::*LISTEN tcp600:::23:::*LISTEN tcp600::1:25:::*LISTEN
1.2若Nginx未监听相应端口则重启Nginx服务,再不行重启服务器
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。