Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of Nginx configuration file structure

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains the "method of Nginx configuration file structure". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the method of Nginx configuration file structure".

Nginx was designed and developed by lgor sysoev for the second most visited rambler.ru site in Russia. Since its release in 2004, with the power of open source, it has been close to maturity and perfection.

Nginx has rich functions and can be used as a http server, a reverse proxy server and a mail server. Support fastcgi, ssl, virtual host, url rewrite, gzip and other functions. And support many third-party module extensions.

Nginx's stability, feature set, sample profile and low consumption of system resources make him come from behind, with a usage rate of 12.18% of the world's active websites, about 22.2 million sites.

Common functions of nginx

1. Http proxy, reverse proxy: as one of the most commonly used functions of web server, especially reverse proxy.

Here I give 2 pictures, to the forward agent and response agent to do an interpretation, details, you can refer to the next information.

When doing reverse proxy, nginx provides stable performance and flexible forwarding function. Nginx can adopt different forwarding strategies according to different regular matches, such as the file server at the end of the picture file and the web server for dynamic pages. As long as you can write regularly and have a corresponding server solution, you can play as much as you like. And nginx carries on the error page jump, exception judgment and so on. If there is an exception on the distributed server, he can reforward the request to another server and automatically remove the exception server.

2. Load balancing

There are two kinds of load balancing strategies provided by nginx: built-in strategy and extended strategy. The built-in policies are polling, weighted polling, ip hash. Expansion strategy, only you can not think of nothing he can not do, you can refer to all the load balancing algorithms, give him one by one to find out to do the implementation.

Understand the implementation of these three load balancing algorithms in the above three diagrams

Ip hash algorithm, hash the ip requested by the client, and then distribute the request of the same client ip to the same server for processing according to the hash result, which can solve the problem of session non-sharing.

3. Web cache

Nginx can do different cache processing for different files, the configuration is flexible, and supports fastcgi_cache, mainly used for caching dynamic programs of fastcgi. With the third-party ngx_cache_purge, the content of the url cache can be added or deleted.

4. Nginx related address

II. Nginx configuration file structure

If you have downloaded, your installation file, might as well open the nginx.conf file of the conf folder, the basic configuration of the nginx server, the default configuration is also stored here.

Comment symbol bit # in nginx.conf

The structure of the nginx file, for beginners, you can take a look at it.

Default config

Nginx file structure

... # global block events {# events block...} http # http block {... # http global block server # server block {... # server global block location [pattern] # location block {...} location [pattern] {...} server {.}. # http global block}

1. Global block: configure directives that affect the global nginx. Generally, there are user groups running nginx server, pid storage path of nginx process, log storage path, introduction of configuration files, number of worker process allowed to be generated, and so on.

2. Events block: the configuration affects the nginx server or the network connection with the user. There is a maximum number of connections per process, which event-driven model is selected to handle connection requests, whether multiple network connections are allowed to be accepted at the same time, serialization of multiple network connections is enabled, and so on.

3. Http blocks: you can nest multiple server, configure most functions such as proxy, cache, log definition, and configure third-party modules. Such as file introduction, mime-type definition, log customization, whether to use sendfile to transfer files, connection timeout, the number of requests for a single connection, etc.

4. Server block: configure the relevant parameters of the virtual host. There can be multiple server in a http.

5. Location block: configure the routing of the request and the processing of various pages.

The following is to give you a configuration file, as an understanding, but also into a test machine I built, to give you an example.

# each instruction must end with a semicolon. # user administrator administrators; # configure users or groups. Default is nobody nobody. # worker_processes 2; # the number of processes allowed to be generated. The default is 1#pid / nginx/pid/nginx.pid; # to specify the address where the nginx process runs files to store, error_log log/error.log debug; # to determine the log path and level. This setting can be put into the global block, http block, server block, and the level is as follows: debug | info | notice | warn | error | crit | alert | emergevents {accept_mutex on; # sets the serialization of network connections to prevent spooky phenomena. The default is on multi_accept on; # to set whether a process accepts multiple network connections at the same time. The default is off # use epoll; # event-driven model, select | poll | kqueue | epoll | resig | / dev/poll | eventport worker_connections 1024; # maximum number of connections. Default is 512} 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; # unservice log log_format myformat'$remote_addr-$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; # Custom format access_log log/access.log myformat; # combined is the default sendfile on for log format # allow sendfile to transfer files. The default is off. You can transfer files in http block, server block and location block. Sendfile_max_chunk 100k; # the number of transfers per call per process cannot be greater than the set value. The default is 0, that is, there is no upper limit. Keepalive_timeout 65; # connection timeout. The default is 75s, which can be found in the http,server,location block. Upstream mysvr {server 127.0.0.1server 7878; server 192.168.10.121 backup; # Hot standby} error_page 404 https://www.baidu.com; # error page server {keepalive_requests 120; # maximum number of single connection requests. Listen 4545; # listening port server_name 127.0.0.1; # listening address location ~ * ^. + ${# request url filtering, regular matching, ~ is case-sensitive, ~ * is case-insensitive. # root path; # Root directory # index vv.txt; # set default page proxy_pass http://mysvr; # request to mysvr defined server list deny 127.0.0.1; # rejected ip allow 172.18.5.54; # allowed ip}

Above is the basic configuration of nginx. You should pay attention to the following points:

1. 1.$remote_addr and $http_x_forwarded_for are used to record the ip address of the client; 2.$remote_user: to record the client user name; 3.$time_local: to record the access time and time zone; 4.$request: the url and http protocol used to record the request

5.$status: used to record the status of the request; 6.$body_bytes_s ent: record the size of the main content of the file sent to the client; 7.$http_referer: used to record the access from that page link; 8.$http_user_agent: record the relevant information of the client browser

