为什么会有虚拟主机呢?平时我们用一台服务队外提供服务,但是呢因为用户数及其他原因,导致呢这台服务的cpu、内存、磁盘的使用率不高,但是呢又希望,一台服务器可以提供多个站点,那就需要用到虚拟主机【即多个域名对应一个ip服务器】,如何实现呢?
在http块儿里可以写多个server 但是注意要是 端口号 + 主机名 保证唯一性:
第一种方式配置【同主机名 + 不同端口号】
nginx指定配置文件启动:sudo ./sbin/nginx -c ./conf/nginx_test.conf 【这里的路径都是相对路径是因为进入了nginx目录里】
#user nobody;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
# 主站虚拟主机
server {
listen 80;
# 域名 或 主机名
server_name localhost;
location / {
root /home/gs/guos/www/www;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
# 视频虚拟主机【vhost】
server {
listen 7007;
server_name localhost;
location / {
root /home/gs/guos/www/vod;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
命令如下:

第二种方式配置 利用我们在阿里云上申请注册的域名来实现【前提是做好解析并添加记录】【不同域名 + 同一个端口号】
#user nobody;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
# 主站虚拟主机
server {
listen 80;
# 域名 或 主机名
server_name www.jngoodnews.com;
location / {
root /home/gs/guos/www/www;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
# 视频虚拟主机【vhost】
server {
listen 80;
server_name vod.jngoodnews.com;
location / {
root /home/gs/guos/www/vod;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
重新加载并检查命令如下:

效果如下:
原创文章,作者:254126420,如若转载,请注明出处:https://blog.ytso.com/tech/aiops/272284.html