[Nginx from entry to practice] [Basics] 04. Nginx basic configuration syntax

basic configuration

# 设置nginx服务的系统使用用户
user nginx;
# 工作进程数
worker_processes auto;
# 错误日志目录
error_log /var/log/nginx/error.log;
# nginx服务启动时候pid
pid /run/nginx.pid;


# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
# 加载模块
include /usr/share/nginx/modules/*.conf;


events {
   worker_connections 1024;//每个进程允许最大连接数/工作进程数
}


# HTTP请求
http {
   #日志格式
   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  /var/log/nginx/access.log  main;


   # 底层设置
   sendfile            on;
   tcp_nopush          on;
   tcp_nodelay         on;
   keepalive_timeout   65;
   types_hash_max_size 2048;


   include             /etc/nginx/mime.types;
   default_type        application/octet-stream;


   # Load modular configuration files from the /etc/nginx/conf.d directory.
   # 加载配置
   include /etc/nginx/conf.d/*.conf;


   # 配置站点
   server {
       # 监听80端口
       listen       80 default_server;
       listen       [::]:80 default_server;
       # 域名
       server_name  _;
       # 站点目录
       root         /usr/share/nginx/html;


       # Load configuration files for the default server block.
       # 加载站点配置
       include /etc/nginx/default.d/*.conf;
       
       # 下面根据url定义方法
       location / {
       }


       error_page 404 /404.html;
       location = /404.html {
       }


       error_page 500 502 503 504 /50x.html;
       location = /50x.html {
       }
   }
}

Insert picture description here
Insert picture description here

Insert picture description here

  1. user www www;: Specify what user to start the nginx process
  2. worker_processes 4;: Specify how many processes to start to process requests. Generally, it is set to the number of CPU cores. If ssl and gzip are enabled, it should be set to the same or even twice the number of logical CPUs, which can reduce I/O operations. Use grep ^processor /proc/cpuinfo | wc -l to view the number of CPU cores.
  3. worker_cpu_affinity 0001 0010 0100 1000;: In the case of high concurrency, by setting the binding of the CPU and the specific process to reduce the performance loss caused by the on-site reconstruction of the register caused by the switching of the multi-core CPU. Such as worker_cpu_affinity 0001 0010 0100 1000; (quad core).
  4. error_log /var/logs/nginx_error.log crit;: error_log is a main module command used to define the global error log file. The log output levels are debug, info, notice, warn, error, and crit to choose from. Among them, the debug output log is the most detailed, and the crit output log is the least.
  5. pid /usr/local/webserver/nginx/nginx.pid;: Specify the location of the process pid file.
  6. worker_rlimit_nofile 65535;: Used to specify the maximum number of file descriptors that an nginx process can open, here is 65535, you need to use the command "ulimit -n 65535" to set.

nginx.conf



user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    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"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

default.conf

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;

    location / {
        root   /usr/share/nginx/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 404  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/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;
    #}
}

Guess you like

Origin blog.csdn.net/happy_teemo/article/details/114926414
Recommended