1、配置通过域名区分的虚拟机
cd /etc/nginx
mv nginx.conf{,.bak} 做个备份
vim nginx.conf
配置文件内容
user nginx;
worker_processes 4;
#error_log logs/error.log;
worker_rlimit_nofile 102400;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
server {
listen 80;
server_name web.testpm.com;
location / {
root /var/www/nginx/;
index index.html index.htm;
limit_rate 2k;
}
}
server {
listen 80;
server_name web.1000phone.com;
location / {
root /1000phone/html;
index index.html index.htm;
}
}
}
不用出去,直接在下面输入:
:! mkdir -p /var/www/nginx && echo "testpm" > /var/www/nginx/index.html
:! mkdir -p /1000phone/html && echo "1000phone" > /1000phone/html/index.html
然后可以看到
cat /1000phone/html/index.html
1000phone
cat /var/www/nginx/index.html
testpm
重新加载文件
/usr/local/nginx/sbin/nginx -s reload
配置域名
vim /etc/hosts
加入 192.168.127.169 web.testpm.com
192.168.127.169 web.1000phone.com
ping web.testpm.com
ping web.1000phone.com
ping通就可以
curl http://web.testpm.com
curl http://web.1000phone.com
(返回welcome to nginx !)
==============================================================================
2、 基于ip的虚拟主机
ip a
会显示你的ens33网卡
我的ip地址是192.168.127.169
然后我们重新设置两个虚拟主机
ifconfig ens33:1 192.168.127.167/24
ifconfig ens33:2 192.168.127.168/24
ifconfig 这时就有三个地址
重新加载配件文件
/usr/local/nginx/sbin/nginx -s reload
测试访问
curl http://192.168.127.167
curl http://192.168.127.168
删除创建的ip
ifconfig ens33:1 192.168.127.167/24 down
ifconfig ens33:2 192.168.127.168/24 down
然后重启一下nginx
/usr/local/nginx/sbin/nginx
==============================================================================
3、 基于端口的虚拟主机
备不备份随便你们
cat /etc/nginx/nginx.conf
=======================
user nginx;
worker_processes 4;
worker_rlimit_nofile 102400;
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"';
sendfile on;
keepalive_timeout 65;
server {
listen 192.168.127.167:80;
server_name web.testpm.com;
location / {
root /var/www/nginx/;
index index.html index.htm;
limit_rate 2k;
}
}
server {
listen 192.168.127.168:8080;
server_name web.1000phone.com;
location / {
root /1000phone/html/;
index index.html index.htm;
}
}
}
===========================
重新加载文件
/usr/local/nginx/sbin/nginx -s reload
测试访问
curl http://web.testpm.com/
curl http://web.1000phone.com:8080