微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

}

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