Nginx管理(一)

Nginx管理内容

一、Nginx介绍

  Nginx (engine x) 是一个高性能的HTTP和反向代理服务,也是一个IMAP/POP3/SMTP服务。

1、Nginx历史和特性

  Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,第一个公开版本0.1.0发布于2004年10月4日。
  其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011年6月1日,nginx 1.0.4发布。
  Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好。
  延伸版本:tengine(淘宝)、openresrt(章亦春)。

2、Nginx官方文档

Nginx官网
Nginx中文文档

二、Nginx源码安装

  代码或软件包必须从官网下载,第三方的软件包可能会有后门、安全漏洞等风险。

1、Nginx下载和环境准备

$ wget http://nginx.org/download/nginx-1.15.5.tar.gz -P /usr/src     # 下载nginx
$ cd /usr/src
$ tar xvf nginx-1.15.5.tar.gz
$ cd nginx-1.15.5
$ yum -y install gcc            # nginx是c写的
$ yum -y install pcre-devel   # url重写用到的包
$ yum -y install zlib  zlib-devel    # 解压缩用到的包

2、配置 configure

  configure常用参数有两个:--prefix=path;--help 配置参数

  1. 检查环境是否满足安装条件、依赖解决;
  2. 指定安装方式、配置文件、命令文件,各种文件放哪里、开启模块功能(内置模块、三方模块)
  3. 指定软件安装路径;
$ ./configure  --prefix=/usr/local/nginx
....

Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"                                           # nginx安装路径
  nginx binary file: "/usr/local/nginx/sbin/nginx"                            # nginx启动文件
  nginx modules path: "/usr/local/nginx/modules"                          # nginx所使用的模块
  nginx configuration prefix: "/usr/local/nginx/conf"                       # nginx配置文件目录
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"           # nginx配置文件
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"                            # pid文件,进程号
  nginx error log file: "/usr/local/nginx/logs/error.log"                     # nginx错误日志
  nginx http access log file: "/usr/local/nginx/logs/access.log"         # nginx访问日志(谁访问了网站)
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

3、编译和安装

# 编译源码生成可执行程序  依赖gcc
$ make  -j4
# 安装程序
$ make install

4、nginx启动

  一个服务有三要素:端口、监听地址、协议。nginx使用的是七层协议(http),使用端口:80,监听地址默认是0.0.0.0(所有机器)。
  在nginx启动前,需要先检查端口是否被占用:

$ lsof -i:80          # 第一种方法
# contos7需要安装netstat工具
$ yum install net-tools
$ netstat -ntpl     # 第二种方法
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1317/master         
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1095/sshd           
tcp6      0     0 ::1:25                      :::*                       LISTEN      1317/master         
tcp6      0     0 :::22                       :::*                       LISTEN      1095/sshd 

  查看发现没有人使用80端口,启动nginx程序:

$ /usr/local/nginx/sbin/nginx
$ lsof -i :80    # 查看服务是否启动
COMMAND   PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   11524   root    6u  IPv4  29204      0t0  TCP *:http (LISTEN)
nginx   11525 nobody    6u  IPv4  29204      0t0  TCP *:http (LISTEN)

5、nginx验证测试

(1)方法一:elinks文本浏览器查看

$ yum install elinks   # 除了elinks还有curl、lynx等文本浏览器
$ elinks  http://192.168.10.42 -dump

elinks浏览效果

方法二:chrome浏览器查看(略)

三、Nginx配置文件详解

  查看文件:/usr/local/nginx/conf/nginx.conf

1、nginx.conf文件逐行介绍


#启动子进程程序默认用户
#user  nobody;
#一个主进程和多个工作进程。工作进程是单进程的,且不需要特殊授权即可运行;这里定义的是工作进程数量
worker_processes  1;

#全局错误日志的位置及日志格式
#error_log是指令定义的是错误日志  后面是路径及日志级别
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#定义pid文件在哪,文件中的是主进程号,/usr/local/nginx/logs/nginx.pid
#pid        logs/nginx.pid;


events {
    #每个工作进程最大的并发数(一个工作进程可以有多少个线程)
    worker_connections  1024;
}


