综合架构之网站服务-nginx

1.logrotate程序进行日志切割原理说明

  1. nginx服务扩展配置文件 server区块
  2. nginx服务如何搭建网站
  3. nginx服务多个虚拟主机 多个server配置信息
  4. nginx服务三种访问方式 基于域名 基于端口(访问原理) 基于地址(监听地址)
  5. nginx服务实现共享存储 autoindex --- 显示站点目录结构
  6. nginx服务安全访问控制 基于访问源地址(deny/allow) 基于认证访问(ngx_http_auth_basic_module)

logrotate程序进行日志切割原理

cat /etc/logrotate.conf
# see "man logrotate" for details
# rotate log files weekly
weekly #默认按周进行切割日志

# keep 4 weeks worth of backlogs
rotate 4 #保留最新4周的数据

# create new (empty) log files after rotating old ones
create #切割日志之后创建新的日志文件

# use date as a suffix of the rotated file
dateext #生成的日志带时间信息。

# uncomment this if you want your log files compressed
#compress #切割的日志是否压缩

# RPM packages drop log rotation information into this directory
include /etc/logrotate.d #调用一个目录下的日志文件

# no packages own wtmp and btmp -- we'll rotate them here
/var/log/wtmp { #具体得哪个文件进行切割(局部配置)
    monthly
    create 0664 root utmp 切割完日志的权限
    minsize 1M        最小尺寸,如果不超过1M就不进行切割
    rotate 1              保存一份
}

/var/log/btmp {
    missingok
    monthly
    create 0600 root utmp
    rotate 1
}

# system-specific logs may be also be configured here.

加载目录下的日志文件

[root@web01 logrotate.d]# ll 
total 20
-rw-r--r--. 1 root root  91 Apr 11  2018 bootlog
-rw-r--r--  1 root root 351 Apr 23 22:34 nginx
-rw-r--r--. 1 root root 224 Oct 30  2018 syslog
-rw-r--r--. 1 root root 100 Oct 31  2018 wpa_supplicant
-rw-r--r--. 1 root root 103 Nov  5  2018 yum

