Nginx Configuration Detailed

Source: Internet
Author: User

Preface

Nginx is Lgor Sysoev for the Russian traffic second rambler.ru site design and development. Released since 2004, with the power of open source, has been close to maturity and perfection.

Nginx features rich, can be used as an HTTP server, but also as a reverse proxy server, mail server. Support FastCGI, SSL, Virtual Host, URL Rewrite, Gzip and other functions. and supports many third-party module extensions.

Nginx's stability, feature set, sample profile, and consumption of low-system resources have made him behind the list, with a 12.18% usage rate on a globally active website, about 22.2 million sites.

It blows almost, if you do not enjoy, you can Baidu encyclopedia or some books to find such boast, abound.

Nginx Common Functions

1, HTTP proxy, reverse proxy: As one of the most common functions of Web server, especially the reverse proxy.

Here I give 2 pictures, to the positive agent and response agent to do an interpretation, specific details, we can read the information.

Nginx provides stable performance when acting as a reverse proxy, and provides the ability to configure the forwarding function flexibly. Nginx can be based on different regular matching, take a different forwarding strategy, than the end of the file file server, dynamic page to go to the Web server, as long as you are writing no problem, and the corresponding server solution, you can play as you like. and Nginx to return the results of error page jump, abnormal judgment and so on. If the server being distributed has an exception, he can retransmit the request to another server and then automatically remove the exception server.

2. Load Balancing

Nginx provides 2 load balancing strategies: built-in policies and scaling strategies. Built-in strategy for polling, weighted polling, Ip hash. Expand the strategy, the imagination, only you can not think of what he can't do, you could refer to all the load balancing algorithm, give him one by one to find to do the next implementation.

3 Graphs to understand the implementation of these three load balancing algorithms

IP hash algorithm, the client requests the IP hash operation, and then according to the hash result will be the same client IP request distributed to the same server for processing, can solve the session does not share the problem.

3. Web Caching

Nginx can do different cache processing for different files, flexible configuration, and support Fastcgi_cache, mainly used to cache fastcgi dynamic program. With the third party's ngx_cache_purge, the development of the URL cache content can be managed and deleted.

4, Nginx related address

Source: Https://trac.nginx.org/nginx/browser

Official website: http://www.nginx.org/

nginx configuration file Structure

If you download good, your installation files, may wish to open the Conf folder of the nginx.conf file, Nginx Server basic configuration, the default configuration is also stored here.

Comment Symbol bit in nginx.conf #

The structure of Nginx file, this pair of students just getting started, can look at two more eyes.

the default Config

#user nobody;worker_processes1; #error_log logs/Error.log, #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid;events {worker_connections1024x768;}    HTTP {include mime.types; Default_type Application/octet-stream; #log_format Main'$remote _addr-$remote _user [$time _local] "$request"'    #                  '$status $body _bytes_sent "$http _referer"'    #                  '"$http _user_agent" "$http _x_forwarded_for "'; #access_log logs/Access.log Main;    Sendfile on;    #tcp_nopush on; #keepalive_timeout0; Keepalive_timeout $;    #gzip on; server {Listen the;        server_name localhost; #charset Koi8-S; #access_log logs/Host.access.log Main; Location/{root HTML;        Index index.html index.htm; } #error_page404/404. html; # REDIRECT Server error pages to theStaticPage/50x.html # Error_page - 502 503 504/50x.html; Location= /50x.html {root html; } # Proxy The PHP scripts to Apache listening on127.0.0.1: the# #location~\.php$ {# Proxy_pass http://127.0.0.1;#} # Pass the PHP scripts to FastCGI server listening on127.0.0.1:9000# #location~\.php$ {# root HTML; # Fastcgi_pass127.0.0.1:9000;        # Fastcgi_index index.php; # Fastcgi_param Script_filename/Scripts$fastcgi_script_name;        # include Fastcgi_params; #} # Deny access to. htaccess files,ifApache's Document Root# concurs with Nginx'S One# #location~ /\.ht {# deny all; #}} # anotherVirtualHostusingMix of ip-, name-, and port-based configuration # #server {# listen8000; # Listen Somename:8080;    # server_name somename alias Another.alias; # Location/{# root HTML;    # index index.html index.htm; #} #} # HTTPS Server # #server {# listen443SSL;    # server_name localhost;    # ssl_certificate Cert.pem;    # Ssl_certificate_key Cert.key;    # Ssl_session_cache shared:ssl:1m;    # ssl_session_timeout 5m; # Ssl_ciphers High:!anull:!MD5;    # ssl_prefer_server_ciphers on; # Location/{# root HTML;    # index index.html index.htm; #    }    #}}
View Code

