Nginx Cookbook之使用篇基础(一)




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. 


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

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.


2. Enabling modules

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



  • 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/”;

3. Deploying a basic site


  • 编辑配置文件添加修改如下:
  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
  • 浏览器检查结果:


    • 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;
        deny all;
  • 结果如下:


    • 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. 总结


想对作者说点什么? 我来说一句