2. Shock phenomenon: when a network connection arrives, multiple sleep processes are awakened by colleagues, but only one process can get the link, which will affect the performance of the system.

3. Each instruction must end with a semicolon.

Let's take a look at the additions from other netizens:

Here record the nginx server nginx.conf configuration file description, some comments collection and network.

# run the user user www-data; # to start the process, which is usually set to equal the number of cpu 1 to worker_processes # Global error log and pid file error_log / var/log/nginx/error.log;pid / var/run/nginx.pid;# working mode and upper limit of connections events {use epoll # epoll is a way of multiplexing io (I multiplexing), but only for kernels above linux2.6, it can greatly improve the performance of nginx worker_connections 1024 multi_accept on # the maximum number of concurrent links for a single background worker process process } # set http server, use its reverse proxy function to provide load balancing support http {# set mime type, type defined by mime.type file include / etc/nginx/mime.types; default_type application/octet-stream; # set log format access_log / var/log/nginx/access.log The # sendfile instruction specifies whether nginx calls the sendfile function (zero copy mode) to output files. For ordinary applications, # must be set to on, and if it is used for downloading and other application disk io heavy-loaded applications, it can be set to off to balance the processing speed of disk and network iCando and reduce the uptime of the system. Sendfile on; # tcp_nopush on; # connection timeout # keepalive_timeout 0; keepalive_timeout 65; tcp_nodelay on; # enable gzip compression gzip on; gzip_disable "msie [1-6]\. (?!. * sv1)"; # set request buffer client_header_buffer_size 1k; large_client_header_buffers 44k; include / etc/nginx/conf.d/*.conf; include / etc/nginx/sites-enabled/* # set the upstream mysvr {# weigth parameter of the load balancer server list to indicate the weight. The higher the weight, the greater the probability of being assigned # the squid on this machine opens port 3128 server 192.168.8.1 weight=5; server 3128 weight=5; server 192.168.2 weight=5; server 80 weight=1; server 192.168.8.3 upstream mysvr 80 weight=6;} server {# listen on port 80 listen 80 # define using www.xx.com to access server_name www.xx.com; # set the access log of this virtual host access_log logs/www.xx.com.access.log main; # default request location / {root / root; # define the default Web site root location of the server index index.php index.html index.htm; # define the name of the home index file fastcgi_pass www.xx.com Fastcgi_param script_filename $document_root/$fastcgi_script_name; include / etc/nginx/fastcgi_params;} # define the error prompt page error_page 500502503504 / 50x.hml; location = / 50x.html {root/ root;} # static files, nginx handles location ~ ^ / (images | javascript | js | flash | media | static) / {root/ var/www/virtual/htdocs # 30 days after expiration, static files are not updated very often. Expiration can be set to a larger size, or smaller if updated frequently. Expires 30d;} # php script requests are all forwarded to fastcgi for processing. Use the default configuration of fastcgi. Location ~\ .php$ {root / root; fastcgi_pass 127.0.0.1 root; fastcgi_pass 9000; fastcgi_index index.php; fastcgi_param script_filename / home/www/www$fastcgi_script_name; include fastcgi_params;} # set the address location / nginxstatus {stub_status on; access_log on; auth_basic "nginxstatus" for viewing nginx status; auth_basic_user_file conf/htpasswd } # prohibit access to .htxxx file location ~ /\ .ht {deny all;}

These are some basic configurations. The biggest advantage of using nginx is load balancing.

If you want to use load balancer, you can modify and configure the http node as follows:

# set http server, use its reverse proxy function to provide load balancing support http {# set mime type, the type is defined by mime.type file include / etc/nginx/mime.types; default_type application/octet-stream; # set log format access_log / var/log/nginx/access.log; # omit some configuration nodes # above # set the upstream mysvr {# weigth parameter of the load balancer server list to indicate the weight. The higher the weight, the greater the probability of being assigned. The squid on the server 192.168.8.1x:3128 weight=5;# machine opens port 3128 server 192.168.8.2x:80 weight=1; server 192.168.8.3x:80 weight=6;} upstream mysvr2 {# weigth parameter indicates the weight. The higher the weight, the greater the probability of being assigned. Server 192.168.8.x:80 weight=6;} # the first virtual server server {# listens on port 80 listen 80 of 192.168.8.x; server_name 192.168.x; # requests for load balancing of aspx suffixes location ~. *\ .aspx$ {root / root; # defines the default site root location index index.php index.html index.htm of the server # define the name of the home index file proxy_pass http://mysvr; # request is directed to the server list defined by mysvr # here are some reverse proxy configurations that can be deleted. Proxy_redirect off; # backend web server can obtain the user's real ip proxy_set_header host $host; proxy_set_header x-real-ip $remote_addr; proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for; client_max_body_size 10m through x-forwarded-for; # the maximum number of bytes per file allowed to be requested by the client is client_body_buffer_size 128k # maximum number of bytes requested by the buffer proxy buffer, proxy_connect_timeout 90; # nginx connection timeout with the backend server (proxy connection timeout) proxy_send_timeout 90; # backend server data return time (proxy send timeout) proxy_read_timeout 90; # response time of the backend server (proxy receiving timeout) proxy_buffer_size 4k after a successful connection # set the buffer size of proxy server (nginx) to store account information: proxy_buffers 4 32k; # proxy_buffers buffer, if the average page size is less than 32k, set proxy_busy_buffers_size 64k; # buffer size (proxy_buffers*2) proxy_temp_file_write_size 64k under high load # set cache folder size, greater than this value, will be transferred from upstream server} thank you for reading, the above is the content of "method of Nginx configuration file structure". After the study of this article, I believe you have a deeper understanding of the method of Nginx configuration file structure, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report