#http服务器设置
http {
    #设定mime类型,类型由mime.type文件定义
    include       mime.types;
    
    #
    default_type  application/octet-stream;

    #日志格式
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';
    #$remote_addr与$http_x_forwarded_for用以记录客户端的ip地址;
    #$remote_user:用来记录客户端用户名称;
    #$time_local: 用来记录访问时间与时区;
    #$request: 用来记录请求的url与http协议;
    #$status: 用来记录请求状态;成功是200,
    #$body_bytes_sent :记录发送给客户端文件主体内容大小;
    #$http_referer:用来记录从那个页面链接访问过来的;
    #$http_user_agent:记录客户浏览器的相关信息;

    #全局访问日志路径 
    #access_log  logs/access.log  main;   # 日志放置的位置
    #sendfile指令指定 nginx 是否调用sendfile 函数(zero copy 方式)来输出文件,对于普通应用,必须设为on。如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络IO处理速度,降低系统uptime。
    sendfile        on;
    
    #此选项允许或禁止使用socke的TCP_CORK的选项,此选项仅在使用sendfile的时候使用
    #tcp_nopush     on;

    #长连接超时时间
    #keepalive_timeout  0;
    keepalive_timeout  65;

    #开启压缩
    #gzip  on;

    #配置虚拟主机
    server {
        #虚拟主机使用的端口
        listen       80;
        #虚拟主机域名
        server_name  localhost;

        #虚拟主机支持的字符集
        #charset koi8-r;

        #虚拟主机的访问日志路径
        #access_log  logs/host.access.log  main;

        #定义web根路径
        location / {
            #根目录路径
            root   html;
            #索引页
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #

        #根据错误码 返回对应的页面
        error_page   500 502 503 504  /50x.html;

        #定义页面路径
        location = /50x.html {
            root   html;
        }

        #定义反向代理服务器 数据服务器是lamp模型(apache发布)
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}


        #定义PHP为本机服务的模型(通过nginx发布)  
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #拒绝apache DR目录及子目录下的.htaccess文件访问
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    #https的配置方案
    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
}

2、配置文件要点总结

(1)#user nobody;

  这一行指定了启动子进程程序的默认用户。

$ lsof -i :80    # 查看服务是否启动
COMMAND   PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   11524   root    6u  IPv4  29204      0t0  TCP *:http (LISTEN)
nginx   11525 nobody    6u  IPv4  29204      0t0  TCP *:http (LISTEN)

  父进程是由root启动的,子进程则是用nobody启动的。

(2)worker_processes 1;

  一个主进程和多个工程进程(子进程)。工作进程是单进程的,且不需要特殊授权即可运行。
  这里定义的是工作进程数量。一般情况建议有几个核就写几个。

# 安装kellall命令
$ yum search killall
$ yum -y install psmisc

# 杀死nginx
$ killall nginx
$ lsof -i :80   # 查看是否杀死程序
# 添加新用户
$ useradd -s /sbin/nologin -r www
# 修改nginx.conf的进程
user www;
worker_processes  4;
# 再次启动nginx
$ /usr/local/nginx/sbin/nginx 
$ lsof -i :80
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   11661 root    6u  IPv4  30504      0t0  TCP *:http (LISTEN)
nginx   11662  www    6u  IPv4  30504      0t0  TCP *:http (LISTEN)
nginx   11663  www    6u  IPv4  30504      0t0  TCP *:http (LISTEN)
nginx   11664  www    6u  IPv4  30504      0t0  TCP *:http (LISTEN)
nginx   11665  www    6u  IPv4  30504      0t0  TCP *:http (LISTEN)

(3)events

  这个指定的是每个工作进程(子进程)最大的并发数,也就是一个工作进程可以有多少个线程。
  工作进程 * 线程 就可以得到并发的数量。

(4)http服务器设置

  一个server叫做一个虚拟主机,server的主要配置。

四、Nginx默认网站

  默认网站概念:当Nginx配置文件中有且只有一个Server的时候,该Server就被Nginx认为是默认网站,所有发给Nginx服务器80端口的数据都会默认给该Server。
  因此只有一个server的时候叫做默认网站,有多个server的时候则叫做虚拟主机。

http {
    # 默认网站
    server {
        listen       80;
        server_name  localhost;

        charset utf-8;    # 修改字符集utf-8

        #access_log  logs/host.access.log  main;    # 使用默认日志路径

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {      # 错误代码页的位置
            root   html; 
        }
    }
}

1、默认网站配置——Nginx目录访问控制

  访问控制配置规则是允许个别,拒绝所有。

(1)常用模板

location /a {
    allow 192.168.1.0/24;     # 允许访问的网段
    deny all;
    #return 404;
    return http://www.jd.com; 
}

(2)使用示例

  对nginx.conf文件做如下配置:

# 网站根目录的相对路径,定位的路径在:/usr/local/nginx/html/a
location /a {
    # 只允许本机访问拒绝所有
    allow 127.0.0.1;
    deny all;
}

  重启nginx服务测试验证:

$ /usr/local/nginx/sbin/nginx -g ../conf/nginx.conf    # 启动前校验配置文件是否有问题
$ /usr/local/nginx/sbin/nginx 
$ lsof -i :80    # 检验服务是否启动
$ elinks http://192.168.31.42/a --dump          # 访问被拒绝    
                                 403 Forbidden
$ elinks http://127.0.0.1/a --dump            
   aaaa

(3)修改返回页面

  注意这里返回的是403,告诉了访问者,说明这里有目录,只是不允许访问。为了迷惑访问者不再尝试访问可做如下配置:

# 网站根目录的相对路径,定位的路径在:/usr/local/nginx/html/a
location /a {
    # 只允许本机访问拒绝所有
    allow 127.0.0.1;
    deny all;
    return 404;    # 还可以返回5xx系列,或者直接返回url:return  www.jd.com
}

  配置修改后,不想重新启动服务,可以给服务发送信号告诉它配置文件重读:

$ killall -s HUP nginx
$ lsof -i :80
$ elinks http://192.168.31.42/a --dump 
                                 404 Not Found

2、默认网站配置——Nginx登录验证

  登录验证的规则是:任何人都可以访问,但是需要凭用户密码才能访问。常见格式如下所示:

location /b {
    auth_basic "登陆验证"; 
    auth_basic_user_file /etc/nginx/htpasswd;
}

(1)auth_basic

  语法:auth_basic string | off;
  默认值是:auth_basic off;

(2)auth_basic_user_file file;

  语法:auth_basic_user_file file;

$ mkdir /etc/nginx/
$ touch /etc/nginx/htpasswd
$ echo 'sko:123456'  > /etc/nginx/htpasswd
$ killall -s HUP nginx
####打开浏览器访问192.168.31.42#####

  此时打开浏览器访问192.168.31.42/b/可以发现弹出了登录框,输入用户名和密码后并不能登录成功,因为密码并没有加密。

(3)使用openssl或htpasswd实现密码加密

  使用htpasswd实现密码加密:-c是指定文件路径没有文件会自动创建;-m是生成加密。文件后的sky是指定的用户名。

# 安装htpasswd
$ yum -y install httpd-tools
# 查看包来源
$ rpm -qf `which htpasswd`
httpd-tools-2.4.6-88.el7.centos.x86_64
# 创建加密密码的用户
$ htpasswd -m /etc/nginx/htpasswd sky  
New password:    # 输入密码
Re-type new password:      # 重复密码
Adding password for user sky

$ cat /etc/nginx/htpasswd 
sky:$apr1$kRE.rFXV$lbR2/Ga09e/i91qGrg/931

  此时打开浏览器访问192.168.31.42/b/,输入sky/123后,登录成功,看到页面信息bbbb。

3、默认网站配置——日志格式

  Nginx访问日志主要有两个参数控制:

  • log_format:用来定义记录日志的格式(可以定义多种日志格式,取不同名字即可)
    log_format log_name string
  • access_log:用来指定日至文件的路径及使用的何种日志格式记录日志
    access_log logs/access.log main;

(1)log_format格式变量

$remote_addr     #记录访问网站的客户端地址
$remote_user     #远程客户端用户名
$time_local        #记录访问时间与时区
$request            #用户的http请求起始行信息
$status              #http状态码,记录请求返回的状态码,例如:200、301、404等
$body_bytes_sent           #服务器发送给客户端的响应body字节数
$http_referer      #记录此次请求是从哪个连接访问过来的,可以根据该参数进 行防盗链设置。
$http_user_agent            #记录客户端访问信息,例如:浏览器、手机客户端等
$http_x_forwarded_for     #当前端有代理服务器时,设置web节点记录客户端 地址的配置,此参数生效的前提是代理服务器也要进行相关的x_forwarded_for设 置

(2)自定义日志格式示例

http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';
    # 第一步:自定义日志格式
    log_format hqs_01 '[$time_local]  $remote_addr "$request"  $status'

    #access_log  logs/access.log  main;
    #####其他代码省略####
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;
        charset utf-8;

        #access_log  logs/host.access.log  main;
        # 第二步:自定义日志
        access_log logs/host.access.log  hqs_01;
    }
}

  重启服务后,从浏览器上访问服务,查看日志信息:

