nginx之 nginx虚拟机配置

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

什么是虚拟主机:
虚拟主机是一种特殊的软硬件技术,它可以将网络上的每一台计算机分成多个虚拟主机,每个虚拟主机可以独立对外提供www服务,这样就可以实现一台主机对外提供多个web服务,每个虚拟主机之间是独立的,互不影响。

nginx可以实现虚拟主机的配置,nginx支持三种类型的虚拟主机配置。
1、基于域名的虚拟主机 (server_name来区分虚拟主机——应用:外部网站)
2、基于ip的虚拟主机, (一块主机绑定多个ip地址)
3、基于端口的虚拟主机 (端口来区分虚拟主机——应用:公司内部网站,外部网站的管理后台)

范例:

一、 基于域名的虚拟主机

1、配置通过域名区分的虚拟机
[root@mysql03 nginx]# cat conf/nginx.conf
worker_processes 1;

events {
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;

server {
listen 80;
server_name www.nginx01.com;
location / {
root html;
index index.html index.htm;
}
}

server {
listen 80;
server_name www.nginx02.com;
location / {
root /root/html;
index index.html index.htm;
}
}
}

2、 为 域名为 www.nginx02.com 的虚拟机,创建 index 文件    
[root@mysql03 ~]# mkdir -p /root/html
[root@mysql03 ~]# cd /root/html/
[root@mysql03 html]# vi index.html
[root@mysql03 html]# cat index.html 
<html>
<p>
this is my nginx
</p>
</html>

3、重新加载配置文件
[root@mysql03 nginx]# ./sbin/nginx -s reload

4、客户端配置路由映射
在 C:\Windows\System32\drivers\etc\hosts 文件中添加两行

10.219.24.26 www.nginx01.com
10.219.24.26 www.nginx02.com
如图:

5、 测试访问

浏览器输入:http://www.nginx01.com/

浏览器输入:http://www.nginx02.com/

 >成功!

 补充:如果配置不能正常访问, 试参考 http://blog.csdn.NET/zhang123456456/article/details/73252148

二、 基于ip的虚拟主机

1. 一块网卡绑定多个ip
[root@mysql03 nginx]# ifconfig eth0:1 10.219.24.27 
[root@mysql03 nginx]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:79:F4:02 
inet addr:10.219.24.26 Bcast:10.255.255.255 Mask:255.0.0.0
...
eth0:1 Link encap:Ethernet HWaddr 00:0C:29:79:F4:02 
inet addr:10.219.24.27 Bcast:10.255.255.255 Mask:255.0.0.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
2. 配置通过ip区分的虚拟机 
[root@mysql03 nginx]# cat conf/nginx.conf
user root root; #说明:这里的user根据 自己的nginx.conf文件所在的目录的属主属性而定 
worker_processes 1;

events {
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;

server {
listen 10.219.24.26:80;
server_name www.nginx01.com;
location / {
root html;
index index.html index.htm;
}
}

server {
listen 10.219.24.27:80;
server_name www.nginx01.com;
location / {
root /root/html;
index index.html index.htm;
}
}
}
3. reopen nginx
[root@mysql03 nginx]# ./sbin/nginx -s reopen

补充:
-- 删除绑定的vip
ifconfig eth0:1 10.219.24.27 down

三、 基于端口的虚拟主机

配置通过端口区分的虚拟机
[root@mysql03 nginx]# cat conf/nginx.conf
user root root; #说明:这里的user根据 自己的nginx.conf文件所在的目录的属主属性而定 
worker_processes 1;

events {
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;

server {
listen 80;
server_name www.nginx01.com;
location / {
root html;
index index.html index.htm;
}
}

server {
listen 8080;
server_name www.nginx01.com;
location / {
root /root/html;
index index.html index.htm;
}
}
}

 


本文转自 张冲andy 博客园博客,原文链接: http://www.cnblogs.com/andy6/p/7010914.html  ,如需转载请自行联系原作者

相关文章
|
22天前
|
运维 前端开发 应用服务中间件
LNMP详解(八)——Nginx动静分离实战配置
LNMP详解(八)——Nginx动静分离实战配置
24 0
|
1月前
|
应用服务中间件 nginx
Nginx中如何配置中文域名?
Nginx中如何配置中文域名?
40 0
|
21天前
|
前端开发 应用服务中间件 nginx
Nginx配置详解Docker部署Nginx使用Nginx部署vue前端项目
Nginx配置详解Docker部署Nginx使用Nginx部署vue前端项目
83 0
|
1月前
|
PHP
百度虚拟机 bcloud_nginx_user.conf配置
百度虚拟机 bcloud_nginx_user.conf配置
22 0
|
1天前
|
安全 应用服务中间件 网络安全
SSL原理、生成SSL密钥对、Nginx配置SSL
现在,你的Nginx虚拟主机应该已经配置了SSL,可以通过HTTPS安全访问。确保在生产环境中使用有效的SSL证书来保护通信的安全性。
7 0
|
4天前
|
域名解析 缓存 负载均衡
Nginx正向代理域名的配置
Nginx正向代理域名的配置
|
4天前
|
前端开发 JavaScript 应用服务中间件
修改Jeecg-boot context-path(附加图片+Nginx配置)
修改Jeecg-boot context-path(附加图片+Nginx配置)
12 0
|
15天前
|
应用服务中间件 nginx
nginx进行反向代理的配置
在Nginx中设置反向代理的步骤:编辑`/etc/nginx/nginx.conf`,在http段加入配置,创建一个监听80端口、服务器名为example.com的虚拟主机。通过`location /`将请求代理到本地3000端口,并设置代理头。保存配置后,使用`sudo nginx -s reload`重载服务。完成配置,通过example.com访问代理服务器。
22 0
|
16天前
|
应用服务中间件 网络安全 nginx
nginx配置https访问
nginx配置https访问
26 0
|
25天前
|
应用服务中间件 nginx
nginx配置访问qicaitun.com强制跳转www.qicaitun.com
nginx配置访问qicaitun.com强制跳转www.qicaitun.com
9 0