search
HomeOperation and MaintenanceNginxWhat are the common functions of Nginx?

What are the common functions of Nginx?

May 18, 2023 pm 10:31 PM
nginx

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.

What are the common functions of Nginx?

#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

What are the common functions of Nginx?

#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.

What are the common functions of Nginx?

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!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
Choosing Between NGINX and Apache: The Right Fit for Your NeedsChoosing Between NGINX and Apache: The Right Fit for Your NeedsApr 15, 2025 am 12:04 AM

NGINX and Apache have their own advantages and disadvantages and are suitable for different scenarios. 1.NGINX is suitable for high concurrency and low resource consumption scenarios. 2. Apache is suitable for scenarios where complex configurations and rich modules are required. By comparing their core features, performance differences, and best practices, you can help you choose the server software that best suits your needs.

How to start nginxHow to start nginxApr 14, 2025 pm 01:06 PM

Question: How to start Nginx? Answer: Install Nginx Startup Nginx Verification Nginx Is Nginx Started Explore other startup options Automatically start Nginx

How to check whether nginx is startedHow to check whether nginx is startedApr 14, 2025 pm 01:03 PM

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

How to close nginxHow to close nginxApr 14, 2025 pm 01:00 PM

To shut down the Nginx service, follow these steps: Determine the installation type: Red Hat/CentOS (systemctl status nginx) or Debian/Ubuntu (service nginx status) Stop the service: Red Hat/CentOS (systemctl stop nginx) or Debian/Ubuntu (service nginx stop) Disable automatic startup (optional): Red Hat/CentOS (systemctl disabled nginx) or Debian/Ubuntu (syst

How to configure nginx in WindowsHow to configure nginx in WindowsApr 14, 2025 pm 12:57 PM

How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

How to solve nginx403 errorHow to solve nginx403 errorApr 14, 2025 pm 12:54 PM

The server does not have permission to access the requested resource, resulting in a nginx 403 error. Solutions include: Check file permissions. Check the .htaccess configuration. Check nginx configuration. Configure SELinux permissions. Check the firewall rules. Troubleshoot other causes such as browser problems, server failures, or other possible errors.

How to start nginx in LinuxHow to start nginx in LinuxApr 14, 2025 pm 12:51 PM

Steps to start Nginx in Linux: Check whether Nginx is installed. Use systemctl start nginx to start the Nginx service. Use systemctl enable nginx to enable automatic startup of Nginx at system startup. Use systemctl status nginx to verify that the startup is successful. Visit http://localhost in a web browser to view the default welcome page.

How to check whether nginx is started?How to check whether nginx is started?Apr 14, 2025 pm 12:48 PM

In Linux, use the following command to check whether Nginx is started: systemctl status nginx judges based on the command output: If "Active: active (running)" is displayed, Nginx is started. If "Active: inactive (dead)" is displayed, Nginx is stopped.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.