静态web服务使用---Nginx

      Nginx("engine x")是一款是由俄罗斯的程序设计师Igor Sysoev所开发高性能的 Web和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。
      在高连接并发的情况下,Nginx是Apache服务器不错的替代品。

一、查看软件目录结构

  1. 日志文件切割

    a. 利用专用文件切割

[root@web02 ~]# cat /etc/logrotate.conf
# rotate log files weekly
weekly                     --- 定义默认日志切割的周期

# keep 4 weeks worth of backlogs
rotate 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   --- 加载包含/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,小于1M不进行切割              
    rotate 1
}

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

    b. 利用脚本实现切割

[root@web02 ~]# vim /etc/access_log.sh
#!/bin/bash
mv /var/log/nginx/access.log  /var/log/nginx/access_$(date +%F).log
[root@web02 ~]# systemctl restart nginx

  2. 服务配置文件

[root@web02 ~]# cat /etc/nginx/nginx.conf

第一个部分: 配置文件主区域配置
user  www;               	 --- 定义worker进程管理的用户 
worker_processes  2;        ---定义有几个worker进程  == CPU核数 / 核数的2倍
error_log  /var/log/nginx/error.log warn;   --- 定义错误日志路径信息
pid        /var/run/nginx.pid;              --- 定义pid文件路径信息

第二个部分: 配置文件事件区域
events {                    
    worker_connections  1024;   --- 一个worker进程可以同时接收1024访问请求
}

