版权声明: https://blog.csdn.net/weixin_42061048/article/details/82765918
【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 -p /home/ylt/tools
[root@ylt001 ~]# wget -q http://nginx.org/download/nginx-1.14.0.tar.gz
[root@ylt001 ~]# ll nginx-1.14.0.tar.gz
[root@ylt001 ~]# useradd nginx -s /sbin/nologin -M
[root@ylt001 ~]# tar xf nginx-1.14.0.tar.gz
[root@ylt001 ~]# cd nginx-1.14.0
[root@ylt001 ~]# ./configure --user=nginx --group=nginx --prefix=/application/nginx-1.14.0/ --with-http_stub_status_module --with-http_ssl_module
[root@ylt001 ~]# make && make install
[root@ylt001 ~]# ln -s /application/nginx-1.14.0 application/nginx
[root@ylt001 ~]# 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_module | Fast_cgi 模块,和动态应用相关 |
ngx_http_proxy_module | 代理模块 |
ngx_http_upsteam_module | 负载均衡模块 |
ngx_http_rewrite_module | URL 地址重写模块 |
ngx_http_limit_conn_module | 限制用户并发连接、请求模块 |
ngx_http_limit_req_module | 限制用户请求速率模块 |
ngx_http_log_module | 用户访问日志模块 |
ngx_http_auth_basic_module web | 访问认证模块 |
ngx_http_ssl_module | ssl 模块,用于 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