nginx.conf configuration file Resolution (HTTP, server, location Proxy_pass)

Source: Internet
Author: User
Tags sendfile nginx reverse proxy

nginx.conf Configuration file Resolution (HTTP, server, location)Tags: nginxnginx-conf2017-04-26 20:10 1031 People read comments (0) favorite reports Classification:Nginx (8)

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

nginx.conf files in the installation directory/conf directory

1, define the user and user group Nginx run

user nginx nginx;

2, Nginx process number, recommended to set equal to the total number of CPU cores

worker_processes 1;

3. Global error log definition type, [Debug | info | notice | warn | error | crit]

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

4. Process files

pid /var/run/nginx.pid;

5, the working mode and the maximum number of connections: Worker_connections is the largest number of concurrent links for a single background worker process, and the total number of concurrent worker_processes and Worker_connections is the product of Max_ Clients = worker_processes * worker_connections

events {    worker_connections  1024;}

6. Some configuration under HTTP and its significance

Include Mime.types;#文件扩展名与文件类型映射表default_typeapplication/octet-stream;  #默认文件类型sendfile on; Span class= "hljs-comment" > #开启高效文件传输模式, sendfile instruction Specifies whether Nginx calls the Sendfile function to output the file, for normal applications set to ON, if used for downloading applications such as disk IO heavy load application, can be set is off to balance disk and network I/O processing speed and reduce the load on the system. Note: If the picture does not appear normal, change this to off. AutoIndex on;  #开启目录列表访问, the appropriate download server, the default shutdown. Tcp_nopush on;  #防止网络阻塞tcp_nodelay on; Span class= "hljs-comment" > #防止网络阻塞keepalive_timeout 120;  #长连接超时时间, the unit is the second gzip on; Span class= "hljs-comment" > #开启gzip压缩输出           

7, server virtual host some configuration and its significance

For example:

#虚拟主机1 server{  listen       80;  server_name  www.nginx1.com;  location / {     root   html;     index  index.html index.htm; } } #虚拟主机2 server{ listen 80; server_name localhost; location / { root html; index index.html index.htm; } }}

Here server_name configuration domain name, if it is local test, need to WinDOS under the Hosts file, your domain name and IP added (C:\Windows\System32\drivers\etc\hosts)

Nginx supports three types of virtual host configurations

    • 1. IP-based virtual host, (one host binds multiple IP addresses)
server{  listen       192.168.1.1:80; server_name localhost;}server{ listen 192.168.1.2:80; server_name localhost;}
    • 2. Domain-based virtual host (ServerName)
#域名可以有多个,用空格隔开server{  listen       80;  server_name  www.nginx1.com www.nginx2.com;}server{ listen 80; server_name www.nginx3.com;}
    • 3, Port-based virtual host (listen not write IP port mode)
server{  listen       80;  server_name  localhost;}server{  listen       81;  server_name  localhost;}

Location map resolution under Server (official Chinese document: Ngx_http_core_module) Matching rules:location [ = | ~ | ~* | ^~ ] uri { ... }

Location URI {}:

Takes effect on all objects under the current path and sub-path;

Location = URI {}:

Exactly matches the specified path (note that the URL is best for a specific path) and does not include sub-paths, so it only takes effect for the current resource;

Location ~ URI {}:

Location ~* URI {}:

The pattern-matching URI, where the URI can use regular expressions, ~ case-sensitive characters, ~* not distinguish between character case;

Location ^~ URI {}:

Regular expressions are no longer checked

Priority: = > ^~ > ~|~* >/|/dir/

Example:

{    [ configuration A ]}location / {    [ configuration B ]}location /documents/ {    [ configuration C ]}location ^~ /images/ {    [ configuration D ]}location ~* \.(gif|jpg|jpeg)$ { [ configuration E ] } 
Answer: Request "/" match configuration A, request "/index.html" Match configuration B, request "/documents/document.html" Match configuration C, request "/images/1.gif" match configuration D, request "/DOCUMENTS/1". JPG "Matching configuration E

Location Configuration Rules

1, the "=" prefix of the instructions strictly match this query. If found, stop the search.
2. All the remaining regular strings, matching the most accurate (the longest one in general). If this match uses the ^? prefix, the search stops.
3, regular expression, in the configuration file is from the top down matching
4. If the 3rd rule produces a match, the result is used. Otherwise, as is used from the 2nd rule
Special cases:
In both cases, you do not need to continue to match the regular location:
(1) When the normal location in front of the designated "^~", specifically told Nginx this article general location once matched, you do not need to continue regular match.
(2) When the normal location is exactly the exact match, not the maximum prefix match, the matching regular is no longer maintained

When configuring Proxy_pass in Nginx, when the following URL is added/, the equivalent is the absolute root path, the Nginx will not be the location of the matching path part of the proxy walk, if not/, will be the matching path part also to the agent go.

The following four cases are accessed separately using http://192.168.1.4/proxy/test.html.

The first type:

location/proxy/{

Proxy_pass http://127.0.0.1:81/;

}

will be proxied to http://127.0.0.1:81/test.html this URL

The second (relative to the first, the last one less/)

location/proxy/{

Proxy_pass http://127.0.0.1:81;

}

will be proxied to http://127.0.0.1:81/proxy/test.html this URL

The third type:

location/proxy/{

Proxy_pass http://127.0.0.1:81/ftlynx/;

}

will be proxied to http://127.0.0.1:81/ftlynx/test.html this URL.

The fourth case (in relation to the third, the last Less/):

location/proxy/{

Proxy_pass Http://127.0.0.1:81/ftlynx;

}

will be proxied to http://127.0.0.1:81/ftlynxtest.html this URL

The above results are all I have tested with the log file. As can be seen from the results, it should be said to be divided into two cases correctly. That is http://127.0.0.1:81 (the second of the above) this and http://127.0.0.1:81/.... (1,3,4 above) this kind of.

In addition Nginx reverse proxy, Tengine (Nginx upgrade version) Health check also used location knowledge, you can go to see

nginx.conf configuration file Resolution (HTTP, server, location Proxy_pass)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.