NGINX file Structure

...              #全局块events {         #events块 ...   } HTTP      #http块 {    ...   #http全局块    Server        #server块    {         ...       #server全局块 location        [PATTERN]   #location块        {            ...        }        Location [PATTERN]         {            ...        }    }    Server    {      ...    }    ...     #http全局块}

1. Global BLOCK: Configure the command that affects Nginx Global. There are usually user groups running Nginx server, nginx process PID storage path, log storage path, configuration file introduction, allow to generate worker process number, etc.

2. Events BLOCK: The configuration affects the Nginx server or the network connection with the user. There are the maximum number of connections per process, which event-driven model is selected to handle connection requests, whether to allow multiple network connections to be accepted at the same time, and to turn on serialization of multiple networks.

3, HTTP BLOCK: can be nested multiple servers, configure proxy, cache, log definition and other most functions and the configuration of third-party modules. such as file introduction, Mime-type definition, log customization, whether to use Sendfile transfer file, connection timeout, number of single connection requests, etc.

4, server block: Configure the parameters of the virtual host, an HTTP can have more than one server.

5, location block: Configure the routing of the request, and the processing of various pages.

The following gives you a configuration file, as an understanding, but also into a test machine I set up, give you an example.

########### Each instruction must have a semicolon end.  ################# #user Administrator administrators; #配置用户或者组, the default is nobody nobody. #worker_processes2; #允许生成的进程数, the default is 1#pid/nginx/pid/Nginx.pid; #指定nginx进程运行文件存放地址error_log Log/error.log Debug; #制定日志路径, level. This setting can be put into a global block, an HTTP block, a server block, and a level with this: debug|info|notice|warn|error|crit|alert|emergevents {Accept_mutex on;  #设置网路连接序列化, to prevent the occurrence of surprise group, the default is on Multi_accept on;      #设置一个进程是否同时接受多个网络连接, the default is off #use epoll; #事件驱动模型,Select|poll|kqueue|epoll|resig|/dev/poll|Eventport worker_connections1024x768;   #最大连接数, the default is 512}http {include mime.types; #文件扩展名与文件类型映射表 Default_type Application/octet-stream; #默认文件类型, the default is text/plain #access_log off; #取消服务日志 Log_format MyFormat'$remote _addr– $remote _user [$time _local] $request $status $body _bytes_sent $http _referer $http _user_agent $http _x_ Forwarded_for'; #自定义格式 access_log Log/Access.log MyFormat;   #combined为日志格式的默认值 sendfile on;    #允许sendfile方式传输文件, the default is off, which can be in HTTP blocks, server blocks, and location blocks.  Sendfile_max_chunk 100k;    #每个进程每次调用传输数量不能大于设定的值, the default is 0, that is, there is no upper limit. Keepalive_timeout $;    #连接超时时间, the default is 75s, which can be http,server,location blocks. Upstream Mysvr {server127.0.0.1:7878; Server192.168.10.121:3333backup; #热备} error_page404Https//www.baidu.com; #错误页server {keepalive_requests -; #单连接请求上限次数. Listen4545; #监听端口 server_name127.0.0.1; #监听地址Location~*^.+$ {#请求的url过滤, regular match, ~ is case-sensitive, ~*to be case insensitive.  #root path;  #根目录 #index Vv.txt; #设置默认页 Proxy_pass http://Mysvr; List of servers #请求转向mysvr definedDeny127.0.0.1; #拒绝的ip Allow172.18.5.54; #允许的ip} }} 

Above is the basic configuration of Nginx, the following points need to be noted:

1, 1. $remote _addr and $http_x_forwarded_for to record the IP address of the client; 2. $remote _user: Used to record the client user name; 3. $time _local: Used to record access time and time zone; 4.$ Request: The URL and HTTP protocol used to record requests;

5. $status: Used to record the status of the request; 6. $body _bytes_s ent: Record the size of the principal content sent to the client file, 7. $http _referer: Used to record links from that page; 8. $http _user_ Agent: Records information about the client browser;

2, surprise group phenomenon: A network connection arrives, a number of sleep processes are woken up by colleagues, but only one process can get links, which will affect system performance.

3, each instruction must have a semicolon end.

Summary

If you use these techniques in the development process, or if you want to use the problem encountered, welcome to the upper left corner of the group, we discuss the study together, this article has not been continued.

Nginx Configuration Detailed

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.