In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of "what is the structure of nginx configuration file". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "what is the structure of nginx configuration file" can help you solve the problem.
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
Source code:
Official website:
Nginx profile 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
# user nobody;worker_processes 1 the errorless log logs/error.log;#error_log logs/error.log notice;#error_log logs/error.log info;#pid logs/nginx.pid;events {worker_connections 1024;} 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_timeout 0; keepalive_timeout 65; # gzip on; server {listen 80; server_name localhost # charset koi8-r; # access_log logs/host.access.log main; location / {root html; index index.html index.htm;} # error_page 404 / 404.html; # redirect server error pages to the static page / 50x.html # error_page 500502 503504 / 50x.html; location = / 50x.html {root html } # proxy the php scripts to apache listening on 127.0.0.1 location 80 # location ~. Php$ {# proxy_pass http://127.0.0.1; #} # pass the php scripts to fastcgi server listening on 127.0.0.1 proxy_pass 9000 # # location ~. Php$ {# root html; # fastcgi_pass 127.0.1 location 9000; # fastcgi_index index.php # fastcgi_param script_filename / scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if apache's document root # concurs with nginx's one # # location ~ /\ .ht {# deny all; #} # another virtual host using mix of ip-, name-, and port-based configuration # # server {# listen 8000; # listen somename:8080 # server_name somename alias another.alias; # location / {# root html; # index index.html index.htm; #} #} # https server # # server {# listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:ssl:1m; # ssl_session_timeout 5m # ssl_prefer_server_ciphers on; # location / {# root html; # index index.html index.htm; #} #}}
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: debug | info | notice | warn | error | crit | alert | emergevents {accept_mutex on; # sets the serialization of network connections to prevent the occurrence of panic 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 value of log format 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. 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.
This is the end of the introduction to "what is the structure of the nginx configuration file". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
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.