【nginx】配置文件

nginx简介

Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,公开版本1.19.6发布于2020年12月15日。
其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、简单的配置文件和低系统资源的消耗而闻名。2022年01月25日,nginx 1.21.6发布。
Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,在BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力在同类型的网页服务器中表现较好。
一般来说,我们常用的就是反向代理的功能,那我们平常使用nginx的时候,绝对绕不开ng的配置文件配置,我一直没有专门研究过这个,所以本次看一下。

配置文件

我们可以先看一下nginx提供的默认的配置文件nginx.conf:

#全局块
#user  nobody;
#nginx的进程数
worker_processes  1;

#错误日志存放位置
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#进程pid存放的位置
#pid        logs/nginx.pid;

#event块
#工作模式及连接数上限
events {
#单个后台worker_process进程的最大并发数连接数
    worker_connections  1024;
}

#http块
http {
    #文件扩展名与文件类型映射表
    include       mime.types;
    #默认文件类型,默认为text/plain
    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方式传输文件
    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;
		#请求的url过滤,此处可以写正则匹配,去学习一下正则吧。
        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;
    #    }
    #}

}

event块

Nginx是以event(事件)处理模型为基础的模块。它为了支持跨平台,抽象出了event模块。它支持的event处理类型有:AIO(异步IO),/dev/poll(Solaris 和Unix特有),epoll(Linux特有),eventport(Solaris 10特有),kqueue(BSD特有),poll,rtsig(实时信号),select等

event模块的主要功能就是,监听accept后建立的连接,对读写事件进行添加删除。事件处理模型和Nginx的非阻塞IO模型结合在一起使用。当IO可读可写的时候,相应的读写事件就会被唤醒,此时就会去处理事件的回调函数。

特别对于Linux,Nginx大部分event采用epoll EPOLLET(边沿触发)的方法来触发事件,只有listen端口的读事件是EPOLLLT(水平触发)。对于边沿触发,如果出现了可读事件,必须及时处理,否则可能会出现读事件不再触发,连接饿死的情况。

http块

可以嵌套多个server,配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置。如文件引入,mime-type定义,日志自定义,是否使用sendfile传输文件,连接超时时间,单连接请求数等。

server块

配置虚拟主机的相关参数,一个http中可以有多个server。

location块

配置请求的路由,以及各种页面的处理情况。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Nginx 配置文件是一个文本文件,通常位于 `/etc/nginx/` 目录下,主要包括以下几个部分: 1. 全局块:配置影响 nginx 全局的指令,一般有运行 nginx 的用户组、nginx 进程 pid 存放路径、日志存放路径和类型以及配置文件引入等。 2. events 块: 配置影响 nginx 服务器或与用户的网络连接,常用于设置连接超时时间、最大连接数等。 3. http 块:http 块中定义的配置指令用于处理 Web 请求,主要包括了 MIME 类型、字符集、缓存、请求限制等。 4. server 块:配置虚拟主机的相关参数,一个 http 块中可以包含多个 server 块,每个 server 块就相当于一个虚拟主机,用于处理来自客户端的请求。 一个简单的 Nginx 配置文件示例如下: ``` user nginx; worker_processes auto; 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; access_log /var/log/nginx/access.log main; sendfile on; keepalive_timeout 65; server { listen 80; server_name example.com; location / { root /usr/share/nginx/html; index index.html index.htm; } } } ``` 这个配置文件包含全局块和一个 server 块,其中: - `user` 指定 Nginx 运行的用户; - `worker_processes` 指定 Nginx 启动的 worker 进程数; - `error_log` 指定错误日志文件路径和级别; - `pid` 指定 Nginx 进程 ID 存放路径; - `events` 块中 `worker_connections` 指定每个 worker 进程最大连接数; - `http` 块中 `include` 指定 MIME 类型配置文件路径; - `default_type` 指定默认 MIME 类型; - `access_log` 指定访问日志文件路径和格式; - `sendfile` 指定是否使用 sendfile 函数传输文件; - `keepalive_timeout` 指定 keep-alive 连接超时时间; - `server` 块中 `listen` 指定监听端口和协议; - `server_name` 指定虚拟主机域名; - `location` 指定请求 URL 和对应的文件路径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

盖丽男

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值