In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you what is the common configuration of Nginx, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Common configuration items
In our work, we deal with Nginx more through its configuration files. Then it is necessary to master the respective roles of these configuration items. (recommended: Linux tutorial)
First of all, the content of nginx.conf usually looks like this:
... # Core Touch Block events {# event Module...} http {# http Module server {# server Block location [PATTERN] {# location Block...} location [PATTERN] {...} server { ...} mail {# mail module server {# server block...}}
Let's take a look at the general configuration items for each module in turn.
Core module
User admin; # configure user or group worker_processes 4; # number of processes allowed to be generated, default is 1 pid/nginx / pid/nginx.pid; # specify nginx process running file storage address error_log log/error.log debug; # error log path, level
Event module
Events {accept_mutex on; # sets the serialization of network connections to prevent panic. The default is on multi_accept on; # to set whether a process accepts multiple network connections at the same time, and the default is off use epoll; # event-driven model select | poll | kqueue | epoll | resig worker_connections 1024; # maximum number of connections, default is 512}
Http module
Http {include mime.types; # File extension and File Type Mapping Table default_type application/octet-stream; # default file type, default is text/plain access_log off; # cancel service log sendfile on; # allows sendfile to transfer files, default is off, can be in http block, server block, location block sendfile_max_chunk 100k # the number of transfers per call per process cannot be greater than the set value, default is 0, that is, no upper limit is set keepalive_timeout 65; # connection timeout, default is 75s, can be server {keepalive_requests 120 in http,server,location block; # maximum number of requests for a single connection listen 80; # listening port server_name 127.0.0.1 # listening address index index.html index.htm index.php; root your_path; # root directory location ~\ .php$ {fastcgi_pass unix:/var/run/php/php7.1-fpm.sock; # fastcgi_pass 127.0.0.1 php$ 9000; fastcgi_index index.php Include fastcgi_params;}
Configuration item resolution
Worker_processes
The number of processes used by worker_processes to set the Nginx service. The number of CPU kernels is recommended for this value.
Worker_cpu_affinity*
Worker\ _ cpu\ _ affinity is used to assign the working kernel of CPU to each process. The parameters are represented by multiple binary values, each group represents a process, and each member in each group represents the situation in which the process uses CPU, 1 for use and 0 for no use. So we use worker\ _ cpu\ _ affinity0001001001001000; to bind processes to different cores. By default, the worker process is not bound to any CPU.
Worker_rlimit_nofile
Sets the maximum number of file openings for each process. If not, the upper limit is the number of ulimit-n of the system, which is usually 65535.
Worker_connections
Set the maximum number of connections allowed by a process theory, which is theoretically as large as possible, but cannot exceed the value of worker_rlimit_nofile.
Use epoll
Set up the event-driven model to use epoll. Epoll is one of the high performance event-driven libraries supported by Nginx. It is recognized as an excellent event-driven model.
Accept_mutex off
Turn off network connection serialization. When it is set to on, multiple Nginx processes will be serialized to accept connections to prevent multiple processes from scrambling for connections. When the number of server connections is small, turning on this parameter will reduce the load to a certain extent. But when the server's throughput is high, turn this parameter off for efficiency; and turning it off also allows requests to be distributed more evenly across multiple worker. So we set up accept_mutex off;.
Multi_accept on
Set up a process to accept multiple network connections at the same time.
Sendfile on
Sendfile is a system call introduced by Linux2.0, which can simplify the steps in the process of network transmission and improve the performance of the server.
Traditional network transmission process without sendfile:
Hard disk > > kernel buffer > > user buffer > > kernel socket buffer > > Protocol Stack
The process of using sendfile () for network transmission:
Hard disk > > kernel buffer (quick copy to kernelsocket buffer) > > protocol stack
Tcp_nopush on
Setting up packets will accumulate and then transmit together, which can improve some transmission efficiency. Tcp_nopush must be used with sendfile.
Tcp_nodelay on
Small packets do not wait for direct transmission. The default is on. appears to be the opposite of tcp_nopush, but nginx can also balance the use of these two functions when both sides are on.
Keepalive_timeout
The duration of the HTTP connection. Setting it too long will make too many useless threads. This is based on the number of server visits, processing speed, and the state of the network.
Send_timeout
Set the timeout for the Nginx server to respond to the client, which is only for the time between the two clients and the server after establishing a connection. If the client has no activity after this time, the Nginx server will close the connection.
Gzip on
Enable gzip to compress the response data online in real time and reduce the amount of data transmission.
Gzip_disable "msie6"
In response to these kinds of client requests, the Nginx server does not use the Gzip function to cache application data, and gzip_disable "msie6" does not compress the data of IE6 browsers with GZIP.
The commonly used configuration items are roughly these. For different business scenarios, some additional configuration items are required, which will not be expanded here.
Other
Location is included in the http configuration, which is used to match the corresponding processing rules according to the uri in the request.
Location lookup rules
Location = / {# exact match /, the hostname cannot be followed by any string [config A]} location / {# because all addresses begin with /, so this rule will match all requests # but regular and longest strings will match [config B]} location / documents/ {# to match any address that starts with / documents/, matching later Continue to search # this line will use this [config C]} location ~ / documents/Abc {# to match any address that begins with / documents/Abc only if the following regular expression does not match. After the match, continue to search # when only the following regular expression does not match This line will use this [config CC]} location ^ ~ / images/ {# to match any address that begins with / images/. After the match, stop searching for regularities, and use this [config D]} location ~ *\. (gif | jpg | jpeg) ${# matches all requests ending in gif,jpg or jpeg # however, all images under the request / images/ will be processed by config D. Because ^ ~ cannot reach this regular [config E]} location / images/ {# character match to / images/, continue, you will find that ^ ~ exists [config F]} location / images/abc {# longest character match to / images/abc, continue to You will find that ^ ~ exists # F has nothing to do with the order in which G is placed. [config G]} location ~ / images/abc/ {# is valid only if config D is removed: first, the longest matching address at the beginning of config G, continue to search, match this rule, and use [config H]}.
The priority of regular lookup from high to low is as follows:
The beginning of "=" indicates an exact match, such as An only matches requests at the end of the root directory, and cannot be followed by any strings.
The beginning of "^ ~" means that uri begins with a regular string and is not a regular match.
The beginning of "~" indicates a case-sensitive regular match.
The beginning of "~ *" indicates a case-insensitive regular match.
"/" Universal match, if there is no other match, any request will match.
Load balancing configuration
Nginx load balancer needs to use upstream module, which can be realized through the following configuration:
Upstream test-upstream {ip_hash; # uses the ip_hash algorithm to assign server 192.168.1.1; # ip server 192.168.1.2;} server {location / {proxy_pass http://test-upstream;}}
The above example defines a test-upstream load balancing configuration that forwards the request to the module for allocation processing through the proxy_pass reverse proxy instruction.
The above are all the contents of what the common configuration items of Nginx are. Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.