zoukankan      html  css  js  c++  java
  • Nginx:综合架构网站服务 -- 2 -- nginx网站搭建配置及访问

    综合架构网站服务

    命令总览:

    [root@web01 html]# nginx -h 		-- 查看nginx帮助选项
    [root@web01 7]# nginx -V    		-- 查看已配置信息,是编译安装是./config配置的内容
    [root@web01 html]# nginx -t 		-- 对配置语法进行检查
    
    # nginx -T 能查看nginx的所有配置信息
    [root@web01 html]# nginx -T 		-- 对配置语法进行检查,并且将所有配置信息显示到屏幕上
    [root@web01 html]# nginx -s reload 	-- -s参数指定需要执行的动作stop, quit, reopen, reload
    									--(建议和systemctl reload nginx只用一种方式,不要混用)
    [root@web01 password]# htpasswd -bc htpasswd oldboy 123456 -- 创建访问认证密码文件
    [root@web01 password]# curl www.oldboy.com -u oldboy   ## 指定用户,输入密码
    [root@web01 password]# curl www.oldboy.com -u oldboy:123456 ## 指定用户及密码
    

    00. 内容说明:

      1. nginx的安装部署回顾
      2. nginx服务的企业应用 (模块功能)
      3. nginx在企业中虚拟主机的访问方式、原理及配置
      4. nginx部分总结
    

    01. 上文回顾:

    链接:https://www.cnblogs.com/moox/p/12689800.html

    1. nginx服务一些特点介绍
      a 支持高并发能力比较强 消耗资源少,如淘宝在Nginx基础上开发了tengine
      b 软件功能比较多样 (支持web,负载均衡,缓存服务)
      c 支持系统平台比较广泛
      可以制作yum仓库
      1. 收集仓库中的软件 --- yum.conf
      2. 客户端访问到制作yum仓库 --- nginx (可以部署在linux windows)
    2. nginx软件部署安装
      a yum安装方式
      b 编译安装软件
    3. nginx目录结构
    4. nginx配置文件参数说明

    02. nginx服务的企业应用

    1. 利用nginx服务搭建一个网站(www)

    1. 编写虚拟主机配置文件/etc/nginx/conf.d/*.conf
    2. 需要获取开发人员编写的网站代码Index.html
    3. 重启nginx服务(平滑重启) systemctl reload nginx 或 nginx -s reload
    4. 编写DNS配置信息-- 模拟域名解析windows../hosts
    5. 进行测试访问

    1.1 : 编写虚拟主机配置文件

    cd /etc/nginx/conf.d/
    vim www.conf
    server {
        listen        80;  				# 监听的端口
        server_name   www.oldboy.com;	 # 主机的域名 
        location  /oldboy {				# 匹配的区域
        root  /usr/share/nginx/html;	# 站点目录
        index oldboy.html;				# 指定首页文件
        }
    }
    

    1.2 : 获取开发人员网站代码

    上传一张图片moox.jpg到站点目录下/usr/share/nginx/html

    <html>
      <meta charset="utf-8">
      <head>
        <title>Nginx head part</title>
      </head>
      <body>
       Nginx 网站服务搭建 body部分
        <table border=1>
          <tr> <td>01</td> <td>oldboy</td> </tr>
          <tr> <td>02</td> <td>oldgirl</td> </tr>
          <tr> <td>03</td> <td>olddog</td> </tr>
        </table>
        <a href="http://cnblos.com/moox">
          <img src="moox.jpg" />
        </a>
       </body>
    </html>
    

    1.3 : 重启nginx服务
    (平滑重启) 两种方法: -- 选其中一种,不要混用

    systemctl reload nginx (yum安装直接平滑重启即可)
    nginx -s reload (编译安装无nginx服务,-s指定执行动作)

    nginx命令参数(nginx -h)

    Options:
      -?,-h         : this help
      -v            : show version and exit
      -V            : show version and configure options then exit
      -t            : test configuration and exit 				
      -- nginx -t 检查配置语法
      -T            : test configuration, dump it and exit		
      -- nginx -T -- 对配置语法进行检查,并且将所有配置信息显示到屏幕上
      -q            : suppress non-error messages during configuration testing
      -s signal     : send signal to a master process: stop, quit, reopen, reload  
      -- nginx -s reload 指定执行动作
      -p prefix     : set prefix path (default: /etc/nginx/)
      -c filename   : set configuration file (default: /etc/nginx/nginx.conf)
      -g directives : set global directives out of configuration file
    

    1.4 : 配置DNS域名解析

    真实域名: 在阿里云上进行DNS解析记录配置
    模拟域名: 在windows主机的hosts文件中进行配置即可
    C:WindowsSystem32driversetchosts
    改动频率比较大,可以将hosts快捷方式到桌面

    10.0.0.7 www.moox.com 
    

    1.5 : 进行测试访问

    浏览器中: 
    http://www.moox.com/    -- 请求的是/ ,默认进入 default.conf中location / 指定的首页
    http://www.moox.com/moox.html    -- 进入 conf.d/www.conf 中location /moox 指定的首页moox.html
    
    linux:
    mkdir /usr/share/nginx/html/moox -p
    mv /usr/share/nginx/html/moox.* /usr/share/nginx/html/moox/
    systemctl reload nginx
    
    清除缓存重新访问测试:
    http://www.moox.com/moox -- 进入 conf.d/www.conf 中location /moox 指定的首页moox.html
    

    2. 部署搭建网站常见错误

    1. 网站服务配置文件编写不正确

      • 404 错误
        • 解决方法一: 修改nginx配置文件---location
        • 解决方法二: 在站点目录中创建相应目录或文件数据信息
      • 403 错误
        • 解决方法一: 不要禁止访问
        • 解决方法二: 因为没有首页文件
      • 500 Internal Server Error
        • 1.内部程序代码编写有问题
        • 2.程序服务中文件权限不正确
    2. DNS信息配置不正确

    3. nginx配置文件修改, 一定要重启服务;
      站点目录中代码文件信息调整,不需要重启服务

    3. 利用nginx服务搭建一个多网站(www bbs blog)

    3.1 创建多个虚拟主机配置文件
    3.2 创建站点目录和目录中首页文件
    3.3 编写hosts解析文件(Windows)
    3.4 进行访问测试
    3.5. 企业中虚拟主机访问方式

    3.1 创建多个虚拟主机配置文件

    ## /etc/nginx/conf.d/bbs.conf
    server {
       listen        80;
       server_name   bbs.oldboy.com;
       location  / {
         root  /html/bbs;
         index index.html;
       }
    }
    ## /etc/nginx/conf.d/blog.conf
    server {
       listen        80;
       server_name   blog.oldboy.com;
       location  / {
         root  /html/blog;
         index index.html;
       }
    }
    ## /etc/nginx/conf.d/www.conf
    server {
       listen        80;
       server_name   www.oldboy.com;
       location  / {
         root  /html/www;
         index index.html;
       }
    }
    [root@web01 nginx]# systemctl reload nginx
    

    3.2 创建站点目录和目录中首页文件

    [root@web01 conf.d]# mkdir /html/{www,bbs,blog} -p
    [root@web01 conf.d]# for name in {www,bbs,blog};do echo "10.0.0.7 $name.moox.com" >/html/$name/index.html  ;done
    [root@web01 conf.d]# for name in {www,bbs,blog};do cat /html/$name/index.html  ;done
    10.0.0.7 www.moox.com
    10.0.0.7 bbs.moox.com
    10.0.0.7 blog.moox.com
    

    3.3 编写hosts解析文件(Windows)
    10.0.0.7 www.oldboy.com bbs.oldboy.com blog.oldboy.com

    3.4 进行访问测试

    • 利用windows进行浏览器访问测试,需要清除缓存。Google浏览器可使用无痕模式,不用每次都清缓存
    • 利用linux进行命令访问测试
    cat /etc/hosts
    172.16.1.7      web01 www.moox.com bbs.moox.com blog.moox.com 
    [root@web01 conf.d]# curl www.oldboy.com
    10.0.0.7 www.oldboy.com
    [root@web01 conf.d]# curl bbs.oldboy.com
    10.0.0.7 bbs.oldboy.com
    [root@web01 conf.d]# curl blog.oldboy.com
    10.0.0.7 blog.oldboy.com
    
    • 有多个网站时,使用ip访问可以指定最先的访问次序
    cat /etc/nginx/nginx.conf
    ## ...
    include /etc/nginx/conf.d/blog.conf; --在包含所有*.conf的前面指定访问顺序
    include /etc/nginx/conf.d/*.conf;
    

    03. 企业中虚拟主机的访问

    1 企业中虚拟主机的访问方式
    2 企业中网站的页面访问原理
    3 企业中网站的安全访问配置

    1. 企业中虚拟主机的访问方式

    • a 基于域名的方式进行访问:

    • b 基于地址的方式进行访问: (只能用指定地址访问) --- 负载均衡+高可用服务

      server {
      listen        10.0.0.7:80;  ## 指定IP及端口,重启nginx服务
      server_name   www.oldboy.com;
      location  / {
      	root  /html/www;
      	index index.html;
      	}	
      }
      ## PS: 服务配置文件中涉及到地址修改,必须重启nginx服务,不能平滑重启**
      
    • c 基于端口的方式进行访问:

      ## zabbix服务(apache:80)  + web服务(nginx:80) --> 同一台主机
      server {
      listen        8080;
      server_name   www.oldboy.com;
      location  / {
      	root  /html/www;
      	index index.html;
      	}
      }
      

    2. 企业中网站的页面访问原理

    1. 将域名进行解析 www.oldboy.com --- 10.0.0.7
    2. 建立TCP的连接(四层协议)
      10.0.0.7 目标端口 8080 -- 但域名访问,tcp端口默认为80
    3. 根据应用层HTTP协议发出请求
      请求报文: hosts: www.oldboy.com
      -- 找到www.oldboy.com ,但端口不是80,放弃;转而找满足8080端口的
    4. 没有相同域名的server主机,会找满足端口要求的第一个主机
      显示主机的网站页面 bbs.oldboy.com --显然不是要找的www.oldboy.com

    3. 企业中网站的安全访问配置

    a 根据用户访问的地址进行控制

    10.0.0.0/24 www.oldboy.com/AV/ 不能访问 deny
    172.16.1.0/24 www.oldboy.com/AV/ 可以访问 allow

    • nginx访问模块: ngx_http_access_module

      • 举例配置:
      location / {
        deny  192.168.1.1;
        allow 192.168.1.0/24;
        allow 10.1.1.0/16;
        allow 2001:0db8::/32;
        deny  all;
      }
      
      • 指令用法
      Syntax:	deny address | CIDR | unix: | all;
      Default:	—
      Context:	http, server, location, limit_except    --- 能够使用deny的区域
      
      • 第一个历程: 编写配置文件
          [root@web01 conf.d]# cat www.conf 
          server{
              listen	80;
              server_name www.moox.com;
              location / {
                  root  /html/www;
                  index index.html index.htm;
              }
              location /AV {
                  deny 10.0.0.0/24;
                  allow 172.16.1.0/24;
              }
          }      
      - 补充: 
          location外面的信息, 全局配置信息
          location里面的信息, 局部配置信息
      
    b 根据用户访问进行认证

    访问时有弹出框,如输入密码认证等
    nginx认证模块: ngx_http_auth_basic_module
    编写虚拟主机配置文件 /etc/nginix/conf.d/www.conf

    • nginx认证模块: ngx_http_auth_basic_module

      • 举例配置:
      location / {
             auth_basic           "closed site";    --- 开启认证功能,引号中的内容是提示信息,但需要有auth_basic
             auth_basic_user_file conf/htpasswd;    --- 加载用户密码文件,密码文件可以指定
      }
      
      • 第一个历程: 编写虚拟主机配置文件
      server {
      	listen        80;
      	server_name   www.oldboy.com;
      	location / {
               root  /html/www;
               index index.html;
               auth_basic      "oldboy-sz-01";
               auth_basic_user_file password/htpasswd;  --- 相对于/etc/nginx目录的相对位置
      	}
      }
      
      • 第二个历程: 创建密码文件(文件中密码信息必须是密文的)

      htpasswd 创建一个有密文信息的密码文件
      htpasswd命令参数说明见文末04.知识总结部分(5)

      mkdir /etc/nginx/password/ -p
      htpasswd -bc ./password/htpasswd oldboy  123456  -- 免交互式创建加密密码文件
      # 命令  -bc:免交互创建   指定文件名	   用户名    密码
      # 此时浏览器访问需要输入用户名和密码才能访问
      
      • 修改密码文件权限:
      [root@web01 conf.d]# ll ./htpasswd 
      -rw-r--r-- 1 root root 45 Apr 18 16:27 ../password/htpasswd
      [root@web01 conf.d]# chmod 600 ./htpasswd 
      [root@web01 password]# ll
      -rw------- 1 root root 45 Apr 18 16:27 htpasswd  
      
      ## 此时使用的文件权限为root,访问时报500 Internal Server Error
      ## 一般原因: 500 Internal Server Error
      1. 内部程序代码编写有问题
      2. 程序服务中文件权限不正确
      
      ## 需要将密码文件权限修改为nginx的worker管理的用户www
      [root@web01 password]# ps -ef|grep nginx
      root       1570      1  0 14:43 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
      www        2260   1570  0 16:34 ?        00:00:00 nginx: worker process
      root       2299   1576  0 16:52 pts/0    00:00:00 grep --color=auto nginx
      [root@web01 password]# chown www.www htpasswd 
      [root@web01 password]# ll
      -rw------- 1 www www 45 Apr 18 16:27 htpasswd
      
    c curl命令参数:
      参数 -u, --user USER[:PASSWORD]  Server user and password
    [root@web01 password]# curl www.oldboy.com -u oldboy   ## 指定用户
    Enter host password for user 'oldboy':
    10.0.0.7 www.oldboy.com
    [root@web01 password]# curl www.oldboy.com -u oldboy:123456 ## 指定用户及密码
    10.0.0.7 www.oldboy.com
    

    04. 知识总结:

    1. 如何搭建一个网站(服务器 域名 网页代码 数据库支持)
    2. 如何搭建多个网站
    3. 如何访问网站(3种 域名 地址 端口)
    4. 网站服务安全配置
      根据用户访问域名地址进行控制
      根据用户访问信息进行认证控制

    附:htpasswd命令参数说明

    mkdir /etc/nginx/password/ -p
    htpasswd -c ./password/htpasswd oldboy    -- 交互式创建加密密码文件
    # 命令  -c:创建   指定文件名			用户名
    或
    htpasswd -bc ./password/htpasswd oldboy  123456  -- 免交互式创建加密密码文件
    # 命令  -bc:免交互创建   指定文件名	   用户名    密码
    或
    htpasswd -bpc      ./password/htpasswd oldboy  123456  -- 免交互式创建明文不加密密码文件
    # 命令  -bpc:免交互创建不加密   指定文件名	   用户名    密码
    
    htpasswd -D htpasswd oldboy   --- 删除指定用户oldboy密码文件htpasswd,参数-D -删除
    
    # htpasswd命令参数说明:
    -c  Create a new file.  *****
    	--创建一个密码文件
    -n  Don't update file; display results on stdout.
    	--不会更新文件; 显示文件内容信息
    -b  Use the password from the command line rather than prompting for it. *****
    	--免交互方式输入用户密码信息
    -i  Read password from stdin without verification (for script usage).
    	--读取密码采用标准输入方式,并不做检查 ???
    -m  Force MD5 encryption of the password (default).
    	--md5的加密算法
    -B  Force bcrypt encryption of the password (very secure).
    	--使用bcrypt对密码进行加密  
    -C  Set the computing time used for the bcrypt algorithm (higher is more secure but slower, default: 5, valid: 4 to 31).
    	--使用bcrypt algorithm对密码进行加密
    -d  Force CRYPT encryption of the password (8 chars max, insecure).
    	--密码加密方式
    -s  Force SHA encryption of the password (insecure).
    	--加密方式
    -p  Do not encrypt the password (plaintext, insecure).
    	--不进行加密
    -D  Delete the specified user.
    	--删除指定用户
    -v  Verify password for the specified user.
    
  • 相关阅读:
    新东西-intel edison
    MFC AfxMessageBox(_T("Please Load Rawdata First !"));
    libgl1-mesa-glx:i386 : 依赖: libglapi-mesa:i386
    开源硬件_瑞芯微开发板
    手机方案商
    嵌入式Linux应用开发__求职要求
    工作要求
    Proc文件系统接口调试
    Sysfs文件系统接口调试
    Ubuntu Linux 解决 bash ./ 没有那个文件或目录 的方法
  • 原文地址:https://www.cnblogs.com/moox/p/12726926.html
Copyright © 2011-2022 走看看