看在nginx使用之前

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 the longest 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.


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值