Nginx Web Server Configuration

Source: Internet
Author: User
Tags sendfile serialization nginx server

Nginx is a lightweight, high-performance Web server designed to respond quickly to large volumes of static file requests and efficiently utilize system resources. Unlike Apache handling requests in a process-or thread-oriented manner, Nginx uses asynchronous event-driven models to perform more prominently in high-concurrency connections. 、

1.Nginx Installation:

Under Ubuntu, you can build and install the process, directly apt-get
sudo apt-get install Nginx
After installation, you can directly pass:
sudo service nginx start
To start the Nginx service, Nginx set the default 80 port forwarding, we can again browser access to Http://locallhost to check.

2.Nginx configuration file Structure

Nginx is the power of the configuration file to achieve, Nginx is a binary file Nginx read into a configuration file nginx.conf (nginx.conf may include a number of sub-profiles) to achieve a variety of functions.

The Nginx master configuration file is divided into the following sections:

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 configuration file structure is roughly as follows:

3. Some basic configurationsGlobal block-related configuration
 User Administrator administrators; #   Configure the user or group, which defaults to nobody nobody.  worker_processes 2; #   The number of processes allowed to be generated, by default 1  #   Locate the global error log file, level notice display, and Debug,info,warn,error,crit mode, debug output is the most, crir output is minimal, depending on the actual environment  error_log logs/error.log;error_log logs / Error.log notice;error_log logs /error.log info;pid /nginx/pid/nginx.pid ; #   specify Nginx process run file address  worker_rlimit_nofile 65535 #   Specifies the maximum number of file descriptors open by an nginx process, limited by the maximum number of open files for a system process  error_log log/error.log Debug ; #   make log path, level. This setting can be put into global blocks, HTTP blocks, server blocks  
Events Block related configuration
Events {  # set up the network connection serialization to prevent the cluster from happening, default toon  # Set Whether a process accepts multiple network connections at the same time, default to OFF   # Event-driven model, Select|poll|kqueue|epoll|resig|/dev/poll|eventport  #  Maximum number of connections, default is
HTTP block-related configuration
###### #Nginx的Http服务器配置, gzip configurationhttp {include mime.types;#set MIME type, type defined by Mime.type fileDefault_type Application/octet-stream;#default file type, default is Text/plainAccess_log off;#Cancel Service LogLog_format MyFormat '$remote _addr–$remote _user[$time _local]$request $status $body _bytes_sent      $http _referer $http _user_agent $http _x_forwarded_for‘;#Custom FormatsAccess_log Log/access.log MyFormat;#combined the default value for the log formatSendfile on;#Open Efficient file transfer mode, default to OFF, can be in HTTP block, server block, location block. Sendfile_max_chunk 100k;#Each process calls the number of transfers cannot be greater than the set value, the default is 0, that is, no upper limit. Keepalive_timeout 65;#The connection timeout time, default is 75s, can be in http,server,location block. Proxy_set_header X-real-ip$remote _addr;#get the user's real IPProxy_set_header x-forwarded- for $proxy _add_x_forwarded_for;  Proxy_redirect off; Proxy_buffers1288k;  Proxy_intercept_errors on; Proxy_send_timeout240; Proxy_connect_timeout240; Proxy_read_timeout240; Client_max_body_size 20M; #set the maximum number of individual file bytes allowed for client requestsClient_header_buffer_size 32k;#Specify the Headebuffer size from the client request headerClient_body_temp_path/dev/shm/client_body_temp;#Specifies the directory path where the connection request attempts to write to the cache fileLarge Client_header_buffers 4 32k;#Specifies the maximum number and size of caches for large headers in a client request, currently set to 4 32KBClient_header_timeout 10;#Set client request Read time-outClient_body_timeout 10;#Set client request principal read time-outSend_timeout#to set the timeout time for the corresponding clientgzip on;#turn on gzip compressionGzip_min_length 1k;#set the minimum number of bytes allowed for compressed pagesGzip_buffers 4 16k;#Request 4 units of 16K of memory as compressed result stream cacheGzip_http_version 1.1;#set the version that recognizes the HTTP protocol by default to 1.1Gzip_comp_level 2;#Specify gzip compression ratio, the smaller the 1-9 number, the smaller the compression ratio, the fasterGzip_types text/plain application/x-javascript text/css application/xml;#specifying the type of compressionGzip_vary on;#have the front-end cache server go through the GZIP-compressed pageupstream Mysvr {server127.0.0.1:7878; Server192.168.10.121:3333 backup;#Hot Spares} error_page502 503 504/50x.html;#error PageServer {Listen80;#The listening port isserver_name 127.0.0.1;#listening address, domain name can have multiple, separated by a spaceKeepalive_requests 120;#The maximum number of single connection requests. CharSet Koi8-r;#set the language encoding for accessAccess_log Logs/host.access.log main;#set the storage path and log format of the virtual host access log as main    #Default RequestLocation/ {
Listen 80;
server_name 127.0.0.1;
Return 301 https://$server _name$request_uri;#重定向到https
}
    #static files, Nginx handle themselvesLocation ~ ^/(images|javascript|js|css|flash|media|static)/{expires 30d;#Expiry Time    }    #prohibit access to. htxxx FilesLocation ~/. HT {deny all; }    #root path; #根目录Proxy_pass Http://mysvr;#request to Mysvr defined server listDeny 127.0.0.1;#denied IPAllow 172.18.5.54;#allowed IP} server {Listen443;#listening on port 443server_name 127.0.0.1;      SSL on; Ssl_certificate/etc/nginx/ssl/server.cer;#Configuring the certificate pathssl_certificate_key/etc/nginx/ssl/Server_key.cer; Location/{proxy_pass http:127.0.0.1:8080;      }    }}

After the Nginx configuration file is modified:
Cd/usr/local/nginx/sbin
./nginx-t re-compiling Nginx
./nginx-s Reload reboot Nginx Reload new configuration file

4.Nginx configuration file-related syntaxLocation Syntax:

Location [=|~|~*|^~]/uri/{...}

= Start with exact match
^~ begins by indicating that the URI begins with a regular string and is understood to match the URL path.
~ For case-sensitive matching
~* for case-insensitive matching
!~ and!~* are case insensitive and case insensitive, respectively
/generic match, any request will be matched to.
Multiple location configurations in the case of the match order
First match =, next match ^~, followed by the regular match in the order of the file, and finally to the/General match. When a match succeeds, the match is stopped and the request is processed according to the current matching rule.
Handle static file requests, which are the strengths of Nginx as an HTTP server
# There are two configuration modes, directory matching or suffix matching, either one or use
Location ^~/static/{
root/webroot/static/;
}
Location ~* \. (Gif|jpg|jpeg|png|css|js|ico) $ {
root/webroot/res/;
}

Rewrite syntax:

last– basically use this flag.
break– abort Rewirte, do not continue to match
redirect– returns the HTTP status of a temporary REDIRECT 302
permanent– returns the HTTP status of permanent redirection 301

Nginx Web Server Configuration

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.