Nginx 基于 ip 的虚拟主机配置 { #serverb (1)/etc/nginx/conf.d/* [[email protected] conf.d]# vim ip.conf server { listen 192.168.1.88:80; root 88.com; index index.html; } server { listen 192.168.1.87:80; root 87.com; index index.html; } [[email protected] ~]# ifconfig eth1:0 192.168.1.88 [[email protected] ~]# ifconfig eth1:1 192.168.1.87 [[email protected] ~]# mkdir /usr/local/nginx/88.com [[email protected] ~]# echo ‘this is 88.com‘ > /usr/local/nginx/88.com/index.html [[email protected] ~]# mkdir /usr/local/nginx/87.com [[email protected] ~]# echo ‘this is 87.com‘ > /usr/local/nginx/87.com/index.html } Nginx 基于端口的虚拟主机配置 { server { listen 80; root 80.com; } server { listen 8080; root 8080.com; } [[email protected] ~]# mkdir /usr/local/nginx/80.com [[email protected] ~]# mkdir /usr/local/nginx/8080.com [[email protected] ~]# echo ‘this is 80.com‘ > /usr/local/nginx/80.com/index.html [[email protected] ~]# echo ‘this is 8080.com‘ > /usr/local/nginx/8080.com/index.html } 基于名称的虚拟主机配置详解 { 1. www.dabao.com www.booboo.com [[email protected] conf.d]# pwd /etc/nginx/conf.d [[email protected] conf.d]# cp default.conf www.booboo.com.conf [[email protected] conf.d]# cp default.conf www.dabao.com.conf [[email protected] conf.d]# vim www.booboo.com.conf server { listen 80; server_name www.booboo.com; location / { root /usr/share/nginx/booboo.com; index index.html index.htm; } } [[email protected] conf.d]# vim www.dabao.com.conf server { listen 80; server_name www.dabao.com; root /usr/share/nginx/dabao.com; index index.html index.htm; } [[email protected] conf.d]# mkdir /usr/share/nginx/dabao.com [[email protected] conf.d]# mkdir /usr/share/nginx/booboo.com [[email protected] conf.d]# echo hi,booboo > /usr/share/nginx/booboo.com/index.html [[email protected] conf.d]# echo hi,dabao> /usr/share/nginx/dabao.com/index.html #重启 nginx 服务 [[email protected] conf.d]# systemctl restart nginx.service workstation 客户端测试 [[email protected] ~]# echo 172.25.15.10 www.dabao.com >> /etc/hosts [[email protected] ~]# echo 172.25.15.10 www.booboo.com >> /etc/hosts 2.server_name mail.dabao.com news.dabao.com www.news.dabao.com www.sports.dabao.com [[email protected] conf.d]# vim www.booboo.com.conf server { listen 80; server_name ~www\..*\.booboo\.com; location / { root /usr/share/nginx/booboo.com; index index.html index.htm; } } [[email protected] conf.d]# vim www.dabao.com.conf server { listen 80; server_name *.dabao.com; root /usr/share/nginx/dabao.com; index index.html index.htm; } 3.location www.booboo.com/news.html [[email protected] conf.d]# cat www.booboo.com.conf server { listen 80; server_name ~www.*\.booboo\.com; location / { root /usr/share/nginx/booboo.com; index index.html index.htm; } location ~/news.html{ root /usr/share/nginx/dabao.com; index index.html index.htm; } } 4.80 default [[email protected] conf.d]# cp default.conf err.com.conf [[email protected] conf.d]# vim err.com.conf server { listen 80 default; #在监听端口号后边添加 default 字段,即代表该虚拟主机为默认虚拟主机 location / { root /usr/share/nginx/err.com; index index.html index.htm; } #创建该虚拟主机网页文件存放目录和首页文件,一般首页文件中即为报错信息 [[email protected] conf.d]# mkdir /usr/share/nginx/err.com [[email protected] conf.d]# echo cannot found this virtual host name > /usr/share/nginx/err.com/index.html #重启 nginx 服务 [[email protected] conf.d]# systemctl restart nginx.service (4)workstation 客户端测试 }
时间: 2024-08-03 11:21:41