看在nginx使用之前

来源:互联网 发布:easyrecovery for mac 编辑:程序博客网 时间:2024/04/28 08:35

1.启动方式

To start nginx, run the executable file. Once nginx is started, it can be controlled by invoking the executable with the -s parameter. Use the following syntax:

nginx -s signal
Where signal may be one of the following:

stop — fast shutdown
quit — graceful shutdown
reload — reloading the configuration file
reopen — reopening the log files


Serving Static Content
An important web server task is serving out files (such as images or static HTML pages). You will implement an example where, depending on the request, files will be served from different local directories: /data/www (which may contain HTML files) and /data/images (containing images). This will require editing of the configuration file and setting up of a server block inside the http block with two location blocks.
First, create the /data/www directory and put an index.html file with any text content into it and create the /data/images directory and place some images in it.


2.选择location匹配的策略

If there are several matching location blocks nginx selects the one with the longest prefix.

When nginx selects a location block to serve a request it first checks location directives that specify prefixes, remembering location with thelongest prefix, and then checks regular expressions.If there is a match with a regular expression, nginx picks this location or, otherwise, it picks the one remembered earlier.


server {
    location / {
        proxy_pass http://localhost:8080/;
    }


    location ~ \.(gif|jpg|png)$ {
        root /data/images;
    }
}


3.默认模块

详见此处


4.如何选择底层connection驱动方式

On platforms that support several methods nginx will normally select the most efficient method automatically. However, if needed, a connection processing method can be selected explicitly with the use directive.
详见http://nginx.org/en/docs/events.html


5.debug方式

error_log /path/to/log debug;
http {
    server {
        error_log /path/to/log debug;
        ...

error_log memory:32m debug;


6.nginx选择server的方式

6.1 基于host选择server

In this configuration nginx tests only the request’s header field “Host” to determine which server the request should be routed to. If its value does not match any server name, or the request does not contain this header field at all, then nginx will route the request to the default server for this port. In the configuration above,the default server is the first one — which is nginx’s standard default behaviour. It can also be set explicitly which server should be default, with the default_server parameter in the listen directive:


server {
    listen      80 default_server;
    server_name example.net www.example.net;
    ...
}


6.2 选择server的顺序

When searching for a virtual server by name, if name matches more than one of the specified variants, e.g. both wildcard name and regular expression match, the first matching variant will be chosen, in the following order of precedence:
  • exact name
  • longest wildcard name starting with an asterisk, e.g. “*.example.org”
  • longest wildcard name ending with an asterisk, e.g. “mail.*”
  • first matching regular expression (in order of appearance in a configuration file)

6.3 server的写法

Wildcard names

A wildcard name may contain an asterisk only on the name’s start or end, and only on a dot border. The names “www.*.example.org” and “w*.example.org” are invalid.

Miscellaneous names

There are some server names that are treated specially.
If it is required to process requests without the “Host” header field in a server block which is not the default, an empty name should be specified:
server {
    listen       80;
    server_name  example.org  www.example.org  "";
    ...
}

catch-all

In catch-all server examples the strange name “_” can be seen:
server {
    listen       80  default_server;
    server_name  _;
    return       444;
}

6.4 针对server_name 过长的两个参数

could not build the server_names_hash,
you should increase server_names_hash_bucket_size: 32
could not build the server_names_hash,
you should increase either server_names_hash_max_size: 512

7.负载均衡

负载均衡
http {
    upstream myapp1 {
        ()| least_conn | ip_hash
        server srv1.example.com;
        server srv2.example.com;
        server srv3.example.com;
    }


    server {
        listen 80;

        location / {
            proxy_pass http://myapp1;
        }
    }
}


负载的更多配置http://nginx.org/en/docs/http/ngx_http_upstream_module.html#server

0 0
原创粉丝点击