nginx快速入门,教你看懂nginx默认配置文件

在这里插入图片描述

0 导入

​ 在专栏的第一篇《使用多种方式部署nginx(mac/windows/docker/docker-compose)》中,我们快速部署了一套nginx服务。

​ 恭喜你,你已经跑通的nginx的hello world!那么服务部署起来之后,下一步我们可以做什么呢?
Nginx,作为一款高性能的HTTP和反向代理服务器,最大的优势就是其强大的功能和灵活的配置,受到了广大开发者和运维人员的青睐。Nginx的配置文件是其核心,它决定了Nginx如何响应各种网络请求。下面,我们将从Nginx的默认配置文件入手,了解配置文件中的常见配置及其含义。

1 默认配置文件长什么样?

​ nginx部署完后会默认生成一份nginx配置文件,通常其位于安装文件的conf目录下,这份配置保留了让nginx运行起来最基础的成分,一些额外的配置也通过注释的形式保留在文件中。先让我们对这份配置文件有个大概的印象,后面部分再逐行解释这些配置的含义。

# mac os nginx默认配置文件 nginx.conf
#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


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;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #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$ {
        #    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;
    #    }
    #}

}

2 每行配置具体是有什么含义?

2.1 用户和组身份
user nobody;

这行配置指定了运行Nginx工作进程的用户和组。出于安全考虑,通常不会使用root用户运行Nginx,生产中通常会创建一个nginx用户和组用来运行nginx。

2.2 工作进程数
worker_processes 1;

这里定义了Nginx启动的工作进程数。1表示工作进程数为1,auto意味着Nginx会自动根据CPU的核心数来决定启动多少个工作进程。

2.3 错误日志
error_log logs/error.log warn;

这行指定了Nginx错误日志的位置和日志级别。在这个例子中,错误日志被保存在logs/error.log,并且只记录警告及以上级别的日志。

2.4 进程ID文件
pid logs/nginx.pid;

Nginx会将主进程的进程ID写入到指定的文件中,这有助于管理员发送信号来控制Nginx的行为,如重启、停止等。

2.5 事件模块配置
events {
    worker_connections 1024;
    multi_accept on;
}

events块中,我们可以配置网络事件的相关参数。worker_connections指定了每个工作进程可以打开的最大连接数。multi_accept允许工作进程一次接收多个连接。

2.6 HTTP服务器配置
http {
    ...
}

http`块包含了控制HTTP服务器行为的指令。大多数的配置都会在这个块或其子块中进行。

2.6.1 MIME类型
include mime.types;
default_type application/octet-stream;

Nginx使用MIME类型来确定如何响应不同类型的文件。include指令导入了预定义的MIME类型映射,而default_type指定了当无法确定文件类型时使用的默认MIME类型。

2.6.2 日志格式
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
               '$status $body_bytes_sent "$http_referer" '
               '"$http_user_agent" "$http_x_forwarded_for"';

这里定义了一个名为main的日志格式,它指定了日志中应该包含哪些字段。

2.6.3 访问日志
access_log logs/access.log main;

这行配置指定了访问日志的位置和使用的日志格式。在这个例子中,访问日志被保存在logs/access.log,并且使用了main这个日志格式。

2.6.4 客户端请求体大小
client_max_body_size 20m;

这个指令设置了允许客户端请求体的最大大小。在这个例子中,它被设置为20MB。

2.6.5 虚拟主机配置
server {
    listen 80;
    server_name localhost;
    
    root /var/www/html;
    index index.html index.htm;
    
    ...
}

server块用于定义一个虚拟主机。listen指定了监听的端口,server_name定义了服务器的域名。root指定了网站的根目录,index定义了默认的首页文件。

server块内,我们还可以定义更多的位置块(location)来处理特定的URL路径,设置SSL证书,配置反向代理等。

3 总结

​ Nginx的配置文件非常灵活,可以根据实际需求进行各种定制。通过深入了解每一行配置的含义,我们可以更好地优化Nginx的性能,保障网站的安全和稳定。希望以上内容能对你部署Nginx有所帮助,激发你进一步学习和探索nginx的兴趣。

  • 欢迎关注我的公众号,不仅为你推荐最新的博文,还有更多惊喜和资源在等着你!一起学习共同进步!
  • 25
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值