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.
########### 每個(gè)指令必須有分號(hào)結(jié)束。################# #user administrator administrators; #配置用戶(hù)或者組,默認(rèn)為nobody nobody。 #worker_processes 2; #允許生成的進(jìn)程數(shù),默認(rèn)為1 #pid /nginx/pid/nginx.pid; #指定nginx進(jìn)程運(yùn)行文件存放地址 error_log log/error.log debug; #制定日志路徑,級(jí)別。這個(gè)設(shè)置可以放入全局塊,http塊,server塊,級(jí)別以此為:debug|info|notice|warn|error|crit|alert|emerg events { accept_mutex on; #設(shè)置網(wǎng)路連接序列化,防止驚群現(xiàn)象發(fā)生,默認(rèn)為on multi_accept on; #設(shè)置一個(gè)進(jìn)程是否同時(shí)接受多個(gè)網(wǎng)絡(luò)連接,默認(rèn)為off #use epoll; #事件驅(qū)動(dòng)模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport worker_connections 1024; #最大連接數(shù),默認(rèn)為512 } http { include mime.types; #文件擴(kuò)展名與文件類(lèi)型映射表 default_type application/octet-stream; #默認(rèn)文件類(lèi)型,默認(rèn)為text/plain #access_log off; #取消服務(wù)日志 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為日志格式的默認(rèn)值 sendfile on; #允許sendfile方式傳輸文件,默認(rèn)為off,可以在http塊,server塊,location塊。 sendfile_max_chunk 100k; #每個(gè)進(jìn)程每次調(diào)用傳輸數(shù)量不能大于設(shè)定的值,默認(rèn)為0,即不設(shè)上限。 keepalive_timeout 65; #連接超時(shí)時(shí)間,默認(rèn)為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; #錯(cuò)誤頁(yè) server { keepalive_requests 120; #單連接請(qǐng)求上限次數(shù)。 listen 4545; #監(jiān)聽(tīng)端口 server_name 127.0.0.1; #監(jiān)聽(tīng)地址 location ~*^.+$ { #請(qǐng)求的url過(guò)濾,正則匹配,~為區(qū)分大小寫(xiě),~*為不區(qū)分大小寫(xiě)。 #root path; #根目錄 #index vv.txt; #設(shè)置默認(rèn)頁(yè) proxy_pass http://mysvr; #請(qǐng)求轉(zhuǎn)向mysvr 定義的服務(wù)器列表 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 Nginx configuration file example analysis. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

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.

Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]

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

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.

Practical Tips for Improving PhpStorm Performance in CentOS Systems This article provides a variety of methods to help you optimize the performance of PhpStorm in CentOS systems and thus improve development efficiency. Before implementing any optimization measures, be sure to back up important data and verify the results in the test environment. 1. System-level optimization and streamline system services: Disable unnecessary system services and daemons to reduce system resource usage. Interfaceless Mode: Switching to interfaceless mode can significantly save resources if you do not need a graphical interface. Uninstall redundant software: Remove software packages and services that are no longer in use and free up system resources. 2. PHP configuration optimization enable OPcache: install and configure OPcache extensions to display

NGINX and Apache are both powerful web servers, each with unique advantages and disadvantages in terms of performance, scalability and efficiency. 1) NGINX performs well when handling static content and reverse proxying, suitable for high concurrency scenarios. 2) Apache performs better when processing dynamic content and is suitable for projects that require rich module support. The selection of a server should be decided based on project requirements and scenarios.
