Nginx虚拟主机应用:(创建多个基于域名的虚拟主机并测试)

一:使用Nginx搭建虚拟主机服务器时,每个虚拟WEB站点拥有独立的"server {}"配置段,各自监听的IP地址、端口号可以单独指定,当然网站名称也是不同的。

二:虚拟机的分类:

1:基于域名虚拟主机(一个ip地址对应多个域名,不同域名就是不同的站点,其内容也不一样)

2:基于端口虚拟主机(服务器只有一个ip地址,不同端口就是不同的站点,其内容也不一样)

3:基于ip虚拟主机(服务器有多个ip地址,不同ip就是不同的站点,其内容也不一样)

=======================================================================

[[email protected] conf]# vim nginx.conf

user  nginx nginx;                           //nginx的程序账户及程序组
worker_processes  2;                         //指定进程数一般与cpu数量一致
worker_cpu_affinity 00000001 00000010;       //为每个进程分配核心数
error_log  logs/error.log  info;             //全局错误日志文件位置
pid        logs/nginx.pid;                   //PID文件的位置
events {
   use epoll;                                //使用epoll模型
    worker_connections  10240;               //每个进程允许的最多的连接数默认为1024一般10000以下
}
http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  ‘$remote_addr - $remote_user [$time_local] "$request" ‘
                      ‘$status $body_bytes_sent "$http_referer" ‘
                      ‘"$http_user_agent" "$http_x_forwarded_for"‘;

    access_log  logs/access.log  main;        //访问日志位
    sendfile        on;                       //支持文件发送超时
    keepalive_timeout  65;
server {                                      //web服务的监听配置
        listen       80;                      //监听地址及端口(IP:PORT)
        server_name  www.crushlinux.com;     //网站名称(FQDN)
       charset utf-8;                       //网页的默认字符集
       access_log  logs/www.crushlinux.com.access.log  main;
location / {                                //根目录配置
         root   html;             //网站根目录的位置安装位置的html中
         index  index.html index.htm;       //默认首页
 }
 location /status {
        stub_status on;                      //打开状态统计状态
        access_log off;                      //关闭此位置的日志记录
}
     error_page 500 502 503 504 /50x.html;       //内部错误的反馈页面
       location =/50x.html {                     //错误页面配置
           root html;
        }
    }
}

[[email protected] conf]# pwd
/usr/local/nginx/conf

[[email protected] conf]# mkdir ../html/mailcom
[[email protected] conf]# echo "<h1>www.crushlinux.com</h1>" > ../html/index.html

[[email protected] conf]# nginx -t

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: [warn] 10240 worker_connections exceed open file resource limit: 1024
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

[[email protected] conf]# ulimit -n 65536
[[email protected] conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

[[email protected] conf]# systemctl stop firewalld
[[email protected] conf]# iptables -F
[[email protected] conf]# setenforce 0

[[email protected] conf]# systemctl restart nginx
Warning: nginx.service changed on disk. Run ‘systemctl daemon-reload‘ to reload units.

[[email protected] conf]# /etc/init.d/nginx reload

Active connections 表示当前活跃的连接数,

第三行的三个数字表示Nginx当前总共处理了2个连接,成功创建3次握手,总共处理了2个请求。

=======================================================================

要创建两个站点www.crushlinux.com和www.cloud.com

为两个虚拟WEB主机分别建立根目录,并准备测试首页

[[email protected]~]#mkdir /usr/local/nginx/html/crushlinux

[[email protected]~]#mkdir /usr/local/nginx/html/cloud

[[email protected]~]# echo "<h1>www.crushlinux.com</h1>" >/usr/local/nginx/html/crushlinux/index.html

[[email protected]~]# echo "<h1>www.cloud.com</h1>" > /usr/local/nginx/html/cloud/index.html

[[email protected]~]# vim /usr/local/nginx/conf/nginx.conf

user  nginx nginx;
worker_processes  2;
worker_cpu_affinity 00000001 00000010;
error_log  logs/error.log  info;
pid        logs/nginx.pid;
events {
   use epoll;
    worker_connections  10240;
}
http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  ‘$remote_addr - $remote_user [$time_local] "$request" ‘
                      ‘$status $body_bytes_sent "$http_referer" ‘
                      ‘"$http_user_agent" "$http_x_forwarded_for"‘;

    access_log  logs/access.log  main;
    sendfile        on;
    keepalive_timeout  65;
server {
        listen       80;
        server_name  www.crushlinux.com;
        charset utf-8;
        access_log  logs/www.crushlinux.com.access.log  main;
location / {
            root   html/crushlinux;
            index  index.html index.htm;
        }

location ~ /status {
              stub_status on;
             access_log off;
}

 error_page  500 502 503 504   /50x.html;

location = /50x.html {
             root html;
   }
}

server {
         listen 80;
         server_name www.cloud.com;
         charset utf-8;
         access_log logs/cloud.access.log main;
 location / {
         root html/cloud;
         index index.html index.html;
}
         error_page 500 502 503 504 /50x.html;
  location = /50x.html {
         root html;
    }
  }
}

[[email protected] ~]# killall -3 nginx               //正常停止
[[email protected] ~]# nginx                            //正常启动

[[email protected] ~]# yum -y install elinks
[[email protected] ~]# elinks --dump http://www.crushlinux.com
www.crushlinux.com
[[email protected] ~]# elinks --dump http://www.cloud.com
www.cloud.com