第三个部分: 配置http区域
http {
    include       /etc/nginx/mime.types;      --- 加载一个配置文件
    default_type  application/octet-stream;   --- 指定默认识别文件类型
    log_format  oldboy  '$remote_addr - $remote_user [$time_local] "$request" '
                        '$status $body_bytes_sent "$http_referer" '
                        '"$http_user_agent" "$http_x_forwarded_for"';
	                  --- 定义日志的格式		
    access_log  /var/log/nginx/access.log  oldboy;
	                  --- 指定日志路径          
    sendfile        on;   ---用来提高服务器性能
    #tcp_nopush     on;   
    keepalive_timeout  65;   --- 超时时间
    #gzip  on;     ---是否需要开启压缩传输
    include /etc/nginx/conf.d/*.conf;        --- 加载一个配置文件
}


/etc/nginx/nginx.d/default  --- 扩展配置(虚拟主机配置文件)
第四个部分: server区域信息(配置一个网站 www/bbs/blog -- 一个虚拟主机)
server {
    listen       8080;                --- 指定监听的端口
    server_name  www.oldboy.com;      --- 指定网站域名                     
    root   /usr/share/nginx/html;     --- 定义站点目录的位置
    index  index.html index.htm;      --- 定义首页文件
    error_page   500 502 503 504  /50x.html;   --- 优雅显示页面信息
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

 二、Nginx服务应用

  1. 搭建一个网站

    a.编写虚拟主机配置文件

[root@web02 ~]# cd /etc/nginx/conf.d/
[root@web02 conf.d]# vim www.conf

server {
   listen        80;
   server_name   www.southbay.com;
   location  / {
     root  /html/www;
     index index.html;
   }
}

    b.  获取开发人员编写的网站代码

[root@web02 ~]# cd /html/www
[root@web02 www]# vim index.html

<html>
  <meta charset="utf-8">
  <head>
    <title>by southbay</title>
  </head>
  <body>
    southbay study file
    <table border=1>
      <tr> <td>01</td> <td>123456</td> </tr>
      <tr> <td>02</td> <td>234567</td> </tr>
      <tr> <td>03</td> <td>345678</td> </tr>
    </table>
    <a href="http://blog.southbay.com">
      <img src="southbay.jpg" />
    </a>
   </body>
</html>

    c.重启nginx服务(平滑重启)

[root@web02 ~]# systemctl reload nginx 
[root@web02 ~]# nginx -s reload

  2. 一台主机搭建多个网站

[root@web02 conf.d]# vim bbs.conf
server {
   listen        80;
   server_name   bbs.southbay.com;
   location  / {
     root  /html/bbs;
     index index.html;
   }
}
[root@web02 conf.d]# vim blog.conf
server {
   listen        80;
   server_name   blog.southbay.com;
   location  / {
     root  /html/blog;
     index index.html;
   }
}
[root@web02 conf.d]# vim www.conf
server {
   listen        80;
   server_name   www.southbay.com;
   location  / {
     root  /html/www;
     index index.html;
   }
}

  3. 虚拟主机访问方式

# 基于域名的方式进行访问
server {
    listen        80;
    server_name   www.southbay.com;
    location  / {
      root /html/www;
      index index.html;
    }
}
# 基于地址的方式进行访问
server {
    listen        10.0.0.7:80;
    server_name   www.southbay.com;
    location  / {
      root /html/www;
      index index.html;
    }
}
# 基于端口的方式进行访问
server {
   listen        8080;
   server_name   www.southbay.com;
   location  / {
     root  /html/www;
     index index.html;
   }
}

  4. 配置文件别名功能

server {
    listen        80;
    server_name   www.southbay.com southbay.com;
    location  / {
      root /html/www;
      index index.html;
    }
}

三、Nginx常用模块

  1. 安全访问配置(ngx_http_access_module)

    Syntax:deny address | CIDR | unix: | all;
    Default: —
    Context:http, server, location, limit_except

[root@web01 conf.d]# vim www.conf 
server {
   listen        80;
   server_name   www.southbay.com;
   location / {
     root  /html/www;
     index index.html;
   }
   location /southbay {
     deny  10.0.0.0/24;
     allow 172.16.1.0/24;
     root  /html/www;
     index index.html;
   }
}

  2. 用户访问认证配置(ngx_http_auth_basic_module)

    Syntax:auth_basic string | off;
    Default:auth_basic off;
    Context:httpserverlocationlimit_except

server {
   listen        80;
   server_name   www.southbay.com;
   location / {
     root  /html/www;
     index index.html;
     auth_basic      "southbay";
     auth_basic_user_file password/htpasswd;
}	

   3. 网站文件共享服务器(ngx_http_autoindex_module)

    Syntax:autoindex on | off;
    Default:autoindex off;
    Context:http, server, location

server {
   listen        80;
   server_name   www.southbay.com;
   location / {
     root  /html/www;
     autoindex on;    --- 开启nginx站点目录索引功能
}

    说明:1. 需要将首页文件进行删除
               2. mime.types媒体资源类型文件作用
                  文件中有的扩展名信息资源,   进行访问时会直接看到数据信息
                  文件中没有的扩展名信息资源, 进行访问时会直接下载资源

  4. 网站状态监控(ngx_http_stub_status_module)

    Syntax:stub_status;

    Default:—

    Context:serverlocation

server {
   listen    80;
   server_name  state.southbay.com
   stub_status;
}

  5. 日志功能配置

    a. 访问日志(ngx_http_log_module)

      Syntax:access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];
        access_log off;
      Default:access_log logs/access.log combined;
      Context:httpserverlocationif in locationlimit_except

[root@web02 ~]# cat /etc/nginx/nginx.conf 

# 定义日志内容格式
log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';
# 调用日志格式
access_log  /var/log/nginx/access.log  main;
访问日志参数说明
常用参数参数说明
$remote_addr显示用户访问源IP地址信息
$remote_user显示认证的用户名信息
$time_local显示访问网站时间
$request请求报文的请求行信息
$status用户访问网站状态码信息
$body_bytes_sent显示响应的数据尺寸信息
$http_referer记录调用网站资源的连接地址信息(防止用户盗链)
$http_user_agent记录用户使用什么客户端软件进行访问页面的  (谷歌 火狐 IE 安卓 iphone)
$http_x_forwarded_for表示 HTTP 请求端真实 IP

    b. 错误日志(Core functionality)

    Syntax:error_log file [level];
    Default:error_log logs/error.log error;
    Context:mainhttpmailstreamserverlocation

[root@web02 ~]# cat /etc/nginx/nginx.conf
# 错误日志配置
error_log  /var/log/nginx/error.log warn;
错误日志级别
错误级别错误级别说明
debug调试级别, 服务运行的状态信息和错误信息详细显示
info信息级别, 只显示重要的运行信息和错误信息
notice通知级别: 更加重要的信息进行通知说明
warn 警告级别: 可能出现了一些错误信息,但不影响服务运行
error错误级别: 服务运行已经出现了错误,需要进行纠正
crit严重级别: 必须进行修改调整
alert严重警告级别: 即警告,而且必须进行错误修改
emerg灾难级别: 服务已经不能正常运行 

  6. location说明(ngx_http_core_module)

    Syntax:location [ = | ~ | ~* | ^~ ] uri { ... }
       location @name { ... }
    Default:—
    Context:serverlocation

# 错误页面优雅显示
# 第一种方式:
location / {
   root /html/www;
   error_page  404  /error.jpg;
}

# 第二种方式:
error_page 500 501 502 503 504 /50x.html;
location =/50x.html {
   root html;
}

# 第三种方式:
error_page 500 501 502 503 504 http://www.southbay.com/error.jpg;
# Location 符号说明
location = / {              --- 精确匹配    优先级01
    [ configuration A ]
}

location / {                --- 默认匹配    优先级04
    [ configuration B ]
}

location /documents/ {      --- 按照目录进行匹配    优先级03
    [ configuration C ]
}

location ^~ /images/ {      --- 优先匹配/不识别uri信息中符号信息   优先级02
    [ configuration D ]
}
 
location ~* \.(gif|jpg|jpeg)$ {  --- 不区分大小写进行匹配  优先级03
    [ configuration E ]
}

  7. 页面跳转功能(http_rewrite_module)

    Syntax:rewrite regex replacement [flag];  
    Default:—
    Context:server, location, if

#第一种方式
server {
   server_name  southbay.com;
   rewrite ^/(.*) http://www.southbay.com/$1 permanent;
}
#第二种方式
if ($host ~* "^southbay.com$") {
   rewrite ^/(.*) http://www.southbay.com/$1 permanent;
}
标记说明
标记说明
last本条规则匹配完成后,继续向下匹配新的Loation URI规则
break本条规则匹配完成即终止,不在匹配后面任何规则
redirect返回302临时重定向
permanent返回301永久重定向

四、Nginx常见错误

错误代码处理方式
4031. 不要禁止访问
2. 因为没有首页文件
404

1. 修改nginx配置文件---location

2. 在站点目录中创建相应目录或文件数据信息

413

修改nginx配置文件   client_max_body_size 50m;   --- 指定用户上传数据的大小限制(默认1M)

修改php.ini配置文件  upload_max_filesize = 50M     --- 使PHP接收用户上传的更大的数据(默认2M)

中文出现乱码网站配置文件加入 charset utf-8;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值