Table of Contents
Nginx basic configuration files and variables Detailed explanation
1. Basic configuration file
/etc/nginx/nginx.conf
2. Nginx variables
HTTP request variables
Home Backend Development PHP Tutorial Analysis of Nginx basic configuration files and variables

Analysis of Nginx basic configuration files and variables

Jul 11, 2018 pm 02:38 PM
linux nginx php

This article mainly introduces the analysis of Nginx basic configuration files and variables. It has certain reference value. Now I share it with everyone. Friends in need can refer to it

Nginx basic configuration files and variables Detailed explanation

1. Basic configuration file

/etc/nginx/nginx.conf

# nginx运行的用户
user  nginx;
# nginx进程数,建议设置为等于CPU总核心数。
worker_processes  1;

# 全局错误日志文件名称和所在目录,错误日志记录级别[ debug | info | notice | warn | error | crit ]
error_log  /var/log/nginx/error.log warn;
# 进程文件nginx.pid所在目录
pid        /var/run/nginx.pid;

# 一个nginx进程打开的最多文件描述符数目,
# 理论值应该是最多打开文件数(系统的值ulimit -n)与nginx进程数相除,
# 但是nginx分配请求并不均匀,所以建议与ulimit -n的值保持一致。
# worker_rlimit_nofile 65535;

events {
    # 参考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; 
    # epoll模型是Linux 2.6以上版本内核中的高性能网络I/O模型,如果跑在FreeBSD上面,就用kqueue模型。
    # use epoll;

    # 单个进程最大连接数(最大连接数 = 连接数 * 进程数)
    worker_connections  1024;
    
    # multi_accept on;
}

