【Web 集群实战】06_Nginx 安装与虚拟主机配置

【Web 集群实战】06_Nginx 安装与虚拟主机配置

标签(空格分隔): Web集群实战


1. 编译安装 Nginx

  • 安装 Nginx 所需的 pcre 库(perl compatible regular expressions,perl 兼容正则表达式)
    • 使 Nginx 支持具备 URI 重写功能的 rewrite 模块
[root@ylt001 ~]# yum -y install pcre pcre-devel 
[root@ylt001 ~]# rpm -qa pcre pcre-devel 
  • 安装 openssl-devel
    • Nginx 在使用 HTTPS 服务时需要用到此模块
[root@ylt001 ~]# yum -y install openssl openssl-devel 
[root@ylt001 ~]# rpm -qa openssl openssl-devel 
  • 安装 Nginx
[root@ylt001 ~]# mkdir -p /home/ylt/tools
[root@ylt001 ~]# cd  /home/ylt/tools 
[root@ylt001 tools]# wget http://nginx.org/download/nginx-1.14.0.tar.gz
[root@ylt001 tools]# ll nginx-1.14.0.tar.gz
[root@ylt001 tools]# useradd nginx -s /sbin/nologin -M
[root@ylt001 tools]# tar xf nginx-1.14.0.tar.gz
[root@ylt001 tools]# cd nginx-1.14.0
[root@ylt001 nginx-1.14.0]# ./configure --user=nginx --group=nginx --prefix=/application/nginx-1.14.0/ --with-http_stub_status_module --with-http_ssl_module
[root@ylt001 nginx-1.14.0]# make && make install
[root@ylt001 nginx-1.14.0]# ln -s /application/nginx-1.14.0 /application/nginx
[root@ylt001 nginx-1.14.0]# ll /application/nginx
  • 检查配置文件语法
[root@ylt001 ~]# /application/nginx/sbin/nginx -t
  • 启动 Nginx 服务
[root@ylt001 ~]# /application/nginx/sbin/nginx 
  • 查看 Nginx 服务对应的端口是否成功启动
[root@ylt001 ~]# lsof -i :80

[root@ylt001 ~]# netstat -lnt|grep 80

[root@ylt001 ~]# ps -ef|grep nginx
  • 检查 Nginx 启动的实际效果

在 Windows 下:在浏览器输入 IP 地址
在 Linux 下:

[root@ylt001 ~]# wget 127.0.0.1

[root@ylt001 ~]# curl 127.0.0.1

2. 部署一个 Web 站点

  • 重新编辑 Nginx 的默认首页文件内容
[root@ylt001 ~]# cd /application/nginx/html
[root@ylt001 html]# rm -f index.html
[root@ylt001 html]# vi index.html

3. 深入了解 Nginx

  • 企业场景常用的 Nginx http 功能模块
ngx_http_core_module包括一些http核心参数配置
ngx_http_access_module访问控制模块
ngx_http_gzip_module压缩模块,优化
ngx_http_fastcgi_moduleFast_cgi 模块,和动态应用相关
ngx_http_proxy_module代理模块
ngx_http_upsteam_module负载均衡模块
ngx_http_rewrite_moduleURL 地址重写模块
ngx_http_limit_conn_module限制用户并发连接、请求模块
ngx_http_limit_req_module限制用户请求速率模块
ngx_http_log_module用户访问日志模块
ngx_http_auth_basic_module web访问认证模块
ngx_http_ssl_modulessl 模块,用于 https 连接
ngx_http_stub_status_module记录 Nginx 基本访问状态信息等模块
  • Nginx 目录结构
[root@ylt001 ~]# yum install tree -y
[root@ylt001 ~]# tree /application/nginx/

4. Nginx 虚拟主机配置

4.1 虚拟主机的概念和类型介绍

4.1.1 虚拟主机概念
  • 所谓虚拟主机,在 web 服务里就是一个独立的网站站点,这个站点对应独立的域名(也可能是 IP 或端口),具有独立的程序及资源目录,可以独立地对外提供服务供用户访问。
  • 这个独立的站点在配置里是由一定格式的标签段标记的,对于 Apache 软件来说,一个虚拟主机的标签段通常被包 含在 内,而 Nginx 软件则使用一个 server{} 标签来标示一个虚拟主机。一个 web 服务里可以有多个虚拟主机标签对,即可以同时支持多个虚拟主机站点。
4.1.2 虚拟主机类型
  • 基于域名的虚拟主机

    • 通过不同的域名区分不同的虚拟主机。
  • 基于端口的虚拟主机

    • 通过不同的端口来区分不同的虚拟主机。基于端口的虚拟主机,地址里要带有端口。
  • 基于 IP 的虚拟主机

    • 通过不同的 IP 区分虚拟主机。

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

