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

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

二:虚拟机的分类:

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

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

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

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

[root@localhost 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; } } }

[root@localhost conf]# pwd
/usr/local/nginx/conf

[root@localhost conf]# mkdir ../html/mailcom
[root@localhost conf]# echo "<h1>www.crushlinux.com</h1>" > ../html/index.html

[root@localhost 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

[root@localhost conf]# ulimit -n 65536
[root@localhost 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

[root@localhost conf]# systemctl stop firewalld
[root@localhost conf]# iptables -F
[root@localhost conf]# setenforce 0

[root@localhost conf]# systemctl restart nginx
Warning: nginx.service changed on disk. Run 'systemctl daemon-reload' to reload units.

[root@localhost conf]# /etc/init.d/nginx reload

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

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

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

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

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

[root@localhost~]#mkdir /usr/local/nginx/html/crushlinux

[root@localhost~]#mkdir /usr/local/nginx/html/cloud

[root@localhost~]# echo "<h1>www.crushlinux.com</h1>" >/usr/local/nginx/html/crushlinux/index.html

[root@localhost~]# echo "<h1>www.cloud.com</h1>" > /usr/local/nginx/html/cloud/index.html

[root@localhost~]# 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;
    }
  }
}

[root@www ~]# killall -3 nginx               //正常停止
[root@www ~]# nginx                            //正常启动

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

 

猜你喜欢

转载自www.cnblogs.com/cxm123123form/p/11601827.html
今日推荐