原文地址:https://www.cnblogs.com/cxm123123form/p/11601827.html

时间: 2024-08-01 00:36:30

Nginx虚拟主机应用:(创建多个基于域名的虚拟主机并测试)的相关文章

13_搭建Nginx服务器、配置网页认证、基于域名的虚拟主机、ssl虚拟主机

官方yum源:[nginx]name=nginx repobaseurl=http://nginx.org/packages/centos/$releasever/$basearch/gpgcheck=0enabled=1 pc71. 安装nginx]# yum -y install nginx]# nginx]# nginx -Vnginx version: nginx/1.16.1]# netstat -anptu | grep nginx]# curl http://10.10.11.10

CentOS7.4—nginx应用之基于域名的虚拟主机

Nginx功能应用-虚拟主机目录:第一部分:准备工作第二部分:搭建nginx第三部分:搭建基于域名的虚拟主机 第一部分 准备工作一:服务器:Linux系统-CentOS 7.4:IP地址:192.168.80.10 客户端:以WIN7为例,测试验证结果,与服务器在同一网段:IP地址:192.168.80.2 二:准备压缩包 三:将防火墙与selinux关闭 第二部分 安装Nginx服务一:安装编译工具与插件[[email protected] ~]# yum -y install \ gcc \

Nginx配置多个基于域名的虚拟主机+实验环境搭建+测试

标签:Linux 域名 Nginx 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://xpleaf.blog.51cto.com/9315560/1901284 0.说明 使用Nginx可以配置基于域名的虚拟主机.基于端口的虚拟主机和基于端口的虚拟主机,比较常用的是基于域名的虚拟主机,这里要做的配置是基于域名的虚拟主机,并且是配置多个基于域名的虚拟主机. 关于Nginx配置文件的说明可以参考官方文档,同时也可以参考老男孩老师的书

Nginx基于域名的虚拟主机

1.1 问题 沿用练习二,配置基于域名的虚拟主机,实现以下目标: 实现两个基于域名的虚拟主机,域名分别为www.aa.com和www.bb.com 对域名为www.aa.com的站点进行用户认证,用户名称为tom,密码为123456 1.2 方案 修改Nginx配置文件,添加server容器实现虚拟主机功能:对于需要进行用户认证的虚拟主机添加auth认证语句. 3.3 步骤 实现此案例需要按照如下步骤进行. 步骤一:修改配置文件 1)修改Nginx服务配置,添加相关虚拟主机配置如下 [[emai

nginx服务做用户认证和基于域名的虚拟主机

实验一.用nginx怎么实现用户访问时的认证 一.目标        通过调整Nginx服务端配置,实现以下目标: 访问Web页面需要进行用户认证 用户名为:tom,密码为:123456 二.方案         通过Nginx实现Web页面的认证,需要修改Nginx配置文件,在配置文件中添加auth语句实现用户认证.    最后使用htpasswd命令创建用户及密码即可,服务端:192.168.4.102,客户端:192.168.4.101 三.实施步骤(nginx服务安装见我的"搭建ngin

nginx的简介和搭建基于域名的虚拟主机

今天就来和大家讲一讲nginx和基于域名搭建虚拟主机 简介 Nginx (engine x) 是一个高性能的Web服务器和反向代理服务器,也是一个IMAP/POP3/SMTP服务器 俄罗斯程序员Igor Sysoev于2002年开始Nginx是增长最快的Web服务器,市场份额已达33.3%全球使用量排名第二2011年成立商业公司 Nginx源码结构: 代码量大约11万行C代码源代码目录结构core (主干和基础设置)event (事件驱动模型和不同的IO复用模块)http (HTTP服务器和模块

nginx基于域名的虚拟主机配置

与apache服务器类似,nginx也有基于域名,IP及端口的虚拟主机配置,在实际工作场景中,基于域名的虚拟主机配置较常见.nginx服务的主要配置文件nginx.conf[[email protected] conf]# ls -l nginx.conf-rw-r--r-- 1 root root 2788 Jan 14 17:41 nginx.conf[[email protected] conf]# pwd/application/nginx/conf 去掉注释及空行后的配置文件[[ema

?搭建LAMP环境及快速部署双网站并实现基于域名的虚拟主机

本节所讲内容: 实战:搭建LAMP环境及快速部署双网站并实现基于域名的虚拟主机 LAMP架构:??? Linux+Apache+Mysql+PHP Linux+Apache+Mysql/MariaDB+Perl/PHP/Python一组常用来搭建动态网站或者服务器的开源软件,共同组成了一个强大的Web应用程序平台. 一.安装需要的软件包 [[email protected] ~]# yum install httpd mysql-server mysql php php-mysql  -y ht

第二章 Web网站服务(二)——搭建基于域名的虚拟web主机

防伪码:自古逢秋悲寂寥,我言秋日胜春朝 web网站服务(二) 一.httpd服务的访问控制 作用: a.控制对网站资源的访问 b.为特定的网站目录添加访问授权 常用访问控制方式: a.客户机地址限制 b.用户授权限制 二.基于客户端地址的访问控制 Order配置项,定义控制顺序 先允许后拒绝,默认拒绝所有:Order allow,deny 先拒绝后允许,默认允许所有:Order deny,allow Allow.Deny配置项,设置允许或拒绝的地址 Deny from address1 addr