# http服务器配置
http {
    # 引入http协议的Content-Type与扩展名对应关系的文件
    include       /etc/nginx/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"';
    
    # HTTP服务器请求日志文件的名称和所在目录、日志记录级别
    access_log  /var/log/nginx/access.log  main;

    # 开启高效文件传输模式。
    # sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为on,
    # 如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。
    # 注意:如果图片显示不正常把这个改成off。
    sendfile        on;
    # 防止网络阻塞
    # tcp_nopush    on;
    # tcp_nodelay   on;

    # 开启目录列表访问,合适下载服务器,默认关闭。
    # autoindex on; 
    
    # 开启限制IP连接数的时候需要使用
    # limit_zone crawler $binary_remote_addr 10m; 
    
    # 长连接超时时间,单位是秒
    keepalive_timeout  65;

    # gzip模块设置
    # 开启gzip压缩输出
    # gzip  on;
    # 最小压缩文件大小
    # gzip_min_length 1k; 
    # 压缩缓冲区
    # gzip_buffers 4 16k; 
    # 压缩版本(默认1.1,前端如果是squid2.5请使用1.0)
    # gzip_http_version 1.0; 
    # 压缩等级
    # gzip_comp_level 2; 
    # 压缩类型,默认就已经包含text/html,所以下面就不用再写了,写上去也不会有问题,但是会有一个warn。
    # gzip_types text/plain application/x-javascript text/css application/xml;
    # gzip_vary on;
        
     
    # FastCGI相关参数是为了改善网站的性能:减少资源占用,提高访问速度。
    # fastcgi_connect_timeout 300;
    # fastcgi_send_timeout 300;
    # fastcgi_read_timeout 300;
    # fastcgi_buffer_size 128k;
    # fastcgi_buffers 8 128k;
    # fastcgi_busy_buffers_size 128k;
    # fastcgi_temp_file_write_size 128k;

    # 允许客户端请求的最大单文件字节数。 
    # 如果请求大于指定的大小,则NGINX发回HTTP 413(Request Entity too large)错误。 
    # 如果服务器处理大文件上传,则该指令非常重要。
    # client_max_body_size 20m;
    # 用于请求主体的缓冲区大小。 
    # 如果主体超过缓冲区大小,则完整主体或其一部分将写入临时文件。 
    # 如果NGINX配置为使用文件而不是内存缓冲区,则该指令会被忽略。 
    # 默认情况下,该指令为32位系统设置一个8k缓冲区,为64位系统设置一个16k缓冲区。 
    # 该指令在NGINX配置的http,server和location区块使用。
    # client_body_buffer_size 128k;
    
    # upstream abc.com {
        # upstream的负载均衡,weight是权重,可以根据机器配置定义权重。weigth参数表示权值,权值越高被分配到的几率越大。
    #     server 192.168.10.121:80 weight=3;
    #     server 192.168.10.122:80 weight=2;
    #     server 192.168.10.123:80 weight=3;
    # }
    
    # 引入 '/etc/nginx/conf.d/' 目录下以 '.conf' 结尾的文件
    include /etc/nginx/conf.d/*.conf;
    
    # 虚拟主机的配置
    server {
        # 监听端口
        listen       80;
        # 域名,可以有多个,用空格隔开
        server_name  localhost;
        
        # 默认编码
        # charset utf-8; 
        
        # 日志格式设定
        # log_format access '$remote_addr - $remote_user [$time_local] "$request" '
        # '$status $body_bytes_sent "$http_referer" '
        # '"$http_user_agent" $http_x_forwarded_for';
        # 当前虚拟主机请求日志文件的名称和所在目录、日志记录级别
        # access_log  /var/log/nginx/host.access.log  main;

        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }
    
        #error_page  404              /404.html;
    
        # 将服务器错误页面重定向到静态页面 /50x.html
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
    
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        # 将 PHP 脚本反向代理到监听 127.0.0.1:80 端口的Apache
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}
    
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        # 将 PHP 脚本反向代理到监听127.0.0.1:9000端口的FastCGI服务器
        #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;
        #}
    
        # 如果Apache的文档根目录与nginx的相同,拒绝访问.htaccess文件。
        #location ~ /\.ht {
        #    deny  all;
        #}
        
        # 图片缓存时间设置
        # location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
        #     expires 10d;
        # }
        
        # JS和CSS缓存时间设置
        # location ~ .*\.(js|css)?$ {
        #     expires 1h;
        # }
        
        # 对 "/" 启用反向代理
        # location / {
        #     proxy_pass http://127.0.0.1:88;
        #     proxy_redirect off;
        #     # 后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
        #     proxy_set_header X-Real-IP $remote_addr;
        #     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        #     # 以下是一些反向代理的配置,可选。
        #     proxy_set_header Host $host;
        #     # nginx跟后端服务器连接超时时间(代理连接超时)
        #     proxy_connect_timeout 90; 
        #     # 后端服务器数据回传时间(代理发送超时)
        #     proxy_send_timeout 90; 
        #     # 连接成功后,后端服务器响应时间(代理接收超时)
        #     proxy_read_timeout 90; 
        #     # 设置代理服务器(nginx)保存用户头信息的缓冲区大小
        #     proxy_buffer_size 4k; 
        #     # proxy_buffers缓冲区,网页平均在32k以下的设置
        #     proxy_buffers 4 32k; 
        #     # 高负荷下缓冲大小(proxy_buffers*2)
        #     proxy_busy_buffers_size 64k; 
        #     # 设定缓存文件夹大小,大于这个值,将从upstream服务器传
        #     proxy_temp_file_write_size 64k;
        # }
    }
}
Copy after login

2. Nginx variables

HTTP request variables

  • arg_parameter name: For example, $arg_userid can refer to the value of the request parameter userid

  • http_Request HEADER name: For example, $http_user_agent can refer to the value of the request header information User-Agent

  • ##sent_http _Return HEADER name: Header header information can be added to the response client

Built-in variables

Nginx built-in variables, refer to Nginx Logging to syslog of the document

Custom variables

Variables defined by the server administrator

The above is the entire content of this article. I hope it will be helpful to everyone. Learning is helpful. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

The above is the detailed content of Analysis of Nginx basic configuration files and variables. 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

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

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1254
24
What happens if session_start() is called multiple times? What happens if session_start() is called multiple times? Apr 25, 2025 am 12:06 AM

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

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 handle high DPI display in C? How to handle high DPI display in C? Apr 28, 2025 pm 09:57 PM

Handling high DPI display in C can be achieved through the following steps: 1) Understand DPI and scaling, use the operating system API to obtain DPI information and adjust the graphics output; 2) Handle cross-platform compatibility, use cross-platform graphics libraries such as SDL or Qt; 3) Perform performance optimization, improve performance through cache, hardware acceleration, and dynamic adjustment of the details level; 4) Solve common problems, such as blurred text and interface elements are too small, and solve by correctly applying DPI scaling.

How to understand DMA operations in C? How to understand DMA operations in C? Apr 28, 2025 pm 10:09 PM

DMA in C refers to DirectMemoryAccess, a direct memory access technology, allowing hardware devices to directly transmit data to memory without CPU intervention. 1) DMA operation is highly dependent on hardware devices and drivers, and the implementation method varies from system to system. 2) Direct access to memory may bring security risks, and the correctness and security of the code must be ensured. 3) DMA can improve performance, but improper use may lead to degradation of system performance. Through practice and learning, we can master the skills of using DMA and maximize its effectiveness in scenarios such as high-speed data transmission and real-time signal processing.

macOS vs. Linux: Exploring the Differences and Similarities macOS vs. Linux: Exploring the Differences and Similarities Apr 25, 2025 am 12:03 AM

macOSandLinuxbothofferuniquestrengths:macOSprovidesauser-friendlyexperiencewithexcellenthardwareintegration,whileLinuxexcelsinflexibilityandcommunitysupport.macOS,developedbyApple,isknownforitssleekinterfaceandecosystemintegration,whereasLinux,beingo

Composer: Aiding PHP Development Through AI Composer: Aiding PHP Development Through AI Apr 29, 2025 am 12:27 AM

AI can help optimize the use of Composer. Specific methods include: 1. Dependency management optimization: AI analyzes dependencies, recommends the best version combination, and reduces conflicts. 2. Automated code generation: AI generates composer.json files that conform to best practices. 3. Improve code quality: AI detects potential problems, provides optimization suggestions, and improves code quality. These methods are implemented through machine learning and natural language processing technologies to help developers improve efficiency and code quality.

What is the significance of the session_start() function? What is the significance of the session_start() function? May 03, 2025 am 12:18 AM

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

How to uninstall MySQL and clean residual files How to uninstall MySQL and clean residual files Apr 29, 2025 pm 04:03 PM

To safely and thoroughly uninstall MySQL and clean all residual files, follow the following steps: 1. Stop MySQL service; 2. Uninstall MySQL packages; 3. Clean configuration files and data directories; 4. Verify that the uninstallation is thorough.

See all articles