zoukankan      html  css  js  c++  java
  • Nginx三种模式的虚拟主机(附Apache基于域名的虚拟主机)

    1.安装nginx

    # pcre中文"perl兼容正则表达式",安装pcre库是为了让nginx支持具备URL重写功能
    # 的Rewrite模块,rewrite可以实现动态页面转成伪静态、301网页跳转等功能.
    yum -y install pcre pcre-devel openssl openssl-devel gcc gcc+
    useradd www -M -s /sbin/nologin
    useradd oldboy
    mkdir /home/oldboy/tools/
    cd /home/oldboy/tools/
    wget http://nginx.org/download/nginx-1.8.0.tar.gz
    tar xf nginx-1.8.0.tar.gz
    # 将这句注释掉取消Debug编译模式 大概在179行
    sed -i "179s/#//" auto/cc/gcc
    ./configure --prefix=/opt/nginx --user=www --group=www 
    --with-http_stub_status_module --with-http_ssl_module
    make
    make install
    mkdir /application
    ln -s /opt/nginx/ /application/nginx
    # 开发用的目录地址是/application/nginx,好处是如果nginx要升级,就可以升完级,
    # 然后删除软链接,再软链一个一样的目录名--/application/nginx
    curl -I 10.0.0.8
    
    HTTP/1.1 200 OK
    Server: nginx/1.8.0
    Date: Tue, 11 Dec 2018 18:01:55 GMT
    Content-Type: text/html
    Content-Length: 612
    Last-Modified: Tue, 11 Dec 2018 17:38:35 GMT
    Connection: keep-alive
    ETag: "5c0ff61b-264"
    Accept-Ranges: bytes
    
    # 排错三部曲--ping、telnet、wget(或curl -I)
    

    2.配置

    # 把空行、注释行都去掉
    egrep -v "^$|#" nginx.conf.default >nginx.conf
    配置文件中index  index.html index.htm;的意思是:访问URL什么都不跟时,会从index后面的文件挨个查找.
    root   html;指站点的根目录是html,相对于安装目录来说;
    # 更改了软链接的内容,源文件或源目录也会相应发生变化.
    # windows下命令窗口输入drivers可以迅速找到hosts文件.
    nginx使用一个server{}标签来表示一个虚拟主机,一个web服务可以有多个
    虚拟主机标签对,即-可以同时支持多个虚拟主机站点.
    

    wget的三个参数:

    -O:下载的文件存放到指定的文件夹下,同时重命名下载的文件;

    -c:断点续传;

    -P LOCAL_DIR:保存所有的文件或目录到指定的目录下.

    3.访问基于域名的虚拟主机的基本流程

    在浏览器输入域名,经DNS解析为IP地址之后,在请求头的host中携带着域名就去服务器端了,

    服务器会根据请求头中的域名去做location匹配;

    直接拿IP去访问服务器,默认是返回第一个匹配到的网页.

    ping这些网站,返回的都是一个IP:www.51cto.com、blog.51cto.com、home.51cto.com、edu.51cto.com

    在server中启用此参数--autoindex on,可以使此目录变成下载目录.

    cat nginx.conf
    worker_processes  1;
    events {
        worker_connections  1024;
    }
    http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;
        keepalive_timeout  65;
        server {
            listen       80;
            server_name  www.etiantian.com;
            location / {
                root   html/www;
                index  index.html index.htm;
            }
        }
        server {
            listen       80;
            server_name  bbs.etiantian.com;
            location / {
                root   html/bbs;
                index  index.html index.htm;
            }
        }
    }
    # 直接用IP地址访问,请求头的host中没有域名,会把匹配到的第一个文件返回:
    curl 10.0.0.8 -- www
    

    4.基于端口的虚拟主机

    用10.0.0.61去测试,在hosts中添加:
    172.16.1.8    web01 www.etiantian.com bbs.etiantian.com
    
    worker_processes  1;
    events {
        worker_connections  1024;
    }
    http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;
        keepalive_timeout  65;
        server {
            listen       80;
            server_name  www.etiantian.com;
            location / {
                root   html/www;
                index  index.html index.htm;
            }
        }
        server {
            listen       81;
            server_name  bbs.etiantian.com;
            location / {
                root   html/bbs;
                index  index.html index.htm;
            }
        }
    }
    此时分别去访问这两个域名:
    curl www.etiantian.com -- www
    curl bbs.etiantian.com -- www
    

    此时分别去访问这两个域名:

    curl www.etiantian.com -- www

    curl bbs.etiantian.com -- www

    都不加端口,会发现返回内容都是www,第一个是对的,第二个返回内容不是我们想要的结果,

    因为以bbs.etiantian.com不加端口访问时,默认是80,经DNS解析的IP地址是172.16.1.8,

    带着这样的请求头去找服务器,没有相应的匹配,就会返回匹配到的第一个html.

    5.基于IP的虚拟主机

    # ifconfig添加的IP叫别名IP
    ifconfig eth0:1 10.0.0.101/24 up
    ifconfig eth0:1 down
    # ip添加的IP叫辅助IP,加了label之后,会让你看不出来是用哪个命令配的
    # 一出手就是专业,高手打羽毛球不会让你预判出他发的是什么球
    ip addr add 10.0.0.101/24 dev eth0 label eth0:2
    ip addr del 10.0.0.101/24 dev eth0 label eth0:2
    
    worker_processes  1;
    events {
        worker_connections  1024;
    }
    http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;
        keepalive_timeout  65;
        server {
            listen       10.0.0.8:80;
            server_name  www.etiantian.com;
            location / {
                root   html/www;
                index  index.html index.htm;
            }
        }
        server {
            listen       10.0.0.101:80;
            server_name  bbs.etiantian.com;
            location / {
                root   html/bbs;
                index  index.html index.htm;
            }
        }
    }
    curl 10.0.0.8 -- www
    curl 10.0.0.101 -- bbs
    

    6.日志及主机别名

    worker_processes  1;
    events {
        worker_connections  1024;
    }
    # 可以放在main、http、server、location等位置
    # 用较低级别的日志级别(如:debug|info|notice)会产生很大的磁盘I/O
    error_log logs/error.log error
    http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;
    	log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    				  '$status $body_bytes_sent "$http_referer" '
    				  '"$http_user_agent" "$http_x_forwarded_for"';
        keepalive_timeout  65;
        incloud    extra/*.conf
    }
    

    别名作用:

    1.可以实现访问两个网站www.etiantian.com、etiantian.com,其实是访问的一个;

    2.比如几台web服务器的主域名相同,监控每台机器状态时,可以通过别名来准确监控.

    7.nginx状态模块

    cat >> /application/nginx/conf/extra/status.conf<<EOF
    # status
    server{
       listen 80;
       server_name  status.etiantian.com;
       location /nginx_status {
        stub_status on;
        access_log off;
        allow 10.0.0.0/24;
        deny all;
         }
    }
    EOF
    # 在10.0.0.61上访问测试
    curl status.etiantian.com/nginx_status
    <html>
    <head><title>403 Forbidden</title></head>
    <body bgcolor="white">
    <center><h1>403 Forbidden</h1></center>
    <hr><center>nginx/1.8.0</center>
    </body>
    </html>
    # 这是因为在10.0.0.61的hosts中status.etiantian.com被解析为172.16.1.8,
    # 服务器端对IP段进行了限制,能与172.16.1.8通信的是172.16.1.61,
    # 所以服务器返回了403,修改hosts之后即可正常访问--10.0.0.8  status.etiantian.com
    172.16.1.61 - - [15/Dec/2018:20:58:31 +0800] "GET / HTTP/1.1" 200 4 "-" 
    

    Active connections: 120
    server accepts handled requests
     200 200 400
    Reading: 40 Writing: 20 Waiting: 60

    Active connections:表示nginx正在处理的活动连接数;

    server:从启动到现在共处理了200个连接;

    accepts:从启动到现在共创建了200次握手;

    请求丢失数=握手数-连接,可以看出,本次状态统计中没有丢失请求;

    handled requests:总共处理了400次请求;

    Reading:nginx读到客户端的Header信息数;

    Writing:nginx返回给客户端的Header信息数;

    Waiting:已经处理完正在等候下一次请求指令的驻留连接,开启 keep-alive 的情况下,这个值等于active–(reading+writing)

    8.Apache基于域名的虚拟主机(IP地址10.0.0.7)

    mkdir /server/tools/
    cd /server/tools/
    wget http://archive.apache.org/dist/httpd/httpd-2.2.32.tar.gz
    tar xf httpd-2.2.32.tar.gz
    cd httpd-2.2.32/
    yum -y install zlib zlib-devel
    ./configure --prefix=/application/apache2.2.32 
    --enable-deflate --enable-expires --enable-headers 
    --enable-modules=most --enable-so --enable-rewrite --with-mpm=worker
    make
    make install
    /application/apache/htdocs
    mkdir www blog bbs
    # 去掉配置文件中396行的注释
    vi /application/apache/conf/httpd.conf +406
    Include conf/extra/httpd-languages.conf
    vi /application/apache/conf/httpd.conf +98
    ServerName 127.0.0.1:80
    # 取消把根目录当做下载站点功能
    vi httpd.conf +145
    Options -Indexes FollowSymLinks
    
    cat /application/apache/conf/extra/httpd-vhosts.conf
    NameVirtualHost *:80
    <VirtualHost *:80>
        ServerAdmin 1746465163-@qq.com
        DocumentRoot "/application/apache2.2.32/htdocs/www"
        ServerName www.etiantian.com
        ServerAlias etiantian.com
        ErrorLog "logs/www-error_log"
        CustomLog "logs/www-access_log" common
    </VirtualHost>
    
    <VirtualHost *:80>
        ServerAdmin 1746465163-@qq.com
        DocumentRoot "/application/apache2.2.32/htdocs/bbs"
        ServerName bbs.etiantian.com
        ErrorLog "logs/bbs-error_log"
        CustomLog "logs/bbs-access_log" common
    </VirtualHost>
    
    <VirtualHost *:80>
        ServerAdmin 1746465163-@qq.com
        DocumentRoot "/application/apache2.2.32/htdocs/blog"
        ServerName blog.etiantian.com
        ErrorLog "logs/blog-error_log"
        CustomLog "logs/blog-access_log" common
    </VirtualHost>
    # 没有首页文件只是报403 Forbidden的原因之一
    echo "apache blog" > blog/index.html
    echo "apache bbs" > bbs/index.html
    /application/apache/bin/apachectl -t
    /application/apache/bin/apachectl start
    /application/apache/bin/apachectl graceful
    
    # apache更改网站的根目录之后,访问报403
    <VirtualHost *:80>
        ServerAdmin 1746465163-@qq.com
        DocumentRoot "/var/html/www"
        ServerName www1.etiantian.com
        ErrorLog "logs/blog-error_log"
        CustomLog "logs/blog-access_log" common
    </VirtualHost>
    # 这是因为配置文件中没有新增这个目录路径
    <Directory "/application/apache2.2.32/htdocs">
        Options -Indexes FollowSymLinks
        AllowOverride None
        Order allow,deny
        Allow from all
    </Directory>
    
    <Directory "/var/html">
        Options -Indexes FollowSymLinks
        AllowOverride None
        Order allow,deny
        Allow from all
    </Directory>
    
    查询第n-m条数据的sql:
    select * from show_md5_status limit 50,100;
    select * from show_md5_status order by id desc limit 100;
    
  • 相关阅读:
    TortoiseSVN 使用详细步骤(三):安装
    TortoiseSVN使用详细步骤(二)
    TortoiseSVN使用详细步骤(一)
    IIS7下访问ashx页面,显示404
    Learning Python 008 正则表达式-003 search()方法
    Learning Python 008 正则表达式-002 findall()方法
    Learning Python 008 正则表达式-001
    Learning Python 007 基本语句
    Learning Python 006 list(列表) 和 tuple(元组)
    Learning Python 005 字符串和编码
  • 原文地址:https://www.cnblogs.com/fawaikuangtu123/p/10170739.html
Copyright © 2011-2022 走看看