Nginx-配置文件详解

Nginx的配置文件的组成部分: 主配置文件:nginx.conf,子配置文件 include conf.d/*.conf

默认配置文件

[root@s2 ~]# grep -v "#" /apps/nginx/conf/nginx.conf | grep -v "^$"
#全局配置端,对全局生效,主要设置nginx的启动用户/组,启动的工作进程数量,工作模式,Nginx的PID路径,日志路径等。
user nginx nginx;
worker_processes  4 |auto;  #启动工作进程数数量,一般设置和CPU核心数一致
events { #events设置快,主要影响nginx服务器与用户的网络连接,比如是否允许同时接受多个网络连接,使用哪种事件驱动模型处理请求,每个工作进程可以同时支持的最大连接数,是否开启对多工作进程下的网络连接进行序列化等。
  worker_connections  1024;  #设置单个nginx工作进程可以接受的最大并发,作为web服务器的时候最大并发数为worker_connections * worker_processes,作为反向代理的时候为(worker_connections * worker_processes)/2
}

http { #http块是Nginx服务器配置中的重要部分,缓存、代理和日志格式定义等绝大多数功能和第三方模块都可以在这设置,http块可以包含多个server块,而一个server块中又可以包含多个location块,server块可以配置文件引入、MIME-Type定义、日志自定义、是否启用sendfile、连接超时时间和单个链接的请求上限等。
 include    mime.types;
 default_type application/octet-stream;
 sendfile    on; #作为web服务器的时候打开sendfile加快静态文件传输,指定是否使用sendfile系统调用来传输文件,sendfile系统调用在两个文件描述符之间直接传递数据(完全在内核中操作),从而避免了数据在内核缓冲区和用户缓冲区之间的拷贝,操作效率很高,被称之为零拷贝,硬盘 >> kernel buffer (快速拷贝到kernelsocket buffer) >>协议栈。
 keepalive_timeout  65;  #长连接超时时间,单位是秒
 server { #设置一个虚拟机主机,可以包含自己的全局快,同时也可以包含多个location模块。比如本虚拟机监听的端口、本虚拟机的名称和IP配置,多个server 可以使用一个端口,比如都使用80端口提供web服务、
   listen    80;  #配置server监听的端口
   server_name localhost; 本server的名称,当访问此名称的时候nginx会调用当前serevr内部的配置进程匹配。
   location / { #location其实是server的一个指令,为nginx服务器提供比较多而且灵活的指令,都是在location中提现的,主要是基于nginx接受到的请求字符串,对用户请求的UIL进行匹配,并对特定的指令进行处理,包括地址重定向、数据缓存和应答控制等功能都是在这部分实现,另外很多第三方模块的配置也是在location模块中配置。
     root  html; #相当于默认页面的目录名称,默认是相对路径,可以使用绝对路径配置。
     index index.html index.htm; #默认的页面文件名称
   }
   error_page  500 502 503 504 /50x.html; #错误页面的文件名称
   location = /50x.html { #location处理对应的不同错误码的页面定义到/50x.html,这个跟对应其server中定义的目录下。
     root  html; #定义默认页面所在的目录
   }
 }
 
#和邮件相关的配置
#mail {
#        ...
#    }     mail 协议相关配置段
#tcp代理配置,1.9版本以上支持
#stream {
#        ...
#    }    stream 服务器相关配置段
#导入其他路径的配置文件
#include /apps/nginx/conf.d/*.conf
}

Nginx 核心配置详解

全局配置

user nginx nginx; #启动Nginx工作进程的用户和组
worker_processes [number | auto]; #启动Nginx工作进程的数量

worker_cpu_affinity 00000001 00000010 00000100 00001000; #将Nginx工作进程绑定到指定的CPU核心,默认Nginx是不进行进程绑定的,绑定并不是意味着当前nginx进程独占以一核心CPU,但是可以保证此进程不会运行在其他核心上,这就极大减少了nginx的工作进程在不同的cpu核心上的来回跳转,减少了CPU对进程的资源分配与回收以及内存管理等,因此可以有效的提升nginx服务器的性能。
worker_cpu_affinity auto; #也支持自动检测

# 未做CPU与进程绑定前,查看进程运行在哪个CPU

~]# ps axo pid,cmd,psr,user | grep nginx
1057 nginx: master process /usr/ 0 root
2086 nginx: worker process 3 nginx
2087 nginx: worker process 1 nginx
2088 nginx: worker process 2 nginx
2089 nginx: worker process 0 nginx


#错误日志记录配置,日志级别语法:error_log file [debug | info | notice | warn | error | crit |
alert | emerg]
#error_log logs/error.log;
#error_log logs/error.log notice;
error_log /apps/nginx/logs/error.log error;

#pid文件保存路径
pid    /apps/nginx/logs/nginx.pid;

worker_priority 0; #工作进程nice值,-20~19
worker_rlimit_nofile 65536; #这个数字包括Nginx的所有连接(例如与代理服务器的连接等),而不仅仅是与客户端的连接,另一个考虑因素是实际的并发连接数不能超过系统级别的最大打开文件数的限制.

[root@s2 ~]# watch -n1 'ps -axo pid,cmd,nice | grep nginx' #验证进程优先级
daemon off;  #前台运行Nginx服务用于测试、docker等环境。
master_process off|on; #是否开启Nginx的master-woker工作模式,仅用于开发调试场景。

events { #事件模型配置参数
 worker_connections  65536;  #设置单个工作进程的最大并发连接数
 use epoll; #使用epoll事件驱动,Nginx支持众多的事件驱动,比如select、poll、epoll,只能设置在events模块中设置。
 accept_mutex on; #优化同一时刻只有一个请求而避免多个睡眠进程被唤醒的设置,on为防止被同时唤醒默认为off,全部唤醒的过程也成为"惊群",因此nginx刚安装完以后要进行适当的优化。
 multi_accept on; Nginx服务器的每个工作进程可以同时接受多个新的网络连接,但是需要在配置文件中配置,此指令默认为关闭,即默认为一个工作进程只能一次接受一个新的网络连接,打开后几个同时接受多个。
}

http详细配置

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; #在开启了sendfile的情况下,合并请求后统一发送给客户端。
  #tcp_nodelay  off; #在开启了keepalived模式下的连接是否启用TCP_NODELAY选项,当为off时,延迟0.2s发送,默认On时,不延迟发送,立即发送用户相应报文。
  #keepalive_timeout 0;
 keepalive_timeout  65 65; #设置会话保持时间
  #gzip on; #开启文件压缩
 server {
   listen    80; #设置监听地址和端口
   server_name localhost; #设置server name,可以以空格隔开写多个并支持正则表达式,如*.magedu.com  www.magedu.*  www.(site\d+)\.magedu\.com$  default_server
    #charset koi8-r; #设置编码格式,默认是俄语格式,可以改为utf-8
    #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$ { #以http的方式转发php请求到指定web服务器
    #  proxy_pass  http://127.0.0.1;
    #}
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ { #以fastcgi的方式转发php请求到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 { #拒绝web形式访问指定文件,如很多的网站都是通过.htaccess文件来改变自己的重定向等功能。
    #  deny all;
    #}
   location ~ /passwd.html {
     deny all;
   }
 }
  # another virtual host using mix of IP-, name-, and port-based configuration
  #
  #server { #自定义虚拟server
  #  listen    8000;
  #  listen    somename:8080;
  #  server_name somename alias another.alias;
  #  location / {
  #    root  html;
  #    index index.html index.htm; #指定默认网页文件,此指令由ngx_http_index_module模块提供
  #  }
  #}
  # HTTPS server
  #
  #server { #https服务器配置
  #  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;
  #  }
 location
/linux38/passwd.ht { #拒绝访问某个文件,会返回403   deny all;   }   #}

核心配置示例

基于不同的IP、不同的端口以及不用得域名实现不同的虚拟主机,依赖于核心模块ngx_http_core_module实现。

新建一个PC web站点
[root@s2 ~]# mkdir /apps/nginx/conf/conf.d -p
[root@s2 ~]# cat /apps/nginx/conf/conf.d/pc.conf
server {
  listen 80;
  server_name www.magedu.net;
location
/ {   root /data/nginx/html/pc; } }
[root@s2
~]# mkdir /data/nginx/html/pc -p [root@s2 ~]# echo "pc web" > /data/nginx/html/pc/index.html
[root@s2
~]# vim /apps/nginx/conf/nginx.conf #主配置文件http块include子配置文件 include /apps/nginx/conf/conf.d/*.conf; [root@s2 ~]# systemctl  reload nginx 访问测试
新建一个Mobile web站点
[root@s2 ~]# cat /apps/nginx/conf/conf.d/mobile.conf
server {
  listen 80;
  server_name mobile.magedu.net;
  location / {
    root /data/nginx/html/mobile;
  }
}
[root@s2
~]# mkdir /data/nginx/html/mobile -p [root@s2 ~]# echo "mobile web" >> /data/nginx/html/mobile/index.html [root@s2 ~]# systemctl  reload nginx
root与alias:

root:指定web的家目录,在定义location的时候,文件的绝对路径等于 root+location,如:

server {
  listen 80;
  server_name www.magedu.net;
location
/ {   root /data/nginx/html/pc; }
location
/about {   root /data/nginx/html/pc; #必须要在html目录中创建一个about目录才可以访问,否则报错。   index index.html; } }
[root@s2
~]# mkdir /data/nginx/html/pc/about [root@s2 ~]# echo about > /data/nginx/html/pc/about/index.html

 alias:定义路径别名,会把访问的路径重新定义到其指定的路径,如:

server {
  listen 80;
  server_name www.magedu.net;
location
/ {  root /data/nginx/html/pc; }
location
/about { #使用alias的时候uri后面如果加了斜杠则下面的路径配置必须加斜杠,否则403  alias /data/nginx/html/pc; #当访问about的时候,会显示alias定义的/data/nginx/html/pc里面的内容。  index index.html; } } 重启Nginx并访问测试 http://www.magedu.net/about/index.html #访问指定文件资源

测试访问

[root@nginx-2 ~]# curl http://www.magedu.net/about/index.html
pc web
[root@nginx-2 ~]# curl http://www.magedu.net/about
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>
[root@nginx-2 ~]# curl http://www.magedu.net
pc web
[root@nginx-2 ~]# curl http://mobile.magedu.net
mobile web

 

posted @ 2022-01-04 22:00  不会跳舞的胖子  阅读(3924)  评论(0编辑  收藏  举报