nginx学习(五)——nginx的配置系统2

1.配置信息概览

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
error_log  logs/error1.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #这个upstream会和下面xxcupid1那个server关联起来,然后映射到192.168.237.186:10087
    #这里配置了两个,会用roundrobin实现负载均衡
    upstream xxcupid1 {
        #least_conn;
        #ip_hash;
        #Consumer
        server 192.168.237.186:10087 weight=3;
        #Provider
        server 192.168.237.186:10086;
        #server srv3.example.com;
    }
    upstream consumer {
        server 192.168.237.186:10087;
    }
    upstream provider {
        server 192.168.237.186:10086;
    }
    #gzip  on;
    #""任何没匹配上的都会走这个,如果下面还有一个""的话,会提示
    #nginx: [warn] conflicting server name "" on 0.0.0.0:80, ignored
    server {
        listen       80;
        #定义使用xxcupid1.baidu.com访问
        server_name  xxcupid1.baidu.com;
        #定向到xxcupid1这个upstream
        location /test {
            proxy_pass http://xxcupid1;
        }
    }
    server {
        listen       80;
        #定义使用xxcupid2.baidu.com访问
        server_name  xxcupid2.baidu.com;

        #定向到欢迎页面
        location / {
            root   html;
            index  index.html index.htm;
        }
        
        #定向到test这个upstream
        location /test {
            proxy_pass http://provider;                                                                                                                      
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

server name

http://nginx.org/en/docs/http/ngx_http_core_module.html#server_name
Sets names of a virtual server, for example:
server {
    server_name example.com www.example.com;
}
The first name becomes the primary server name.

server

http://nginx.org/en/docs/http/ngx_http_core_module.html#server
Sets configuration for a virtual server. There is no clear separation between IP-based (based on the IP address) and name-based (based on the “Host” request header field) virtual servers. Instead, the listen directives describe all addresses and ports that should accept connections for the server, and the server_name directive lists all server names. Example configurations are provided in the “How nginx processes a request” document.

listen

http://nginx.org/en/docs/http/ngx_http_core_module.html#listen
Sets the address and port for IP, or the path for a UNIX-domain socket on which the server will accept requests. Both address and port, or only address or only port can be specified. An address may also be a hostname, for example:

listen 127.0.0.1:8000;
listen 127.0.0.1;
listen 8000;
listen *:8000;
listen localhost:8000;

root

http://nginx.org/en/docs/http/ngx_http_core_module.html#root
Sets the root directory for requests. For example, with the following configuration


location /i/ {
    root /data/w3;
}
The /data/w3/i/top.gif file will be sent in response to the “/i/top.gif” request.


The path value can contain variables, except $document_root and $realpath_root.


A path to the file is constructed by merely adding a URI to the value of the root directive. If a URI has to be modified, the alias directive should be used.

像上面指定的root是html,比如使用xxcupid2.baidu.com/oscar.html访问的时候,会去nginx的html文件夹下去找对应文件。
如果这里指定改了一个绝对路径的话,会因为没有权限而无法读取到对应文件。

index

http://nginx.org/en/docs/http/ngx_http_index_module.html#index
Defines files that will be used as an index. The file name can contain variables. Files are checked in the specified order. The last element of the list can be a file with an absolute path. Example:


index index.$geo.html index.0.html /index.html;
It should be noted that using an index file causes an internal redirect, and the request can be processed in a different location. For example, with the following configuration:


location = / {
    index index.html;
}


location / {
    ...
}
a “/” request will actually be processed in the second location as “/index.

proxy_pass

http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass
Sets the protocol and address of a proxied server and an optional URI to which a location should be mapped. As a protocol, “http” or “https” can be specified. The address can be specified as a domain name or IP address, and an optional port:


proxy_pass http://localhost:8000/uri/;
or as a UNIX-domain socket path specified after the word “unix” and enclosed in colons:


proxy_pass http://unix:/tmp/backend.socket:/uri/;
If a domain name resolves to several addresses, all of them will be used in a round-robin fashion. In addition, an address can be specified as a server group.


A request URI is passed to the server as follows:


If the proxy_pass directive is specified with a URI, then when a request is passed to the server, the part of a normalized request URI matching the location is replaced by a URI specified in the directive:
location /name/ {
    proxy_pass http://127.0.0.1/remote/;
}
If proxy_pass is specified without a URI, the request URI is passed to the server in the same form as sent by a client when the original request is processed, or the full normalized request URI is passed when processing the changed URI:
location /some/path/ {
    proxy_pass http://127.0.0.1;
}
Before version 1.1.12, if proxy_pass is specified without a URI, the original request URI might be passed instead of the changed URI in some cases.
In some cases, the part of a request URI to be replaced cannot be determined:


When location is specified using a regular expression.
In this case, the directive should be specified without a URI.


When the URI is changed inside a proxied location using the rewrite directive, and this same configuration will be used to process a request (break):
location /name/ {
    rewrite    /name/([^/]+) /users?name=$1 break;
    proxy_pass http://127.0.0.1;
}
In this case, the URI specified in the directive is ignored and the full changed request URI is passed to the server.


A server name, its port and the passed URI can also be specified using variables:


proxy_pass http://$host$uri;
or even like this:


proxy_pass $request;
In this case, the server name is searched among the described server groups, and, if not found, is determined using a resolver.


WebSocket proxying requires special configuration and is supported since version 1.3.13.

该指令可以指向一个domain name(域名),也可以指向一个ip地址和端口。
指向域名的时候可以配合upstream使用。
指向ip地址和端口的时候,uri是可以被替换的,比如下面的配置:
location /name {
            proxy_pass http://192.168.237.186:10086/test;
        }
这里访问的时候使用/name进行访问,映射到的地址却是ip:host/test

upstream

http://nginx.org/en/docs/http/ngx_http_upstream_module.html#upstream

Defines a group of servers. Servers can listen on different ports. In addition, servers listening on TCP and UNIX-domain sockets can be mixed.


Example:


upstream backend {
    server backend1.example.com weight=5;
    server 127.0.0.1:8080       max_fails=3 fail_timeout=30s;
    server unix:/tmp/backend3;


    server backup1.example.com  backup;
}
By default, requests are distributed between the servers using a weighted round-robin balancing method. In the above example, each 7 requests will be distributed as follows: 5 requests go to backend1.example.com and one request to each of the second and third servers. If an error occurs during communication with a server, the request will be passed to the next server, and so on until all of the functioning servers will be tried. If a successful response could not be obtained from any of the servers, the client will receive the result of the communication with the last server.

server in upstream

http://nginx.org/en/docs/http/ngx_http_upstream_module.html#upstream

你可能感兴趣的:(nginx)