$ tailf logs/host.access.log 
[11/Dec/2018:15:51:49 +0800]  192.168.31.28 "GET /b/ HTTP/1.1"  304sendfileon
[11/Dec/2018:15:52:28 +0800]  192.168.31.28 "GET / HTTP/1.1"  200sendfileon

(3)自定义日志格式为json

http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';
    # 自定义日志格式
    log_format hqs_01 '[$time_local]  $remote_addr "$request"  $status';
    # 自定义json日志格式
    log_format main_json '{"@timestamp":"$time_local",'
        '"client_ip": "$remote_addr",'
        '"request": "$request",'
        '"status": "$status",'
        '"bytes": "$body_bytes_sent",'
        '"x_forwarded": "$http_x_forwarded_for",'
        '"referer": "$http_referer"'
    '}';

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;
        charset utf-8;

        #access_log  logs/host.access.log  main;
        # 自定义日志
        access_log logs/host.access.log  main_json;

        location / {
            root   html;
            index  index.html index.htm;
        }

  重新启动nginx后,查看日志,可以看到新的日志格式:

$ killall nginx
$ sbin/nginx 
$ tailf logs/host.access.log 
{"@timestamp":"11/Dec/2018:16:25:30 +0800","client_ip": "192.168.31.28","request": "GET / HTTP/1.1","status": "304","bytes": "0","x_forwarded": "-","referer": "-"}
{"@timestamp":"11/Dec/2018:16:25:34 +0800","client_ip": "192.168.31.28","request": "GET /a HTTP/1.1","status": "404","bytes": "555","x_forwarded": "-","referer": "-"}

