add by zhj: 文章写的很好,适合初学者

原文:https://www.netguru.com/codestories/nginx-tutorial-basics-concepts

Introduction

Hello! Sharing is caring, so we'd love to share another piece of knowledge with you. We prepared a three-part nginx tutorial. If you already know something about nginx, or if you'd just like to expand your experience and understanding - this is the perfect place for you!

We will tell you how nginx works, going through the concepts behind it, how you can optimise it to boost your app's performance, and how to get it up and running quickly.

This tutorial will have three parts:

  • Basics concepts: get to know the difference between directive and context, the inheritance model, and the order in which nginx picks server blocks and locations.
  • Performance: tips and tricks on improving speed. We will discuss gzip, caching, buffers, and timeouts.
  • SSL setup: set up the configuration to serve content over HTTPS.

We aimed to create a series in which you can easily find the proper configuration for a particular topic (like gzip, SSL, etc.), or simply read it all through. For the best learning experience, we suggest you set nginx up on your own machine and fiddle with it yourself.

What is Nginx?

Nginx was originally created as a web server to solve the C10k problem. And, as a web server, it can serve your data with blazing speed. But nginx is so much more than just a web server. You can use it as a reverse proxy, making for easy integration with slower upstream servers (like Unicorn or Puma). You can distribute your traffic properly (load balancing), stream media, resize your images on the fly, cache content, and much more.

The basic nginx architecture consists of a master process and its workers. The master is supposed to read the configuration file and maintain worker processes, while workers do the actual processing of requests.

Base commands

To start nginx, simply type:

[sudo] nginx

While your nginx instance is running, you can manage it by sending signals:

[sudo] nginx -s signal

Available signals:

  • stop: fast shutdown
  • quit: graceful shutdown (wait for workers to finish their processes)
  • reload: reload the configuration file
  • reopen: reopen the log files

Directive and Context

By default, the nginx configuration file can be found in:

  • /etc/nginx/nginx.conf,
  • /usr/local/etc/nginx/nginx.conf, or
  • /usr/local/nginx/conf/nginx.conf

This file consists of:

  • directive: the option that consists of name and parameters; it should end with a semicolon
gzip on;
  • context: the section where you can declare directives (similar to scope in programming languages)
worker_processes 2; # directive in global context

http {              # http context
gzip on; # directive in http context server { # server context
listen 80; # directive in server context
}
}

Directive types

You have to pay attention when using the same directive in multiple contexts, as the inheritance model differs for different directives. There are 3 types of directives, each with its own inheritance model.

Normal

Has one value per context. Also, it can be defined only once in the context. Subcontexts can override the parent directive, but this override will be valid only in a given subcontext.

gzip on;
gzip off; # illegal to have 2 normal directives in same context server {
location /downloads {
gzip off;
} location /assets {
# gzip is in here
}
}

Array

Adding multiple directives in the same context will add to the values instead of overwriting them altogether. Defining a directive in a subcontext will override ALL parent values in the given subcontext.

error_log /var/log/nginx/error.log;
error_log /var/log/nginx/error_notive.log notice;
error_log /var/log/nginx/error_debug.log debug; server {
location /downloads {
# this will override all the parent directives
error_log /var/log/nginx/error_downloads.log;
}
}

Action directive

Actions are directives that change things. Their inheritance behaviour will depend on the module.

For example, in the case of the rewrite directive, every matching directive will be executed:

server {
rewrite ^ /foobar; location /foobar {
rewrite ^ /foo;
rewrite ^ /bar;
}
}

If the user tries to fetch /sample:

  • a server rewrite is executed, rewriting from /sample, to /foobar
  • the location /foobar is matched
  • the first location rewrite is executed, rewriting from /foobar, to /foo
  • the second location rewrite is executed, rewriting from /foo, to /bar

This is a different behaviour than what the return directive provides:

server {
location / {
return 200;
return 404;
}
}

In the case above, the 200 status is returned immediately.

Processing requests

Inside nginx, you can specify multiple virtual servers, each described by a server { } context.

server {
listen *:80 default_server;
server_name netguru.co; return 200 "Hello from netguru.co";
} server {
listen *:80;
server_name foo.co; return 200 "Hello from foo.co";
} server {
listen *:81;
server_name bar.co; return 200 "Hello from bar.co";
}

This will give nginx some insight on how to handle incoming requests. Nginx will first check the listen directive to test which virtual server is listening on the given IP:port combination. Then, the value from server_name directive is tested against the Host header, which stores the domain name of the server.

Nginx will choose the virtual server in the following order:

  1. Server listing on IP:port, with a matching server_name directive;
  2. Server listing on IP:port, with the default_server flag;
  3. Server listing on IP:port, first one defined;
  4. If there are no matches, refuse the connection.

In the example above, this will mean:

