Home > Operation and Maintenance > Nginx > Nginx configuration file structure method

Nginx configuration file structure method

王林
Release: 2023-05-13 08:34:16
forward
1515 people have browsed it

nginx was designed and developed by lgor sysoev for the rambler.ru site, which is the second most visited site in Russia. Since its release in 2004, it has become nearly mature and perfect with the power of open source.

nginx is rich in functions and can be used as an http server, a reverse proxy server, and a mail server. Supports fastcgi, ssl, virtual host, url rewrite, gzip and other functions. And supports many third-party module extensions.

nginx’s stability, feature set, sample configuration files, and low system resource consumption have allowed it to catch up, with a usage rate of 12.18% of the world’s active websites, approximately 22.2 million websites.

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 structure method

#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 structure method

#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 structure method

3. Web caching

nginx can perform different cache processing on different files, with flexible configuration and support for fastcgi_cache. Mainly used to cache fastcgi dynamic programs. 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

2. nginx configuration file structure
If you have downloaded it, 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

nginx file structure

...    #全局块

events {   #events块
 ...
}

http  #http块
{
 ... #http全局块
 server  #server块
 { 
  ...  #server全局块
  location [pattern] #location块
  {
   ...
  }
  location [pattern] 
  {
   ...
  }
 }
 server
 {
  ...
 }
 ...  #http全局块
}
Copy after login

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   
  } 
 }
}
Copy after login

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 client Browser related information;

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、每个指令必须有分号结束。

下面看下其他网友的补充:

在此记录下nginx服务器nginx.conf的配置文件说明, 部分注释收集与网络.

#运行用户
user www-data;  
#启动进程,通常设置成和cpu的数量相等
worker_processes 1;

#全局错误日志及pid文件
error_log /var/log/nginx/error.log;
pid    /var/run/nginx.pid;

#工作模式及连接数上限
events {
  use  epoll;       #epoll是多路复用io(i/o multiplexing)中的一种方式,但是仅用于linux2.6以上内核,可以大大提高nginx的性能
  worker_connections 1024;#单个后台worker process进程的最大并发链接数
  # multi_accept on; 
}

#设定http服务器,利用它的反向代理功能提供负载均衡支持
http {
   #设定mime类型,类型由mime.type文件定义
  include    /etc/nginx/mime.types;
  default_type application/octet-stream;
  #设定日志格式
  access_log  /var/log/nginx/access.log;

  #sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用,
  #必须设为 on,如果用来进行下载等应用磁盘io重负载应用,可设置为 off,以平衡磁盘与网络i/o处理速度,降低系统的uptime.
  sendfile    on;
  #tcp_nopush   on;

  #连接超时时间
  #keepalive_timeout 0;
  keepalive_timeout 65;
  tcp_nodelay    on;
  
  #开启gzip压缩
  gzip on;
  gzip_disable "msie [1-6]\.(?!.*sv1)";

  #设定请求缓冲
  client_header_buffer_size  1k;
  large_client_header_buffers 4 4k;

  include /etc/nginx/conf.d/*.conf;
  include /etc/nginx/sites-enabled/*;

  #设定负载均衡的服务器列表
   upstream mysvr {
  #weigth参数表示权值,权值越高被分配到的几率越大
  #本机上的squid开启3128端口
  server 192.168.8.1:3128 weight=5;
  server 192.168.8.2:80 weight=1;
  server 192.168.8.3:80 weight=6;
  }


  server {
  #侦听80端口
    listen    80;
    #定义使用www.xx.com访问
    server_name www.xx.com;

    #设定本虚拟主机的访问日志
    access_log logs/www.xx.com.access.log main;

  #默认请求
  location / {
     root  /root;   #定义服务器的默认网站根目录位置
     index index.php index.html index.htm;  #定义首页索引文件的名称

     fastcgi_pass www.xx.com;
     fastcgi_param script_filename $document_root/$fastcgi_script_name; 
     include /etc/nginx/fastcgi_params;
    }

  # 定义错误提示页面
  error_page  500 502 503 504 /50x.html; 
    location = /50x.html {
    root  /root;
  }

  #静态文件,nginx自己处理
  location ~ ^/(images|javascript|js|css|flash|media|static)/ {
    root /var/www/virtual/htdocs;
    #过期30天,静态文件不怎么更新,过期可以设大一点,如果频繁更新,则可以设置得小一点。
    expires 30d;
  }
  #php 脚本请求全部转发到 fastcgi处理. 使用fastcgi默认配置.
  location ~ \.php$ {
    root /root;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param script_filename /home/www/www$fastcgi_script_name;
    include fastcgi_params;
  }
  #设定查看nginx状态的地址
  location /nginxstatus {
    stub_status      on;
    access_log       on;
    auth_basic       "nginxstatus";
    auth_basic_user_file conf/htpasswd;
  }
  #禁止访问 .htxxx 文件
  location ~ /\.ht {
    deny all;
  }
   
   }
}
Copy after login

以上是一些基本的配置,使用nginx最大的好处就是负载均衡

如果要使用负载均衡的话,可以修改配置http节点如下:

#设定http服务器,利用它的反向代理功能提供负载均衡支持
http {
   #设定mime类型,类型由mime.type文件定义
  include    /etc/nginx/mime.types;
  default_type application/octet-stream;
  #设定日志格式
  access_log  /var/log/nginx/access.log;

  #省略上文有的一些配置节点

  #。。。。。。。。。。

  #设定负载均衡的服务器列表
   upstream mysvr {
  #weigth参数表示权值,权值越高被分配到的几率越大
  server 192.168.8.1x:3128 weight=5;#本机上的squid开启3128端口
  server 192.168.8.2x:80 weight=1;
  server 192.168.8.3x:80 weight=6;
  }

  upstream mysvr2 {
  #weigth参数表示权值,权值越高被分配到的几率越大

  server 192.168.8.x:80 weight=1;
  server 192.168.8.x:80 weight=6;
  }

  #第一个虚拟服务器
  server {
  #侦听192.168.8.x的80端口
    listen    80;
    server_name 192.168.8.x;

   #对aspx后缀的进行负载均衡请求
  location ~ .*\.aspx$ {

     root  /root;   #定义服务器的默认网站根目录位置
     index index.php index.html index.htm;  #定义首页索引文件的名称

     proxy_pass http://mysvr ;#请求转向mysvr 定义的服务器列表

     #以下是一些反向代理的配置可删除.

     proxy_redirect off;

     #后端的web服务器可以通过x-forwarded-for获取用户真实ip
     proxy_set_header host $host;
     proxy_set_header x-real-ip $remote_addr;
     proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;
     client_max_body_size 10m;  #允许客户端请求的最大单文件字节数
     client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数,
     proxy_connect_timeout 90; #nginx跟后端服务器连接超时时间(代理连接超时)
     proxy_send_timeout 90;    #后端服务器数据回传时间(代理发送超时)
     proxy_read_timeout 90;     #连接成功后,后端服务器响应时间(代理接收超时)
     proxy_buffer_size 4k;       #设置代理服务器(nginx)保存用户头信息的缓冲区大小
     proxy_buffers 4 32k;        #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
     proxy_busy_buffers_size 64k;  #高负荷下缓冲大小(proxy_buffers*2)
     proxy_temp_file_write_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传

    }

   }
}
Copy after login

The above is the detailed content of Nginx configuration file structure method. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template