LNMP-nginx配置文件解析

 

 

[root@localhost ~]# yum install -y gcc gcc-c++ glibc glibc-devel pcre pcre- devel openssl openssl-devel systemd-devel net-tools iotop bc zip unzip zlib- devel bash-completion nfs-utils automake libxml2 libxml2-devel libxslt libxslt- devel perl perl-ExtUtils-Embed

[root@localhost ~]# useradd -s /sbin/nologin nginx -u 2021

[root@localhost src]# wget http://nginx.org/download/nginx- 1.18.0.tar.gz

[root@localhost src]# tar xf nginx-1.18.0.tar.gz

[root@localhost nginx-1.18.0]# ./configure --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module

[root@localhost nginx-1.18.0]# make -j 4 && make install

[root@localhost ~]# /apps/nginx/sbin/nginx

[root@localhost conf]# cat nginx.conf

#user  nobody; 
user nginx;  #全局配置端,对全局生效,主要设置nginx的启动用户/组,启动的工作进程数量,工作模式,Nginx的PID路径,日志路径等
worker_processes  1; #启动工作进程数数量 ,这个值默认是1,可设置成auto

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

#pid        logs/nginx.pid;


events {  #events设置块,主要影响nginx服务器与用户的网络连接,比如是否允许同时接受多个网络 连接,使用哪种事件驱动模型处理请求,每个工作进程可以同时支持的最大连接数,是否开启对多工作进程下 的网络连接进行序列化等。
    worker_connections  1024; #设置单个nginx工作进程可以接受的最大并发,作为web服务器 的时 候最大并发数为worker_connections * worker_processes,作为反向代理的时候为 要除以 2(worker_connections * worker_processes)/2 因为作为反向代理的时候是自身来处理,所以本身扛得住。
}


http {  #http块是Nginx服务器配置中的重要部分,缓存、代理和日志格式定义等绝大多数功能和第三方 模块都可以在这设置,http块可以包含多个server块,而一个server块中又可以包含多个location块, server块可以配置文件引入、MIME-Type定义、日志自定义、是否启用sendfile、连接超时时间和单个链 接的请求上限等。
    include       mime.types; #支持的mime类型,MIME(Multipurpose Internet Mail Extensions)多用途互联网邮件扩展类型,MIME消息能包含文本、图像、音频、视频以及其他应用程序专用 的数据,是设定某种扩展名的文件用一种应用程序来打开的方式类型,当该扩展名文件被访问的时候,浏览器 会自动使用指定应用程序来打开。多用于指定一些客户端自定义的文件名,以及一些媒体文件打开方式。
    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; #作为web服务器的时候打开sendfile加快静态文件传输,指定是否使用 sendfile系统调用来传输文件,sendfile系统调用在两个文件描述符之间直接传递数据(完全在内核中操 作),从 而避免了数据在内核缓冲区和用户缓冲区之间的拷贝,操作效率很高,被称之为零拷贝,硬盘 >> kernel buffer (快速拷贝到kernelsocket buffer) >>协议栈。
    #tcp_nopush     on; 

    #keepalive_timeout  0;
    keepalive_timeout  65 65; #长连接超时时间,单位是秒 通常加两个,让客户端和服务器的持续 链接时间相同
    server_tokens off; #安全优化-隐藏版本号
    #gzip  on;

    server { #设置一个虚拟机主机,可以包含自己的全局块,同时也可以包含多个location模 块。比如本虚拟机监听的端口、本虚拟机的名称和IP配置,多个server 可以使用一个端口,比如都使用80 端口提供web服务、本身可以多次监听同一个端口
        listen       80; #配置server监听的端口默认监听所有80
        server_name  localhost; #本server的名称,当访问此名称的时候nginx会调用当前serevr内部的配置进程匹配nginx可以有多个虚拟主机,只要使用域名区分即可。

        #charset koi8-r; #设置编码格式,默认是俄语格式,可以改为utf-8 让他支持全球标准语法

        #access_log  logs/host.access.log  main;

        location / { #location其实是server的一个指令,为nginx服务器提供比较多而且灵活的指令, 都是在location中提现的,主要是基于nginx接受到的请求字符串,对用户请求的UIL进行匹配,并对特定 的指令进行处理,包括地址重定向、数据缓存和应答控制等功能都是在这部分实现,另外很多第三方模块的配 置也是在location模块中配置。
            root   html;
            index  index.html index.htm; #nginx配置默认首页
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html; #出现这几个状态即可跳转到 50x.html文件
        location = /50x.html {
            root   html; #定义错误页面的时候做的处理,这个50x.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$ { #以http的方式转发php请求到指定web服务器
        #    root           html;  #php文件存放路径
        #    fastcgi_pass   127.0.0.1:9000;  #指明后端php-fpm服务器主机及端口
        #    fastcgi_index  index.php;  #默认使用的php文件
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name; #这个配置的意思是 在浏览器中访问的.php文件,实际读取的是 $document_root(网站根目录)下的.php 文件 -- 也就是说当访问127.0.0.1/index.php的时候,需要读取网站根目录下面的index.php文件,如 果没有配置这一配置项时,nginx不回去网站根目录下访问.php文件,所以返回空白
        #    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;
    #    }
    #}

}

 

 

[root@localhost sbin]# cat   /apps/nginx/conf/nginx.conf

#user  nobody;
user nginx;
worker_processes  auto;

#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 65;
    server_tokens off;
    #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;
    #    }
    #}

}

 

posted @ 2021-12-01 08:54  gg888666  阅读(111)  评论(0编辑  收藏  举报