亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Home Operation and Maintenance Nginx Nginx configuration file example analysis

Nginx configuration file example analysis

May 17, 2023 pm 07:25 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.

Nginx configuration file example analysis

#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

Nginx configuration file example analysis

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

Nginx configuration file example analysis

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.

########### 每個指令必須有分號結(jié)束。#################
#user administrator administrators; #配置用戶或者組,默認(rèn)為nobody nobody。
#worker_processes 2; #允許生成的進程數(shù),默認(rèn)為1
#pid /nginx/pid/nginx.pid;  #指定nginx進程運行文件存放地址
error_log log/error.log debug; #制定日志路徑,級別。這個設(shè)置可以放入全局塊,http塊,server塊,級別以此為: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è)置一個進程是否同時接受多個網(wǎng)絡(luò)連接,默認(rèn)為off
  #use epoll;   #事件驅(qū)動模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
  worker_connections 1024;  #最大連接數(shù),默認(rèn)為512
}
http {
  include    mime.types;  #文件擴展名與文件類型映射表
  default_type application/octet-stream; #默認(rèn)文件類型,默認(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; #每個進程每次調(diào)用傳輸數(shù)量不能大于設(shè)定的值,默認(rèn)為0,即不設(shè)上限。
  keepalive_timeout 65; #連接超時時間,默認(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; #錯誤頁
  server {
    keepalive_requests 120; #單連接請求上限次數(shù)。
    listen    4545;  #監(jiān)聽端口
    server_name 127.0.0.1;  #監(jiān)聽地址    
    location ~*^.+$ {    #請求的url過濾,正則匹配,~為區(qū)分大小寫,~*為不區(qū)分大小寫。
      #root path; #根目錄
      #index vv.txt; #設(shè)置默認(rèn)頁
      proxy_pass http://mysvr; #請求轉(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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

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.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1488
72
NGINX vs. Apache: Performance, Scalability, and Efficiency NGINX vs. Apache: Performance, Scalability, and Efficiency Apr 19, 2025 am 12:05 AM

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.

NGINX vs. Apache: A Comparative Analysis of Web Servers NGINX vs. Apache: A Comparative Analysis of Web Servers Apr 21, 2025 am 12:08 AM

NGINX is more suitable for handling high concurrent connections, while Apache is more suitable for scenarios where complex configurations and module extensions are required. 1.NGINX is known for its high performance and low resource consumption, and is suitable for high concurrency. 2.Apache is known for its stability and rich module extensions, which are suitable for complex configuration needs.

NGINX and Apache: Understanding the Key Differences NGINX and Apache: Understanding the Key Differences Apr 26, 2025 am 12:01 AM

NGINX and Apache each have their own advantages and disadvantages, and the choice should be based on specific needs. 1.NGINX is suitable for high concurrency scenarios because of its asynchronous non-blocking architecture. 2. Apache is suitable for low-concurrency scenarios that require complex configurations, because of its modular design.

How to execute php code after writing php code? Several common ways to execute php code How to execute php code after writing php code? Several common ways to execute php code May 23, 2025 pm 08:33 PM

PHP code can be executed in many ways: 1. Use the command line to directly enter the "php file name" to execute the script; 2. Put the file into the document root directory and access it through the browser through the web server; 3. Run it in the IDE and use the built-in debugging tool; 4. Use the online PHP sandbox or code execution platform for testing.

After installing Nginx, the configuration file path and initial settings After installing Nginx, the configuration file path and initial settings May 16, 2025 pm 10:54 PM

Understanding Nginx's configuration file path and initial settings is very important because it is the first step in optimizing and managing a web server. 1) The configuration file path is usually /etc/nginx/nginx.conf. The syntax can be found and tested using the nginx-t command. 2) The initial settings include global settings (such as user, worker_processes) and HTTP settings (such as include, log_format). These settings allow customization and extension according to requirements. Incorrect configuration may lead to performance issues and security vulnerabilities.

How to limit user resources in Linux? How to configure ulimit? How to limit user resources in Linux? How to configure ulimit? May 29, 2025 pm 11:09 PM

Linux system restricts user resources through the ulimit command to prevent excessive use of resources. 1.ulimit is a built-in shell command that can limit the number of file descriptors (-n), memory size (-v), thread count (-u), etc., which are divided into soft limit (current effective value) and hard limit (maximum upper limit). 2. Use the ulimit command directly for temporary modification, such as ulimit-n2048, but it is only valid for the current session. 3. For permanent effect, you need to modify /etc/security/limits.conf and PAM configuration files, and add sessionrequiredpam_limits.so. 4. The systemd service needs to set Lim in the unit file

What are the Debian Nginx configuration skills? What are the Debian Nginx configuration skills? May 29, 2025 pm 11:06 PM

When configuring Nginx on Debian system, the following are some practical tips: The basic structure of the configuration file global settings: Define behavioral parameters that affect the entire Nginx service, such as the number of worker threads and the permissions of running users. Event handling part: Deciding how Nginx deals with network connections is a key configuration for improving performance. HTTP service part: contains a large number of settings related to HTTP service, and can embed multiple servers and location blocks. Core configuration options worker_connections: Define the maximum number of connections that each worker thread can handle, usually set to 1024. multi_accept: Activate the multi-connection reception mode and enhance the ability of concurrent processing. s

NGINX's Purpose: Serving Web Content and More NGINX's Purpose: Serving Web Content and More May 08, 2025 am 12:07 AM

NGINXserveswebcontentandactsasareverseproxy,loadbalancer,andmore.1)ItefficientlyservesstaticcontentlikeHTMLandimages.2)Itfunctionsasareverseproxyandloadbalancer,distributingtrafficacrossservers.3)NGINXenhancesperformancethroughcaching.4)Itofferssecur

See all articles