1. 首页
  2. >
  3. 服务器技术
  4. >
  5. Nginx

Nginx总结(三)基于端口的虚拟主机配置

应用场景

nginx对外提供81和82两个端口监听服务。

请求81端口则请求html81目录下的html

请求82端口则请求html82目录下的html

准备环境

1. 创建192.168.78.132虚拟机,保证本地电脑和虚拟网络通畅。

2. 在192.168.78.132上安装nginx。

html目录创建

将原来nginx的html目录拷贝两个目录 html81和html82,为了方便测试需要修改每个目录下的index.html内容使之个性化。

配置虚拟主机

修改/usr/local/nginx/conf/nginx.conf文件,添加两个虚拟主机,如下:vi /usr/local/nginx/conf/nginx.conf

#user  nobody;
worker_processes 1;


events {
worker_connections 1024;
}


http {

include mime.types;

default_type application/octet-stream;

sendfile on;

keepalive_timeout 65;

#配置虚拟主机
server {
#监听的ip和端口,配置81
listen 81;
#虚拟主机名称这里配置ip地址
server_name 192.168.78.132;
#所有的请求都以/开始,所有的请求都可以匹配此location
location / {
#使用root指令指定虚拟主机目录即网页存放目录
#比如访问http://ip/test.html将找到/usr/local/html3/test.html
#比如访问http://ip/item/test.html将找到/usr/local/html3/item/test.html

root /usr/local/nginx/html80;
#指定欢迎页面,按从左到右顺序查找
index index.html index.htm;

}
}

#配置虚拟主机

server {
listen 82;
server_name 192.168.78.132;

location / {

root /usr/local/nginx/html8080;

index index.html index.htm;

}

}

}

测试

重新加载配置nginx配置文件,查看端口监听状态:

访问http://192.168.78.132:81

Nginx总结(三)基于端口的虚拟主机配置

访问http://192.168.78.132:82

Nginx总结(三)基于端口的虚拟主机配置

最后

以上,就把nginx 基于ip的配置虚拟主机讲完了。后面会继续讲基于域名配置虚拟主机。