配置基于域名的 nginx.conf 内容
[root@ylt001 ~]# cd /application/nginx/conf
[root@ylt001 conf]# diff nginx.conf.default nginx.conf
// 过滤包含 # 号和空行
[root@ylt001 conf]# egrep -v "#|^$" nginx.conf.default >nginx.conf
  • 配置一个基于 www.yangyangyang.org 域名的站点
worker_processes 1;
events {
    worker_connections 1024;
}
http {
    include         mime.types;
    default_type    application/octet-stream;
    sendfile        on;
    keepalived_timeout 65;
    Server {
        listen  80;
        server_name www.yangyangyang.org;
        location / {
            root    html/www;
            index.html index.htm;
        }
    }
}
创建域名对应的站点目录及文件
[root@ylt001 conf]# mkdir ../html/www -p
[root@ylt001 conf]# echo "http://www.yangyangyang.org" >../html/www/index.html
[root@ylt001 conf]# cat ../html/www/index.html
检查语法并重新加载 Nginx
[root@ylt001 conf]# ../sbin/nginx -t
[root@ylt001 conf]# ../sbin/nginx -s reload
检查 Nginx 重新加载后的情况
[root@ylt001 conf]# lsof -i :80

[root@ylt001 conf]# netstat -lnt|grep 80

[root@ylt001 conf]# ps -ef|grep nginx
测试域名站点配置的访问结果
  • Linux 客户端
[root@ylt001 conf]# echo "192.168.2.129 www.yangyangyang.org" >>/etc/hosts
[root@ylt001 conf]# tail -1 /etc/hosts
[root@ylt001 conf]# curl www.yangyangyang.org
  • Windows 客户端
    • 添加hosts
    • ping www.yangyangyang.org
    • 浏览器检查

4.3 配置多个基于域名的虚拟主机

增加新域名对应的配置
worker_processes 1;
events {
    worker_connections 1024;
}
http {
    include         mime.types;
    default_type    application/octet-stream;
    sendfile        on;
    keepalived_timeout 65;
    Server {
        listen  80;
        server_name www.yangyangyang.org;
        location / {
            root    html/www;
            index.html index.htm;
        }
    }
    Server {
        listen  80;
        server_name bbs.yangyangyang.org;
        location / {
            root    html/bbs;
            index.html index.htm;
        }
    }
    Server {
        listen  80;
        server_name blog.yangyangyang.org;
        location / {
            root    html/blog;
            index.html index.htm;
        }
    }
}
创建新虚拟主机站点对应的目录和文件
[root@ylt001 conf]# .mkdir -p ../html/bbs  ../html/blog
[root@ylt001 conf]# echo "http://bbs.yangyangyang.org" >../html/bbs/index.html
[root@ylt001 conf]# echo "http://blog.yangyangyang.org" >../html/blog/index.html
[root@ylt001 conf]# cat ../html/bbs/index.html
[root@ylt001 conf]# cat ../html/blog/index.html
[root@ylt001 conf]# LANG=en
[root@ylt001 conf]# tree ../html
重新加载 Nginx 配置
[root@ylt001 conf]# ../sbin/nginx -t
[root@ylt001 conf]# ../sbin/nginx -s reload
在客户端测试
  • Linux 客户端
[root@ylt001 conf]# tail -1 /etc/hosts
[root@ylt001 conf]# curl www.yangyangyang.org
[root@ylt001 conf]# curl blog.yangyangyang.org
[root@ylt001 conf]# curl bbs.yangyangyang.org
  • Windows 客户端
    • 添加hosts
    • ping
    • 浏览器检查
备份当前配置
[root@ylt001 conf]# /bin/cp nginx.conf nginx.conf_BaseName

4.4 基于端口的虚拟主机配置

配置虚拟主机监听的端口
worker_processes 1;
events {
    worker_connections 1024;
}
http {
    include         mime.types;
    default_type    application/octet-stream;
    sendfile        on;
    keepalived_timeout 65;
    Server {
        listen  80;
        server_name www.yangyangyang.org;
        location / {
            root    html/www;
            index.html index.htm;
        }
    }
    Server {
        listen  81;
        server_name bbs.yangyangyang.org;
        location / {
            root    html/bbs;
            index.html index.htm;
        }
    }
    Server {
        listen  82;
        server_name blog.yangyangyang.org;
        location / {
            root    html/blog;
            index.html index.htm;
        }
    }
}
检查语法并重新加载 Nginx
[root@ylt001 conf]# ../sbin/nginx -t
[root@ylt001 conf]# ../sbin/nginx -s reload
检查 Nginx 重新加载后的情况
[root@ylt001 conf]# lsof -i :80

[root@ylt001 conf]# netstat -lnt|grep 80

[root@ylt001 conf]# ps -ef|grep nginx
测试域名站点配置的访问结果

浏览器访问如下 3 个地址:

http://www.yangyangyang.org:80
http://bbs.yangyangyang.org:81
http://blog.yangyangyang.org:82
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值