[关闭]
@yanglt7 2018-10-21T15:53:44.000000Z 字数 5509 阅读 735

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

Web集群实战


1. 编译安装 Nginx

  1. [root@ylt001 ~]# yum -y install pcre pcre-devel
  2. [root@ylt001 ~]# rpm -qa pcre pcre-devel
  1. [root@ylt001 ~]# yum -y install openssl openssl-devel
  2. [root@ylt001 ~]# rpm -qa openssl openssl-devel
  1. [root@ylt001 ~]# mkdir -p /home/ylt/tools
  2. [root@ylt001 ~]# cd -p /home/ylt/tools
  3. [root@ylt001 ~]# wget -q http://nginx.org/download/nginx-1.14.0.tar.gz
  4. [root@ylt001 ~]# ll nginx-1.14.0.tar.gz
  5. [root@ylt001 ~]# useradd nginx -s /sbin/nologin -M
  6. [root@ylt001 ~]# tar xf nginx-1.14.0.tar.gz
  7. [root@ylt001 ~]# cd nginx-1.14.0
  8. [root@ylt001 ~]# ./configure --user=nginx --group=nginx --prefix=/application/nginx-1.14.0/ --with-http_stub_status_module --with-http_ssl_module
  9. [root@ylt001 ~]# make && make install
  10. [root@ylt001 ~]# ln -s /application/nginx-1.14.0 application/nginx
  11. [root@ylt001 ~]# ll application/nginx
  1. [root@ylt001 ~]# /application/nginx/sbin/nginx -t
  1. [root@ylt001 ~]# /application/nginx/sbin/nginx
  1. [root@ylt001 ~]# lsof -i :80

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

  1. [root@ylt001 ~]# ps -ef|grep nginx
  1. [root@ylt001 ~]# wget 127.0.0.1

  1. [root@ylt001 ~]# curl 127.0.0.1

2. 部署一个 Web 站点

  1. [root@ylt001 ~]# cd /application/nginx/html
  2. [root@ylt001 html]# rm -f index.html
  3. [root@ylt001 html]# vi index.html

3. 深入了解 Nginx

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 基本访问状态信息等模块
  1. [root@ylt001 ~]# yum install tree -y
  2. [root@ylt001 ~]# tree /application/nginx/

4. Nginx 虚拟主机配置

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

4.1.1 虚拟主机概念

4.1.2 虚拟主机类型

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

配置基于域名的 nginx.conf 内容

  1. [root@ylt001 ~]# cd /application/nginx/conf
  2. [root@ylt001 conf]# diff nginx.conf.default nginx.conf
  3. // 过滤包含 # 号和空行
  4. [root@ylt001 conf]# egrep -v "#|^$" nginx.conf.default >nginx.conf
  1. worker_processes 1;
  2. events {
  3. worker_connections 1024;
  4. }
  5. http {
  6. include mime.types;
  7. default_type application/octet-stream;
  8. sendfile on;
  9. keepalived_timeout 65;
  10. Server {
  11. listen 80;
  12. server_name www.yangyangyang.org;
  13. location / {
  14. root html/www;
  15. index.html index.htm;
  16. }
  17. }
  18. }

创建域名对应的站点目录及文件

  1. [root@ylt001 conf]# mkdir ../html/www -p
  2. [root@ylt001 conf]# echo "http://www.yangyangyang.org" >../html/www/index.html
  3. [root@ylt001 conf]# cat ../html/www/index.html

检查语法并重新加载 Nginx

  1. [root@ylt001 conf]# ../sbin/nginx -t
  2. [root@ylt001 conf]# ../sbin/nginx -s reload

检查 Nginx 重新加载后的情况

  1. [root@ylt001 conf]# lsof -i :80

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

  1. [root@ylt001 conf]# ps -ef|grep nginx

测试域名站点配置的访问结果

  1. [root@ylt001 conf]# echo "192.168.2.129 www.yangyangyang.org" >>/etc/hosts
  2. [root@ylt001 conf]# tail -1 /etc/hosts
  3. [root@ylt001 conf]# curl www.yangyangyang.org

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

增加新域名对应的配置

  1. worker_processes 1;
  2. events {
  3. worker_connections 1024;
  4. }
  5. http {
  6. include mime.types;
  7. default_type application/octet-stream;
  8. sendfile on;
  9. keepalived_timeout 65;
  10. Server {
  11. listen 80;
  12. server_name www.yangyangyang.org;
  13. location / {
  14. root html/www;
  15. index.html index.htm;
  16. }
  17. }
  18. Server {
  19. listen 80;
  20. server_name bbs.yangyangyang.org;
  21. location / {
  22. root html/bbs;
  23. index.html index.htm;
  24. }
  25. }
  26. Server {
  27. listen 80;
  28. server_name blog.yangyangyang.org;
  29. location / {
  30. root html/blog;
  31. index.html index.htm;
  32. }
  33. }
  34. }

创建新虚拟主机站点对应的目录和文件

  1. [root@ylt001 conf]# .mkdir -p ../html/bbs ../html/blog
  2. [root@ylt001 conf]# echo "http://bbs.yangyangyang.org" >../html/bbs/index.html
  3. [root@ylt001 conf]# echo "http://blog.yangyangyang.org" >../html/blog/index.html
  4. [root@ylt001 conf]# cat ../html/bbs/index.html
  5. [root@ylt001 conf]# cat ../html/blog/index.html
  6. [root@ylt001 conf]# LANG=en
  7. [root@ylt001 conf]# tree ../html

重新加载 Nginx 配置

  1. [root@ylt001 conf]# ../sbin/nginx -t
  2. [root@ylt001 conf]# ../sbin/nginx -s reload

在客户端测试

  1. [root@ylt001 conf]# tail -1 /etc/hosts
  2. [root@ylt001 conf]# curl www.yangyangyang.org
  3. [root@ylt001 conf]# curl blog.yangyangyang.org
  4. [root@ylt001 conf]# curl bbs.yangyangyang.org

备份当前配置

  1. [root@ylt001 conf]# /bin/cp nginx.conf nginx.conf_BaseName

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

配置虚拟主机监听的端口

  1. worker_processes 1;
  2. events {
  3. worker_connections 1024;
  4. }
  5. http {
  6. include mime.types;
  7. default_type application/octet-stream;
  8. sendfile on;
  9. keepalived_timeout 65;
  10. Server {
  11. listen 80;
  12. server_name www.yangyangyang.org;
  13. location / {
  14. root html/www;
  15. index.html index.htm;
  16. }
  17. }
  18. Server {
  19. listen 81;
  20. server_name bbs.yangyangyang.org;
  21. location / {
  22. root html/bbs;
  23. index.html index.htm;
  24. }
  25. }
  26. Server {
  27. listen 82;
  28. server_name blog.yangyangyang.org;
  29. location / {
  30. root html/blog;
  31. index.html index.htm;
  32. }
  33. }
  34. }

检查语法并重新加载 Nginx

  1. [root@ylt001 conf]# ../sbin/nginx -t
  2. [root@ylt001 conf]# ../sbin/nginx -s reload

检查 Nginx 重新加载后的情况

  1. [root@ylt001 conf]# lsof -i :80

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

  1. [root@ylt001 conf]# ps -ef|grep nginx

测试域名站点配置的访问结果

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

  1. http://www.yangyangyang.org:80
  2. http://bbs.yangyangyang.org:81
  3. http://blog.yangyangyang.org:82
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注