Nginx的配置文件各个模块介绍

一、前言

    学习Nginx首先需要对它的核心配置文件nginx.conf有一定的认识,Nginx的核心配置文件主要由三个部分构成:

  1. 基本配置
  2. events配置
  3. http配置

二、各个模块的作用

#==============基本配置===================

#user  nobody; #配置worker进程运行用户
worker_processes  1; #配置工作进程数目,根据硬件调整,通常等于CPU数量或2倍于CPU数量

#error_log  logs/error.log; #配置全局错误日志以及类型 【debug | info | notice | warn | error | crit】,默认是error
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid; #配置进程pid文件

#==============events配置===================

#配置工作模式和连接数
events {
    #配置每个worker进程的连接数上限,nginx支持的总连接数就等于worker_processes*worker_connections
    worker_connections  1024;
}

#==============http配置===================

#配置http服务器,利用它的反向代理功能提供负载均衡支持
http {

#==============http的基本配置===================

    include       mime.types; #配置nginx支持哪些多媒体类型,可以在conf.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日志及存放路径,并且使用上面定义的main日志格式
    #access_log  logs/access.log  main;

    sendfile        on; #假期高效文件传输模式
    #tcp_nopush     on; #防止网络阻塞

    #keepalive_timeout  0;
    keepalive_timeout  65; #长连接超时时间,单位是秒

    #gzip  on; #开启gzip压缩输出

#==============http的多个server配置===================

    #配置虚拟主机
    server {
        listen       80; #配置监听端口
        server_name  localhost; #配置服务名

        #charset koi8-r; #配置字符集

        #access_log  logs/host.access.log  main; #配置本虚拟主机的访问日志

        #默认的匹配斜杠/的请求,当访问路径有斜杠,会被该location匹配并且进行处理
        location / {
            root   html; #root是配置服务器的默认网站根目录位置,默认为nginx安装主目录下的html目录
            index  index.html index.htm; #配置首页文件的名称
        }

        #error_page  404              /404.html; #配置404页面

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html; #配置50x错误页面
        location = /50x.html {
            root   html;
        }

        #PHP 脚本请求全部转发到Apache处理
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        #PHP 脚本请求全部转发到FastCGI处理
        # 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;
        #}

        #禁止访问.htaccess 文件
        # 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服务
    # 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;
    #    }
    #}

}

三、总结

希望对大家有帮助!

发布了185 篇原创文章 · 获赞 457 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/weixin_42146366/article/details/103833672