基于 LNMP 的 Nginx 百万并发之路 (三)基于域名的虚拟主机
Nginx 虚拟主机分为基于网卡或者 ip、或基于端口和基于域名三大类。其中 基于网卡或ip和基于端口 私人应用场合较多,多用于测试,基于域名则是实际应用场合中的主要形式,一般一台服务器维持一个域名。如果网站访问量小,一台服务器可以部署数个网站。如何搭建 LNMP 平台可参考 Wiki PHP 环境安装:Linux 开发环境 。
建议在 Nginx 安装目录下创建 conf.d
目录,以便于基于不同目的的虚拟主机配置文件的导入。
cache_temp
client_body_temp
conf
conf.d
core
fastcgi_temp
html
logs
pid
proxy_temp
sbin
scgi_temp
uwsgi_temp
本机的 Nginx 版本为
nginx version: nginx/1.18.0
修改 nginx.conf
文件,在 http
段添加
include /opt/nginx/conf.d/*.conf;
进入 conf.d
目录,编辑 domain_based.conf
文件
server {
listen 80;
server_name test.nginx1.my;
location / {
root /home/html/nginx1;
index nginx1.html;
}
}
server {
listen 80;
server_name test.nginx2.my;
location / {
root /home/html/nginx2;
index nginx2.html;
}
}
响应的创建相应目录和文件,如下
mkdir -p /home/html/nginx1 /home/html/nginx2
echo "<h1> test.nginx1.my<h1>" > /home/html/nginx1/nginx1.html
echo "<h1> test.nginx2.my<h1>" > /home/html/nginx2/nginx2.html
开启 Nginx,关闭防火墙。
nginx # vim /etc/profile 设置 export PATH="/opt/nginx/sbin:$PATH"
systemctl stop firewalld
也可以将 80 端口永久放行
firewall-cmd --zone=public --add-port=80/tcp --permanent
临时性关闭 Selinux,再次开机后无效。
setenforce 0
永久性关闭 Selinux,但存在系统安全风险。
vim /etc/selinux/config
SELINUX=disabled
不要忘了在你的物理机添加主机名,windows 环境下 hosts
文件所在路径为
C:\Windows\System32\drivers\etc
访问 test.nginx1.my
访问 test.nginx2.my
本作品采用《CC 协议》,转载必须注明作者和本文链接