[root@web01 logrotate.d]# cat syslog 
/var/log/cron
/var/log/maillog
/var/log/messages
/var/log/secure
/var/log/spooler
{
    missingok    #固定格式
    sharedscripts #固定格式
    postrotate #固定格式
    /bin/kill -HUP `cat /var/run/syslogd.pid 2#系统日志服务> /dev/null` 2#没有这个进程追加到空> /dev/null || true
    endscript #把日志做了切割日志,原有的日志还在调用,没有生效,所以重启新生成的日志才会被调用。

nginx服务扩展配置文件

mv /etc/nginx/conf.d/default.conf   /etc/nginx/conf.d/default.conf.bak #避免主配置文件加载默认配置文件,影响www配置文件。
/etc/nginx/conf.d/default.conf
egrep -v "#|^$" default.conf >www.conf #将#号空格重新追加到一个新的配置文件当中。
server {                               --- 可以配置网站信息  每个网站==server==每个虚拟主机
        listen       80;                   --- 网站服务监听端口
        server_name  www.oldboy.com;       --- 定义网站主机域名
        location / {  ???
            root   /html/www;              --- 指定站点目录(存放网站所有资源)
            index  oldboy.jpg index.htm;   --- 首页文件#注:访问www.oldboy的时候会默认访问第一个配置的首页文件,如果第一个丢失或删除会继续向下匹配。匹配的首页文件都删除或丢失的时候会报403的错误,首页文件不存在。
        }
        error_page   404 500 502 503 504  /oldboy.jpg;   --- 错误页面优雅显示#也可以配置百度的优雅界面,后面的都不要
        location = /oldboy,jpg { 匹配URI的信息
            root   /usr/share/nginx/html; #去哪找URI的信息
        }
    }

注:更新站点下的目录内容可直接加载。


在站点目录存放index.html就可以访问页面内容


启动nginx出现报错

[root@web01 conf.d]# systemctl restart nginx 
Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.

检查语法是否正确

[root@web01 conf.d]# nginx -t www.conf 
nginx: invalid option: "www.conf"

nginx配置文件规范:
01. 区域模块信息 必须有一对花括号
02. 指令信息后面必须有分号
03. 相应指令信息必须放置在正确区块中

nginx服务配置多个虚拟主机

  1. 多个虚拟主机配置
    第一个历程: 编写多个虚拟主机配置文件
    /etc/nginx/conf.d/
    [root@web01 conf.d]# cat *.conf
    server {
        listen       80;
        server_name  bbs.oldboy.com;
        location / {
            root   /html/bbs;
            index  index.html index.htm;
        }
        error_page  404 500 502 503 504  https://www.baidu.com/search/error.html;
    }
    server {
        listen       80;
        server_name  blog.oldboy.com;
        location / {
            root   /html/blog;
            index  index.html index.htm;
        }
        error_page  404 500 502 503 504  https://www.baidu.com/search/error.html;
    }
    server {
        listen       80;
        server_name  www.oldboy.com;
        location / {
            root   /html/www;
            index  index.html index.htm;
        }
        error_page  404 500 502 503 504  https://www.baidu.com/search/error.html;
    }

第二个历程: 创建站点目录和首页文件

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

第三个历程: 重启nginx服务

    nginx配置文件规范:
    01. 区域模块信息 必须有一对花括号
    02. 指令信息后面必须有分号
    03. 相应指令信息必须放置在正确区块中
   
    nginx -t    --- 检查配置文件语法命令
    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /etc/nginx/nginx.conf test is successful

    systemctl restart/reload nginx

第四个历程: 进行网站页面访问测试
配置DNS解析信息

10.0.0.7 www.oldboy.com bbs.oldboy.com blog.oldboy.com

网站服务访问方式

  • a 基于域名信息访问
  • b 基于端口信息访问
    修改server虚拟主机配置文件
server {
          listen       8080;#修改了端口
          server_name  www.oldboy.com;
          location / {
              root   /html/www;
              index  index.html index.htm;
          }
          error_page  404 500 502 503 504  https://www.baidu.com/search/error.html;
      }

访问方式
www.oldboy.com:8080

如果输入www.oldboy.com则访问的是bbs.oldboy.com信息的内容

原理

用户访问网站的原理

传输层封装的时候不加8080的端口号会封装80端口去站点寻找首页文件的时候会根据字母排序的html进行访问。

  • c 基于地址信息访问
    准备工作: 主配置文件
    include /etc/nginx/conf.d/www.conf;
       listen       10.0.0.7:80;
        server_name  www.oldboy.com;
        location / {
            root   /html/www;
            index  index.html index.htm;
        }
       PS: nginx配置文件中只要涉及到IP地址修改,必须重启nginx服务,不能平滑重启。

利用nginx服务实现FTP存储服务器

第一个历程: 编写配置文件  www.conf 
    server {
         listen            80;
         server_name  www.oldboy.com;
         location / {
             root   /html/www;
             index  index.html index.htm;
             autoindex  on;    --- 开启网站站点目录信息显示功能
             charset utf-8;    --- 设置中文字符集信息,避免页面中文出现乱码
         }
         error_page  404 500 502 503 504  https://www.baidu.com/search/error.html;
     }
    
    systemctl restart nginx
    第二个历程: 创建站点目录, 将指定的首页文件删除
    rm index.html -f
    第三个历程: 修改媒体资源类型文件
    sed -r '/jpg\;$|gif\;$|txt\;$/s@(.*)@#\1@g' /etc/nginx/mime.types
    systemctl restart nginx

mine.type识别的文件类型注释或删除就可以下载了。

对网站页面信息进行访问控制

allow/deny: 
location: location /VIP专区/ 

server {
    listen            80;
    server_name  www.oldboy.com;
    location / {
        root   /html/www;
        index  index.html index.htm;
        autoindex  on;
        charset utf-8;
    }
    location /VIP专区/ {     #--- 匹配uri操作,禁止访问URI信息
        root   /html/www;
        deny   10.0.0.1;     --- 进行访问控制
        allow  all;
    }
    error_page 403 404 500 502 503 504  /error.html;
    location = /error.html {
        root /html/www;
    }
}

根据用户登录密码进行控制

auth_basic --- 开启登录认证功能
auth_basic_user_file --- 指定加载的密码文件
http://nginx.org/en/docs/http/ngx_http_auth_basic_module.html

第一个历程: 修改配置文件

   root@web01 VIP专区]# cat /etc/nginx/conf.d/www.conf 
    server {
        listen            80;
        server_name  www.oldboy.com;
        location / {
            root   /html/www;
            index  index.html index.htm;
            autoindex  on;
            charset utf-8;
        }
        location /VIP专区/ { 
            root   /html/www;
            autoindex on;
            charset utf-8;
            auth_basic  oldboy62;
            auth_basic_user_file  /etc/password.txt; #可以相对路径也可以使用绝对路径
        }
        error_page 403 404 500 502 503 504  /error.html;
        location = /error.html {   #每个location是局部配置
            root /html/www;
        }
    }

第二个历程: 生成密码文件

    yum install -y httpd-tools
    htpasswd -bc /etc/password.txt oldgirl oldboy123   #---第一次创建
    htpasswd -b  /etc/password.txt oldboy oldboy123    #---添加新的用户
    -b  免交互输入密码
    -c  创建密码文件,但是是交互的形式

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 229,460评论 6 538
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 99,067评论 3 423
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 177,467评论 0 382
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 63,468评论 1 316
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 72,184评论 6 410
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 55,582评论 1 325
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 43,616评论 3 444
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 42,794评论 0 289
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 49,343评论 1 335
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 41,096评论 3 356
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 43,291评论 1 371
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 38,863评论 5 362
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 44,513评论 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 34,941评论 0 28
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 36,190评论 1 291
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 52,026评论 3 396
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 48,253评论 2 375