4、默认网站配置——防盗链

  盗链是指服务提供商自己不提供服务的内容,通过技术手段绕过其它有利益的最终用户界面(如广告),直接在自己的网站上向最终用户提供其它服务提供商的服务内容,骗取最终用户的浏览和点击率。受益者不提供资源或提供很少的资源,而真正的服务提供商却得不到任何的收益。
  百度就是最大的盗链公司,很多图片和数据在百度就看到了。服务提供商需要支付带宽,但访问和用户并没有增加,反而会越来越少。
  防盗链:在HTTP协议中,有一个表头字段叫referer,采用URL的格式来表示从哪儿链接到当前的网页或文件。通过referer,网站可以检测目标网页访问的来源网页,如果是资源文件,则可以跟踪到显示它的网页地址。有了referer跟踪来源,就可以通过技术手段来进行处理,一旦检测到来源不是本站即进行阻止或者返回指定的页面。

(1)使用示例

location /c {
    valid_referers none blocked *.ayitula.com;
    if ($invalid_referer) {
        return 403
    }
}

(2)参数介绍

  valid_referers定义的是有效的referer:

valid_referers none blocked *.ayitula.com; 
    - none:没有referer字段的(用户直接过来访问的)
    - blocked:硬件防火墙标识的
    - *.ayitula.com:自己的内网服务器

  if ($invalid_referer){return 403}说明所有不满足valid_referer的referer都是无效的referer,直接返回403页面。

  如果要实现宽广匹配:~是匹配,*是不区分大小写 .(png|gif|bmp)$:域名内任意以png/gif/bmp结尾的。

location ~* \.(png|gif|bmp)$ {
    valid_referers none blocked *.ayitula.com;
    if ($invalid_referer) {
        return 403
    }
}

五、作业实操

  发布一个内部HTML网站,要求对图片目录做防盗链设置,用户只能来自192.168.31.0网段,访问网站需要使用账号密码。

posted @ 2018-12-11 13:49  休耕  阅读(583)  评论(0编辑  收藏  举报