Nginx Cookbook之使用篇基础(一)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/LoveStackover/article/details/79951271

前言

  不在介绍基础的安装编译部分,书中推荐yum源安装方式,非常便捷快速。

1. Configuring NGINX

By default, NGINX will have two main configuration files. The first is /etc/nginx/nginx.conf, which contains the main server configuration. 

  以下是一个默认的配置文件,最新版本的1.12.1与其有出入:

user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;

events {
    worker_connections 1024;
}

http {
    include /etc/nginx/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"';
    access_log /var/log/nginx/access.log main;
    sendfile on;
    #tcp_nopush on;
    keepalive_timeout 65;
    #gzip on;
    include /etc/nginx/conf.d/*.conf;
}

  几个关键参数的理解:

  • user nginx: defines the user in which NGINX will run as.
    • This is important to note if you have a server-side script which requires the ability to write files and a user will also require permission to read the files.
  • worker_processes: The optimal number of processes depends on many factors, but an easy starting reference is to go by the number of CPU cores your server has.
  • worker_connections:the maximum amount of simultaneous connections that a worker process can open.
  • include /etc/nginx/conf.d/*.conf:line tells NGINX to load all of the .conf files as if they were all part of the main nginx.conf file.
    • This allows you to separate the configuration for different sites.

  实际上nginx为prefork模型,以前笔者在UNP的记录篇也记录实验过该模型,work_process的数目并非越多越好。

2. Enabling modules

By default, not every module for NGINX has been compiled and is available.

这里写图片描述

  现在的nginx为动态加载module模型,与linux内核很像:

  • However, even though 1.9.11 added the dynamic modules, none of the standard modules are dynamic by default. To make them into dynamic modules, you’ll need to update the configure options.标准模块都是非动态的,如果想让他们变成动态的,需要使用将其先变成动态库

    • For example, to make the mail module dynamic, update –with-mail to –with-mail=dynamic.
  • The main nginx.conf configuration file will then need the following:然后在配置文件里加上下面的配置项
    • load_module “modules/ngx_mail_module.so”;

3. Deploying a basic site

  展示静态网页非常简单,笔者以Tinyhttp中的htdocs中的网页为例,进行配置:

  • 编辑配置文件添加修改如下:
 #修改nginx以root用户运行,因为笔者不想进行繁琐的权限修改哈。
 #因为如果你的静态网页放置的路径中每层目录的都必须对该用户有可
 #执行权限,就会出现403的错误信息,当然最终的index.html文件也应该有相应的读写权限。
  user root;
  server {
        listen 59877;
        server_name localhost;
        location / {
        root /root/tinyhttpd-0.1.0/htdocs;
        index index.html;
     }  
  • 确保没有权限问题后,关闭SElinux后,重新加载配置文件:
    [root@localhost tinyhttpd-0.1.0]# nginx -s reload
  • 浏览器检查结果:
    这里写图片描述

      接下来NCK中对刚才的server配置中,几条配置项进行说明:

    • server_name:This directive tells the server what hostname to match from the request. This allows you to run name-based virtual servers from one IP address, but with different domain names. You can also use different aliases here; 注意这里需要你在本机的/etc/hosts添加本机的别名
    • access_log: The access log records all client access to the site, stores it in the specified file (the second parameter), and uses the third parameter to define the format of the log (combined is the default).笔者刚刚的例子中没有使用这个log
    • location:This one is for a root directive (represented by /), meaning everything in the URL path. There are then two directives contained within this block—the first is the root directive. This defines where NGINX should look for the files.比较关键,如果你的静态网页不在nginx的工作目录,必须使用绝对地址,推荐使用绝对地址
    • index: The second is the index directive. This lets NGINX know what name of a file to try if it hasn’t been specified in the path.和Tinyhttp一样,会进行URL补全。

4. Basic monitoring

Monitoring both the performance and uptime of a web server is paramount when you want to ensure consistent performance.

  还是以刚刚的tinyhttp网页为例,使用基础的NGINX stub_status进行分析:

  • 首先在conf中tinyhttp网页的server块中添加下面配置信息:

    location = /nginx_status {
        stub_status on;
        access_log off;
        allow 127.0.0.1;
        deny all;
        }
  • 结果如下:
    这里写图片描述

      同样的NCK中对其中几个配置项进行说明:

    • To prevent information leakage about your system, we have added the allow command. This should be your IP address.
    • This is followed by the deny all command to prevent anyone else from loading the URL. We’ve also turned off access logs for this URL to save space.
        

     
      对刚刚产生的结果的说明:

    • Active connections:line lists the amount of connections to the server.
    • For a quiet server, this could be less than a dozen. For a busy server, expect this to be in the hundreds.对于server此刻繁忙度给了一个量化的指标:Busy server:上百个连接 quiet server: 小于12个
    • server accepts handled requests:
      • The first number represents the amount of accepted connections.
      • The second number represents the total number of handled connections. Unless there’s a resource limitation, the number of accepted and handled connections should be the same. 一般前两个数字相同
      • Next, we have the total number of client requests
    • The last line represents the state of the active connections:
      • Reading means NGINX is reading the request headers.
      • Writing means NGINX is writing data back to the client
      • Waitingmeans that the client is now idle but still has the connection open (due to keep-alives).

5. 总结

 今天学习了简单的配置文件信息,展示静态网页,动态模块及进行简单的监控Nginx状态的方式。总体来说都是非常简单的概念。如果要进行一个东西的原理剖析,首先应该会使用该东西,知道每部分的功能是什么样,有个直观的感受。

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