Request to foo.co:80     => "Hello from foo.co"
Request to www.foo.co:80 => "Hello from netguru.co"
Request to bar.co:80 => "Hello from netguru.co"
Request to bar.co:81 => "Hello from bar.co"
Request to foo.co:81 => "Hello from bar.co"

The server_name directive

The server_name directive accepts multiple values. It also handles wildcard matching and regular expressions.

server_name netguru.co www.netguru.co; # exact match
server_name *.netguru.co; # wildcard matching
server_name netguru.*; # wildcard matching
server_name ~^[0-9]*\.netguru\.co$; # regexp matching

When there is ambiguity, nginx uses the following order:

  1. Exact name;
  2. Longest wildcard name starting with an asterisk, e.g. “*.example.org”;
  3. Longest wildcard name ending with an asterisk, e.g. “mail.*”;
  4. First matching regular expression (in the order of appearance in the configuration file).

Nginx will store 3 hash tables: exact names, wildcards starting with an asterisk, and wildcards ending with an asterisk. If the result is not in any of the tables, the regular expressions will be tested sequentially.

It is worth keeping in mind that

server_name .netguru.co;

is an abbreviation of:

server_name  netguru.co  www.netguru.co  *.netguru.co;

With one difference: .netguru.co is stored in the second table, which means that it is a bit slower than an explicit declaration.

listen directive

In most cases, you’ll find that the listen directive accepts IP:port values.

listen 127.0.0.1:80;
listen 127.0.0.1; # by default port :80 is used listen *:81;
listen 81; # by default all ips are used listen [::]:80; # IPv6 addresses
listen [::1]; # IPv6 addresses

However, it is also possible to specify UNIX-domain sockets:

listen unix:/var/run/nginx.sock;

You can even use hostnames:

listen localhost:80;
listen netguru.co:80;

This should be used with caution, as the hostname may not be resolved upon nginx's launch, causing nginx to be unable to bind on a given TCP socket.

Finally, if the directive is not present, *:80, is used.

Minimal configuration

With all that knowledge, we should be able to create and understand the minimal configuration needed to run nginx.

# /etc/nginx/nginx.conf

events {}                   # event context needs to be defined to consider config valid

http {
server {
listen 80;
server_name netguru.co www.netguru.co *.netguru.co; return 200 "Hello";
}
}

rootlocation, and try_files directives

root directive

The root directive sets the root directory for requests, allowing nginx to map the incoming request onto the file system.

server {
listen 80;
server_name netguru.co;
root /var/www/netguru.co;
}

Which allows nginx to return server content according to the request:

netguru.co:80/index.html     # returns /var/www/netguru.com/index.html
netguru.co:80/foo/index.html # returns /var/www/netguru.com/foo/index.html

location directive

The location directive sets the configuration depending on the requested URI.

location [modifier] path

location /foo {
# ...
}

When no modifier is specified, the path is treated as prefix, after which anything can follow. The above example will match:

/foo
/fooo
/foo123
/foo/bar/index.html
...

Also, multiple location directives can be used in a given context:

server {
listen 80;
server_name netguru.co;
root /var/www/netguru.co; location / {
return 200 "root";
} location /foo {
return 200 "foo";
}
}
netguru.co:80   /       # => "root"
netguru.co:80 /foo # => "foo"
netguru.co:80 /foo123 # => "foo"
netguru.co:80 /bar # => "root"

Nginx also provides a few modifiers which can be used in conjunction with location. These modifiers impact which location block will be used, as each modifier has assigned precedence.

=           - Exact match
^~ - Preferential match
~ && ~* - Regex match
no modifier - Prefix match

Nginx will first check for any exact matches. If it doesn't find any, it'll look for preferential ones. If this match also fails, regex matches will be tested in the order of their appearance. If everything else fails, the last prefix match will be used.

location /match {
return 200 'Prefix match: matches everything that starting with /match';
} location ~* /match[0-9] {
return 200 'Case insensitive regex match';
} location ~ /MATCH[0-9] {
return 200 'Case sensitive regex match';
} location ^~ /match0 {
return 200 'Preferential match';
} location = /match {
return 200 'Exact match';
}
/match     # => 'Exact match'
/match0 # => 'Preferential match'
/match1 # => 'Case insensitive regex match'
/MATCH1 # => 'Case sensitive regex match'
/match-abc # => 'Prefix match: matches everything that starting with /match'

try_files directive

This directive will try different paths, returning whichever is found.

try_files $uri index.html =404;

So for /foo.html , it will try to return files in the following order:

  1. $uri ( /foo.html );
  2. index.html;
  3. If none is found: 404.

What’s interesting, if we define try_files in a server context, and then define a location that matches all requests, our try_files will not be executed. This will happen because try_files in a server context defines its own pseudo-location, which is the least specific location possible. Therefore, defining location / will be more specific than our pseudo-location.

server {
try_files $uri /index.html =404; location / {
}
}

Thus, you should avoid try_files in a server context:

server {
location / {
try_files $uri /index.html =404;
}
}

Concluding remarks

