linux系统CentOS7
Nginx1.9.9
一台nginx服务器同一IP被注册多个不同域名,访问不同域名到该服务器后请求不同项目
本台nginx服务器的IP地址为 192.168.155.129
假设服务器有两个项目websuit_a,websuit_b,客户端访问websuit_a.com时请求websuit_a项目,访问websuit_b.com时请求websuit_b项目 首先在nginx服务器项目根目录下新建项目文件夹websuit_a和websuit_b用来存放两个虚拟站点的PHP代码
mkdir -m777 -p /usr/local/nginx/html/websuit_a /usr/local/nginx/html/websuit_b 在websuit_a项目下新建index.php文件,并输入以下内容
<?php echo "this is websuit_a";?>
vim /usr/local/nginx/html/websuit_a/index.php 在websuit_b项目下新建index.php文件,并输入以下内容
<?php echo "this is websuit_b";?>
vim /usr/local/nginx/html/websuit_b/index.php 在nginx配置文件夹内新建vhosts_conf文件夹保存不同虚拟站点的配置文件
mkdir -m777 -p /usr/local/nginx/conf/vhosts_conf 在里面创建websuit_a.com站点的配置文件,命名为websuit_a.conf编辑填写以下内容保存
server {
listen 80; #监听的端口号
server_name websuit_a.com; #客户端访问进来的域名
#access_log logs/host.access.log main;
location / {
root html/websuit_a; #站点的项目路径也可填成绝对路径/usr/local/nginx/html/websuit_a
index index.html index.htm index.php;
#站点的rewrite在这里写,URL重写就是首先获得一个进入的URL请求然后把它重新写成网站可以处理的另一个URL的过程
#nginx支持.htaccess,但不支持.htaccess里原生路由重写规则,要以nginx的路由重写规则写在.htaccess才可执行,所以,一般nginx路由重写规则直接写在配置文件中,不用.htaccess文件
rewrite ^/(\w+)\.html$ /$1.php;
rewrite ^/(\w+)/(\w+)$ /$1/$2.php;
}
#错误页的配置
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
root html/websuit_a;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html/websuit_a$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
vim /usr/local/nginx/conf/vhosts_conf/websuit_a.conf 再创建websuit_b.com站点的配置文件,命名为websuit_b.conf编辑填写以下内容保存
server {
listen 80; #监听的端口号
server_name websuit_b.com; #客户端访问进来的域名
#access_log logs/host.access.log main;
location / {
root html/websuit_b; #站点的项目路径也可填成绝对路径/usr/local/nginx/html/websuit_b
index index.html index.htm index.php;
#站点的rewrite在这里写
rewrite ^/(\w+)\.html$ /$1.php;
rewrite ^/(\w+)/(\w+)$ /$1/$2.php;
}
#错误页的配置
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
root html/websuit_b;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html/websuit_b$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
vim /usr/local/nginx/conf/vhosts_conf/websuit_b.conf 关闭nginx服务
service nginx stop 编辑nginx配置文件在http {}块内的最后一行添加如下内容
include /usr/local/nginx/conf/vhosts_conf/*.conf;
vim /usr/local/nginx/conf/nginx.conf 启动nginx
service nginx start 由于不是外网环境,只能在本地访问,所以修改客户端hosts文件,添加以下两项
192.168.155.129 websuit_a.com
192.168.155.129 websuit_b.com 分别在客户端浏览器上输入
websuit_a.com,websuit_b.com
分别显示
this is websuit_a,this is websuit_b

linux Nginx VirtualHost虚拟主机多站点设置的更多相关文章

  1. 企业级Nginx基于虚拟主机别名的设置

    生活中访问www.baidu.com和baidu.com是一个效果,同理也可以用rewrite 301跳转的思路(多发了一次请求过去)配置nginx.conf文件或者include里面的引用的文件,道 ...

  2. [转] linux学习第四十四篇:Nginx安装,Nginx默认虚拟主机,Nginx域名重定向

    Nginx安装 进入存放源码包的目录: cd /usr/local/src 下载源码包: wget http://nginx.org/download/nginx-1.12.1.tar.gz 解压: ...

  3. nginx 搭建虚拟主机

    一.排错三部曲 第一步在客户端上ping服务端ip  ping 10.0.0.8 第二部在客户端上telnet服务器端IP.端口  telnet 10.0.0.8 第三部在客户端使用wget命令检测 ...

  4. 细说Linux下的虚拟主机那些事儿

    细说Linux下的虚拟主机那些事儿 我们知道Linux操作系统是目前在服务器上应用广泛的操作系统.在Linux操作系统下的虚拟主机是不是就是我们常说的Linux虚拟主机呢?其实从专业方面说并不是,它是 ...

  5. nginx配置虚拟主机vhost的方法详解

    Nginx vhost配置,可实现基于ip.端口号.servername的虚拟主机,同时可避免直接修改主配置文件.在nginx下配置虚拟主机vhost非常方便.这篇文章主要介绍了nginx配置虚拟主机 ...

  6. httpd虚拟主机、站点访问控制、基于用户的访问控制、持久链接等应用配置实例

    httpd配置内容 httpd2.2 配置文件: /etc/httpd/conf/httpd.conf /etc/httpd/conf.d/*.conf 服务脚本: /etc/rc.d/init.d/ ...

  7. Nginx中虚拟主机配置

    一.Nginx中虚拟主机配置 1.基于域名的虚拟主机配置 1.修改宿主机的hosts文件(系统盘/windows/system32/driver/etc/HOSTS) linux : vim /etc ...

  8. Apache 创建虚拟主机目录和设置默认访问页面

    虚拟主机 (Virtual Host) 是在同一台机器搭建属于不同域名或者基于不同 IP 的多个网站服务的技术. 可以为运行在同一物理机器上的各个网站指配不同的 IP 和端口, 也可让多个网站拥有不同 ...

  9. 烂泥:使用nginx利用虚拟主机搭建WordPress博客

    本文由秀依林枫提供友情赞助,首发于烂泥行天下. 最近开始打算学习nginx web服务器,既然是学习还是以实用为目的的.我们在此以搭建WordPress博客为例. 搭建WordPress博客,我们需要 ...

随机推荐

  1. SQL server 测试

    设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的表(一)~表(四)所示,数据如表1-2的表 ...

  2. A trip through the Graphics Pipeline 2011_07_Z/Stencil processing, 3 different ways

    In this installment, I’ll be talking about the (early) Z pipeline and how it interacts with rasteriz ...

  3. asp.net identity 2.2.0 中角色启用和基本使用(五)

    建立控制器UsersAdminController 第一步:在controllers文件夹上点右键>添加>控制器, 我这里选的是“MVC5 控制器-空”,名称设置为:UsersAdminC ...

  4. 使用DB4o做一个.Net版的website(一)环境

    一个机缘巧合之下,知道了DB4o这个数据库引擎,下载查看之后,被其方便.高效.以及便捷的管理方式锁折服. 故决定使用其做一个.NET版本的web站点,来巩固学到的知识,以及为后来人做一点点贡献. 首先 ...

  5. 简单的cc攻击防御

    简单的cc攻击防御cckiller 一.下载#wget wget --no-check-certificate https://zhangge.net/wp-content/uploads/files ...

  6. play for scala 实现SessionFilter 过滤未登录用户跳转到登录页面

    一.编写SessionFilter.scala代码 package filters import javax.inject.{Inject, Singleton} import akka.stream ...

  7. 构建Logstash+tomcat镜像(让logstash收集tomcat日志)

    1.首先pull logstash镜像作为父镜像(logstash的Dockerfile在最下面): 2.构建my-logstash镜像,使其在docker镜像实例化时,可以使用自定义的logstas ...

  8. linux查看是否被入侵

    一.检查系统日志 lastb命令检查系统错误登陆日志,统计IP重试次数 二.检查系统用户 1.cat /etc/passwd查看是否有异常的系统用户 2.grep “0” /etc/passwd查看是 ...

  9. Android-项目介绍

    一个.net开发人员 在了解android项目只能凭自己的理解慢慢来了! 新建项目 右击 New-JAVA Application Project 傻瓜似的下一步骤填写每一步 文件介绍 Android ...

  10. Java 使用jaxp修改节点

    <?xml version="1.0" encoding="UTF-8" standalone="no"?> <perso ...