nginx common functions
1. http proxy, reverse proxy: As one of the most commonly used functions of web servers, especially reverse proxy.
Here I will give you two pictures to explain the positive agent and the reactive agent. You can read the information for the specific details.
#nginx provides stable performance when used as a reverse proxy, and can provide forwarding functions with flexible configuration. nginx can adopt different forwarding strategies based on different regular matching, such as going to the file server at the end of the image file, and going to the web server for dynamic pages. As long as you have no problem writing regular rules and have corresponding server solutions, you can do whatever you want. of play. And nginx performs error page jump, exception judgment, etc. on the returned results. If the distributed server is abnormal, he can re-forward the request to another server and then automatically remove the abnormal server.
2. Load balancing
nginx provides two types of load balancing strategies: built-in strategies and extended strategies. The built-in strategies are polling, weighted polling, and ip hash. The expansion strategy is as imaginative as it is unimaginable. There is nothing you can't think of that it can't do. You can refer to all the load balancing algorithms to find them one by one and implement them.
The above three pictures, understand the implementation of these three load balancing algorithms
#ip hash algorithm, perform a hash operation on the IP requested by the client, and then Distributing requests from the same client IP to the same server for processing based on the hash result can solve the problem of session not being shared.
3. Web caching
nginx can perform different cache processing on different files, with flexible configuration, and supports fastcgi_cache, which is mainly used for fastcgi Dynamic programs are cached. Cooperating with the third-party ngx_cache_purge, the specified URL cache content can be added and deleted to be managed.
4. nginx related address
Source code:
Official website:
nginx configuration file structure
If Now that you have downloaded your installation file, you might as well open the nginx.conf file in the conf folder. The basic configuration of the nginx server and the default configuration are also stored here.
In the comment symbol bit of nginx.conf
#The structure of the nginx file, students who are just getting started can take a second look at this.
Default config
#user nobody; worker_processes 1; #error_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 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the php scripts to apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the php scripts to fastcgi server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1: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_ciphers high:!anull:!md5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }
nginx file structure
... #全局块 events { #events块 ... } http #http块 { ... #http全局块 server #server块 { ... #server全局块 location [pattern] #location块 { ... } location [pattern] { ... } } server { ... } ... #http全局块 }
1. Global block: Configure instructions that affect nginx globally. Generally, there is a user group for running the nginx server, nginx process pid storage path, log storage path, configuration file introduction, the number of worker processes allowed to be generated, etc.
2. Events block: 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 to choose to handle connection requests, whether to allow multiple network connections to be accepted at the same time, enable serialization of multiple network connections, etc.
3. http block: You can nest multiple servers, configure proxies, caches, log definitions and other most functions and third-party module configurations. Such as file introduction, mime-type definition, log customization, whether to use sendfile to transfer files, connection timeout, number of single connection requests, etc.
4. Server block: Configure the relevant parameters of the virtual host. There can be multiple servers in one http.
5. Location block: Configure the routing of requests and the processing of various pages.
The following is a configuration file for everyone, for understanding, and it is also installed into a test machine I built to give you an example.
########### 每个指令必须有分号结束。################# #user administrator administrators; #配置用户或者组,默认为nobody nobody。 #worker_processes 2; #允许生成的进程数,默认为1 #pid /nginx/pid/nginx.pid; #指定nginx进程运行文件存放地址 error_log log/error.log debug; #制定日志路径,级别。这个设置可以放入全局块,http块,server块,级别以此为:debug|info|notice|warn|error|crit|alert|emerg events { accept_mutex on; #设置网路连接序列化,防止惊群现象发生,默认为on multi_accept on; #设置一个进程是否同时接受多个网络连接,默认为off #use epoll; #事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport worker_connections 1024; #最大连接数,默认为512 } http { include mime.types; #文件扩展名与文件类型映射表 default_type application/octet-stream; #默认文件类型,默认为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方式传输文件,默认为off,可以在http块,server块,location块。 sendfile_max_chunk 100k; #每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。 keepalive_timeout 65; #连接超时时间,默认为75s,可以在http,server,location块。 upstream mysvr { server 127.0.0.1:7878; server 192.168.10.121:3333 backup; #热备 } error_page 404 https://www.baidu.com; #错误页 server { keepalive_requests 120; #单连接请求上限次数。 listen 4545; #监听端口 server_name 127.0.0.1; #监听地址 location ~*^.+$ { #请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。 #root path; #根目录 #index vv.txt; #设置默认页 proxy_pass http://mysvr; #请求转向mysvr 定义的服务器列表 deny 127.0.0.1; #拒绝的ip allow 172.18.5.54; #允许的ip } } }
The above is the basic configuration of nginx. You need to pay attention to the following points:
1. 1.$remote_addr and $http_x_forwarded_for are used to record the client’s ip address. ; 2.$remote_user: used to record the client user name; 3.$time_local: used to record the access time and time zone; 4.$request: used to record the requested url and http protocol;
5. $status: Used to record the request status; success is 200, 6.$body_bytes_s ent: Record the size of the body content of the file sent to the client; 7.$http_referer: Used to record the page link accessed from; 8.$http_user_agent: Record the relevant information of the client browser;
2. Thundering herd phenomenon: when a network connection arrives, multiple sleeping processes are awakened by colleagues, but only one process can obtain the link, which will affect system performance .
3. Each instruction must end with a semicolon.
The above is the detailed content of What are the common functions of Nginx?. For more information, please follow other related articles on the PHP Chinese website!