Thank you for reading. This series would not have been possible without the great amount of resources found in the depths of the internet. Here are a few great websites we found especially useful when writing this series:


We'll be more than happy to see some feedback or discussion, so feel free to leave comments or get in touch in any other way! Did you like the tutorial? Do you have some suggestions on what subject we should take up next? Or maybe you spotted a bug? Let us know and see you next time!

Nginx Tutorial #1: Basic Concepts(转)的更多相关文章

  1. Basic Concepts of Block Media Recovery

    Basic Concepts of Block Media Recovery Whenever block corruption has been automatically detected, yo ...

  2. (二)Basic Concepts 基本概念

    Basic Concepts There are a few concepts that are core to Elasticsearch. Understanding these concepts ...

  3. CMUSphinx Learn - Basic concepts of speech

    Basic concepts of speech Speech is a complex phenomenon. People rarely understand how is it produced ...

  4. [Web Service] Tutorial Basic Concepts

    WSDL是网络服务描述语言,是一个包含关于web service信息(如方法名,方法参数)以及如何访问它. WSDL是UDDI的一部分. 作为web service 应用程序之间的接口,发音为wiz- ...

  5. [Network]Introduction and Basic concepts

    [该系列是检讨计算机网络知识.因为现在你想申请出国.因此,在写这篇博客系列的大多数英语.虽然英语,但大多数就是我自己的感受和理解,供大家学习和讨论起来] 1 Network Edge The devi ...

  6. Lesson 1 Basic Concepts: Part 1

    www.how-to-build-websites.com/basic-concepts/part1.php An introduction to domain names, web servers, ...

  7. Eric Linux - 1 Basic concepts of linux

    Computer basic Computer 5 parts CPU Input Output Memory External storage device. CPU RISC: Reduced I ...

  8. Basic concepts of docker/kubernete/kata-container

    Kubereters An open-source system for automating deployment, scaling, and management of containerized ...

  9. MVC LINQ to SQL: Basic Concepts and Features

    http://www.codeproject.com/Articles/215712/LINQ-to-SQL-Basic-Concepts-and-Features

随机推荐

  1. 剑指:最小的k个数

    题目描述 输入 n 个整数,找出其中最小的 K 个数.例如输入 4,5,1,6,2,7,3,8 这 8 个数字,则最小的 4 个数字是 1,2,3,4. 解法 解法一 利用快排中的 partition ...

  2. Linux从入门到放弃、零基础入门Linux(第一篇):计算机操作系统简介、linux介绍

    一.计算机操作系统简介 操作系统的定义: 操作系统是一个用来协调.管理和控制计算机硬件和软件资源的系统程序,它位于硬件和应用程序之间. 操作系统的内核的定义: 操作系统的内核是一个管理和控制程序,负责 ...

  3. atlas笔记

    目录 环境 Mysql+Atlas配置 atlas:mysql-proxy扩展,mysql中间件,可以实现分表.分库(sharding版本).读写分离.数据库连接池等功能! Atlas类似于Twemp ...

  4. CORE DUMP生成调试

    之前我调试嵌入式linux程序,一般是借助ucontext库,在发生段错误时,直接将错误函数打印出来.有同事建议我使用core dump,于是我今天在嵌入式板卡尝试了core文件的生成,但是也是几经波 ...

  5. 查看 Python 对象的属性

    1 .dir函数可以返回一个对象的所有属性和方法. 示例:查看 int 对象的属性和方法 示例: 查看 dict 对象的属性和方法 标红的这些是不是遇到过? 2.help()调用内置帮助系统 示例 3 ...

  6. 如何使用和关闭onbeforeunload 默认的浏览器弹窗事件

    Onunload,onbeforeunload都是在刷新或关闭时调用,可以在<script>脚本中通过 window.onunload来指定或者在<body>里指定.区别在于o ...

  7. 监控进程cpu meminfo

    https://github.com/cdrandin/cpsc_351 https://github.com/cdrandin?after=Y3Vyc29yOnYyOpK5MjAxNC0wNy0xM ...

  8. Windows中的txt文件到Mac打开是乱码 解决办法

    在Mac下打开“文本编辑”程序之后,选择菜单“文本编辑” -> “偏好设置”.2)在“偏好设置”中选择第二个标签页“打开和存储”,选择“纯文本文件编码”中的“打开文件”和“存储文件”修改成为“中 ...

  9. sass、less异同

    相同点: 1.混入(Mixins):class中的class 2.参数混入:可以传递参数的class,就像函数一样 3.嵌套规则:class中嵌套class,从而减少重复的代码 4.运算:css中用上 ...

  10. 一本通 1615:【例 1】序列的第 k 个数

    传送门 我在这里! 思路 输入一个序列的前三个数并求出这个序列的第K项,这个数列不是等比序列就是等差数列,等差数列比较好判断,如果序列中\(a_{i+2}-a_{i+1}=a_{i+1}-a_{i}\ ...