所谓虚拟主机,在Web服务里就是一个独立的网站站点,这个站点对应独立的域名(也可能是IP或端口),具有独立的程序及资源目录,可以独立地对外提供服务供用户访问。这个独立的站点在配置里是由一定格式的标签段标记的。在nginx中是使用一个server{}标签来标示一个虚拟主机。一个Web服务里可以有多个虚拟主机标签对,即可以同时支持多个虚拟主机站点。
nginx常见的虚拟主机分为以下三种:
1、基于域名的虚拟主机
所谓基于域名的虚拟主机,意思就是通过不同的域名区分不同的虚拟主机,基于域名的虚拟主机是企业应用最广的虚拟主机类型,几乎所有对外提供服务的网站使用的都是基于域名的虚拟主机,例如:www.ywx.com
server { listen 80; server_name www.ywx.com; access_log logs/ywx/ywx_access.log main; location / { root html/ywx; index index.html index.htm; } }
所谓基于端口的虚拟主机,意思就是通过不同的端口来区分不同的虚拟主机,此类虚拟主机对应的企业应用主要为公司内部的网站,例如:一些不希望直接对外提供用户访问的网站后台等,访问基于端口的虚拟主机,地址里要带有端口,例如:http://www.ywx.org
server { listen 9000; server_name www.ywx.com; access_log logs/ywx/ywx_access.log main; location / { root html/ywx; index index.html index.htm; } }
server { listen 80; server_name 192.168.31.101; access_log logs/ywx/ywx_access.log main; location / { root html/ywx; index index.html index.htm; } }
配置3个虚拟主机 1、分别使用基于域名、ip及端口的方式来配置 2、访问域名www.ywx.com 显示www.ywx.com页面 3、访问ww.ywx.com:1234 显示www.ywx.com:1234 4、访问ip地址 显示hello world
[root@inode3 ~]# cat /etc/redhat-release CentOS Linux release 7.5.1804 (Core) [root@inode3 ~]# uname -r 3.10.0-862.el7.x86_64 IP:192.168.32.103
步骤一:源码安装nginx
mkdir -p /server/tools cd /server/tools yum install -y gcc gcc-c++ make pcre pcre-devel openssl openssl-devel zlib zlib-devel useradd -r www wget -c http://nginx.org/download/nginx-1.16.0.tar.gz tar -xf nginx-1.16.0.tar.gz cd nginx-1.16.0 ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_stub-status_module make && make install ln -s /usr/local/nginx/sbin/nginx /usr/sbin/nginx chown -R www.www /usr/local/nginx
步骤二:配置nginx的主配置文件nginx.conf
cat > /usr/local/nginx/conf/nginx.conf << EOF worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; log_format main ‘$remote_addr - $remote_user [$time_local] "$request" ‘ ‘$status $body_bytes_sent "$http_referer" ‘ ‘"$http_user_agent" "$http_x_forwarded_for"‘; access_log logs/access.log main; sendfile on; keepalive_timeout 65; include vhost/*.conf; } EOF mkdir -p /usr/local/nginx/conf/vhost
步骤三:配置虚拟主机
1、基于域名的虚拟主机
vim /usr/local/nginx/conf/vhost/ywx.conf server { listen 80; server_name www.ywx.com; access_log logs/ywx_access.log main; location / { root html/ywx; index index.html index.htm; } }
创建基于域名的虚拟主机的页面发布目录
mkdir -p /usr/local/nginx/html/ywx
创建基于域名的虚拟主机的访问页面
echo "www.ywx.com" > /usr/local/nginx/html/ywx/index.html
2、基于ip的虚拟主机
vim /usr/local/nginx/conf/vhost/ip.conf server { listen 80; server_name 192.168.32.101; access_log logs/ip_access.log main; location / { root html/ip; index index.html index.htm; } }
创建基于ip的虚拟主机的页面发布目录
mkdir -p /usr/local/nginx/html/ip
创建基于域名的虚拟主机的访问页面
echo "hello world" > /usr/local/nginx/html/ip/index.html
3、基于端口的虚拟主机
vim /usr/local/nginx/conf/vhost/port.conf server { listen 1234; server_name www.ywx.com; access_log logs/port_access.log main; location / { root html/port; index index.html index.htm; } }
创建基于端口的虚拟主机的页面发布目录
mkdir -p /usr/local/nginx/html/port
创建基于端口的虚拟主机的访问页面
echo "www.ywx.com:1234" > /usr/local/nginx/html/port/index.html
步骤四:把nginx目录的权限该为www
chown -R www.www /usr/local/nginx
步骤五:启动nginx
nginx -t
nginx
步骤六:访问测试页面
基于域名的访问 [root@inode3 vhost]# curl www.ywx.com www.ywx.com 基于端口的访问 [root@inode3 vhost]# curl www.ywx.com:1234 www.ywx.com:1234 基于ip的访问 [root@inode3 vhost]# curl 192.168.32.103 hello world
例如:
访问www.ywx.com 显示访问页面www.ywx.com
再为其增加一个别名 www.king.com 访问www.king.com与访问www.ywx.com结果一样
vim /usr/local/nginx/conf/vhost/ywx.conf server { listen 80; server_name www.ywx.com www.king.com; access_log logs/ywx_access.log main; location / { root html/ywx; index index.html index.htm; } }
测试
[root@inode3 vhost]# 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 [root@inode3 vhost]# nginx -s reload [root@inode3 vhost]# curl www.ywx.com www.ywx.com [root@inode3 vhost]# curl www.king.com www.ywx.com
原文:https://www.cnblogs.com/yaokaka/p/13632343.html