nginx虚拟主机配置小结
2016-05-28 16:30
525 查看
nginx的安装在lnmp环境搭建中已经介绍过了,配置文件在安装目录下的conf子目录下,主要主要分成四部分:main(全局设置)、server(主机设置)、upstream(负载均衡服务器设置)、location(URL 匹配特定位置的设置)。可以狭义的理解一个server就是一个虚拟主机。主要有三种配置虚拟主机的方式:基于域名、基于端口、基于ip。下面分别介绍:
一、基于域名:
主要步骤:1.首先在windows本地hosts添加虚拟机ip地址对应的域名解析
192.168.1.104 z.com
2.对应域名网站目录下新增index.html文件
3.修改nginx.conf配置文件 添加如下信息保存退出
server {
listen 80;
server_name z.com;#
location /{
root z.com;#相对路径 nginx的安装目录
index index.html;
}
}
4.检测配置信息#/app/local/nginx/sbin/nginx -t成功的话提示successful
5.重启nginx #/app/local/nginx/sbin/nginx -s reload
6.通过curl命令或者浏览器输入域名访问
# curl -xlocalhost:80 z.com
<html>
This is z.com
</html>
二、基于端口: 使用端口来区分,浏览器使用域名或ip地址:端口号来访问
主要步骤:1.新建目录 mkdir /var/tmp/www 并且在该目录下新建一个index.html文件 添加
<html>
welcome to z.com'spanel
</html>
2.修改nginx.conf配置文件 添加如下信息保存退出
server {
listen 2022;
server_name localhost;
location / {
root /var/tmp/www;
index index.html;
}
}
3.检测配置信息#/app/local/nginx/sbin/nginx -t成功的话提示successful
4.重启nginx #/app/local/nginx/sbin/nginx -s reload
5.通过curl命令或者浏览器输入域名访问
# curl http://192.168.1.104:2022 <html>
welcome to z.com'spanel
</html>
三、基于IP
主要步骤:1.添加ip地址
#ifconfig |grep "inet addr"
inet addr:192.168.1.104 Bcast:192.168.1.255 Mask:255.255.255.0
inet addr:127.0.0.1 Mask:255.0.0.0
#ifconfig eth0:0 192.168.1.220 netmask 255.255.255.0 up
# ifconfig |grep "inet addr"
inet addr:192.168.1.104 Bcast:192.168.1.255 Mask:255.255.255.0
inet addr:192.168.1.220 Bcast:192.168.1.255 Mask:255.255.255.0
inet addr:127.0.0.1 Mask:255.0.0.0
2.新建目录 mkdir /app/local/nginx/html/ip并且在该目录下新建一个index.html文件 添加
<html>
this is from ip
</html>
3.修改nginx.conf配置文件 添加如下信息保存退出
server {
listen 80;
server_name 192.168.1.220;
location /{
root html/ip;
index index.html;
}
}
4.检测配置信息#/app/local/nginx/sbin/nginx -t成功的话提示successful
5.重启nginx #/app/local/nginx/sbin/nginx -s reload
6.通过curl命令或者浏览器输入域名访问
# curl http://192.168.1.220 <html>
this is from ip
</html>
一、基于域名:
主要步骤:1.首先在windows本地hosts添加虚拟机ip地址对应的域名解析
192.168.1.104 z.com
2.对应域名网站目录下新增index.html文件
3.修改nginx.conf配置文件 添加如下信息保存退出
server {
listen 80;
server_name z.com;#
location /{
root z.com;#相对路径 nginx的安装目录
index index.html;
}
}
4.检测配置信息#/app/local/nginx/sbin/nginx -t成功的话提示successful
5.重启nginx #/app/local/nginx/sbin/nginx -s reload
6.通过curl命令或者浏览器输入域名访问
# curl -xlocalhost:80 z.com
<html>
This is z.com
</html>
二、基于端口: 使用端口来区分,浏览器使用域名或ip地址:端口号来访问
主要步骤:1.新建目录 mkdir /var/tmp/www 并且在该目录下新建一个index.html文件 添加
<html>
welcome to z.com'spanel
</html>
2.修改nginx.conf配置文件 添加如下信息保存退出
server {
listen 2022;
server_name localhost;
location / {
root /var/tmp/www;
index index.html;
}
}
3.检测配置信息#/app/local/nginx/sbin/nginx -t成功的话提示successful
4.重启nginx #/app/local/nginx/sbin/nginx -s reload
5.通过curl命令或者浏览器输入域名访问
# curl http://192.168.1.104:2022 <html>
welcome to z.com'spanel
</html>
三、基于IP
主要步骤:1.添加ip地址
#ifconfig |grep "inet addr"
inet addr:192.168.1.104 Bcast:192.168.1.255 Mask:255.255.255.0
inet addr:127.0.0.1 Mask:255.0.0.0
#ifconfig eth0:0 192.168.1.220 netmask 255.255.255.0 up
# ifconfig |grep "inet addr"
inet addr:192.168.1.104 Bcast:192.168.1.255 Mask:255.255.255.0
inet addr:192.168.1.220 Bcast:192.168.1.255 Mask:255.255.255.0
inet addr:127.0.0.1 Mask:255.0.0.0
2.新建目录 mkdir /app/local/nginx/html/ip并且在该目录下新建一个index.html文件 添加
<html>
this is from ip
</html>
3.修改nginx.conf配置文件 添加如下信息保存退出
server {
listen 80;
server_name 192.168.1.220;
location /{
root html/ip;
index index.html;
}
}
4.检测配置信息#/app/local/nginx/sbin/nginx -t成功的话提示successful
5.重启nginx #/app/local/nginx/sbin/nginx -s reload
6.通过curl命令或者浏览器输入域名访问
# curl http://192.168.1.220 <html>
this is from ip
</html>
相关文章推荐
- nginx 编译安装时提示md5.h不存在
- nginx启动脚本和配置文件
- Apache与Nginx的优缺点比较
- Apache与Nginx的区别
- window下搭建nginx服务器
- windows 下Nginx+Django构造流媒体播放器
- windows 下安装nginx + php (cgi)的一些问题
- linux和mac下的nginx和php的安装
- 通过nginx配置文件抵御攻击
- nginx模块开发流程
- 基于Apriori算法的Nginx+Lua+ELK异常流量拦截方案 郑昀 基于杨海波的设计文档(转)
- Nginx的基础应用
- nginx配置账号访问
- Mac上启动nginx报错:nginx: [emerg] bind() to 0.0.0.0:80 failed (48: Address already in use)
- Nginx启动报错
- CentOS安装Nginx
- nginx负载均衡
- Nginx 开启gzip 压缩
- 使用Nginx+Lua(OpenResty)开发高性能Web应用
- LNMP/LEMP(PHP7.0.04+mysql5.7.12+nginx1.10.0)