Nginx核心配置-新建一个web站

1.Nginx基础配置常用参数说明

[root@nginx-test-10e0e0e22 ~]# cat /yanhuihuang/softwares/nginx/conf/nginx.conf
#启动Nginx工作进程的用户和组,比如"user nginx nginx"
#user  nobody;                                              
    
#启动nginx工作进程的数量,默认是一个,这个参数取决于你服务器CPU的核心数,一般情况下该值要小于等于你服务器的核心数哟。在nginx1.8版本之后咱们可以直接写"auto",即无需手写几个core数量,而是交给nginx自动取判断服务器拥有的core数量。
worker_processes  4;   
    
#将Nginx工作进程绑定到指定的CPU核心,默认Nginx是不进行进程绑定的,绑定并不是意味着当前nginx进程独占以一核心CPU,但是可以保证此进程不会运行在其他核心上,这就极大减少了nginx的工作进程在不同的cpu核心上的来回跳转,减少了CPU对进程的资源分配与回收以及内存管理等,因此可以有效的提升nginx服务器的性能。
worker_cpu_affinity 00000001 00000010 00000100 00001000; 
    
#错误日志记录配置,语法:error_log file [debug | info | notice | warn | error | crit |alert | emerg]
#error_log  logs/error.log;                                                 
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
     
#pid文件保存路径 
#pid        logs/nginx.pid;                         
                           
#工作进程优先级,-20~19  
worker_priority 0;              
                                              
#这个数字包括Nginx的所有连接(例如与代理服务器的连接等),而不仅仅是与客户端的连接,另一个考虑因素是实际的并发连接数不能超过系统级别的最大打开文件数的限制.
worker_rlimit_nofile 65536;                   
                                 
#前台运行Nginx服务用于测试、docker等环境。   
daemon off;                                                           
        
#是否开启Nginx的master-woker工作模式。
master_process off|on;                                                       
    
#events设置快,主要影响nginx服务器与用户的网络连接,比如是否允许同时接受多个网络连接,使用哪种事件驱动模型处理请求,每个工作进程可以同时支持的最大连接数,是否开启对多工作进程下的网络连接进行序列化等。
events {

    #设置单个nginx工作进程可以接受的最大并发,作为web服务器的时候最大并发数为"worker_connections * worker_processes",作为反向代理的时候为"(worker_connections * worker_processes)/2"(因为反向代理服务器是响应一个客户端请求连接需要消耗两个文件描述符,即接收客户端请求需要消耗一个文件描述符,将请求转发给后端的rip服务器处理又消费了一个文件描述符)
    worker_connections  100000;                  
        
    #使用epoll事件驱动,Nginx支持众多的事件驱动,比如select、poll、epoll,只能设置在events模块中设置。
    use epoll;                                                               
        
    #优化同一时刻只有一个请求而避免多个睡眠进程被唤醒的设置,on为防止被同时唤醒默认为off,全部唤醒的过程也成为"惊群",因此nginx刚安装完以后要进行适当的优化。
    accept_mutex on;                                                         
        
    #Nginx服务器的每个工作进程可以同时接受多个新的网络连接,但是需要在配置文件中配置,此指令默认为关闭,即默认为一个工作进程只能一次接受一个新的网络连接,打开后几个同时接受多个,配置语法如下:
    multi_accept on; 
        
}

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

    #长连接超时时间,单位是秒,即设置会话保持时间
    #keepalive_timeout  0;

    #设置会话保持时间时,我们也可以指定2个参数,第一个参数表示会话的保存时间,第二个参数是nginx通过response报文告诉客户端会话的保持时间,比如咱们配置的了服务端主动告诉客户端设置的连接超时时间是60秒,而实际上是65秒会话才会断开哟~
    keepalive_timeout  65 60;
  
   #通常情况下咱们都会开启压缩功能的哟~
    #gzip  on;

    #设置一个虚拟机主机,可以包含自己的全局快,同时也可以包含多个locating模块。比如本虚拟机监听的端口、本虚拟机的名称和IP配置,多个server 可以使用一个端口,比如都使用80端口提供web服务、
    server {

        #配置server监听的端口
        listen       80;

        #本server的名称,当访问此名称的时候nginx会调用当前serevr内部的配置进程匹配,以空格方式隔开多个FQDN,当然也支持正则表达式的方式匹配主机名。
        server_name  localhost;
    
      #指定编码格式,推荐修改为UTF-8字符编码
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

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

            #相当于默认页面的目录名称,默认是相对路径(如果是基于yum方式安装则是"/usr/share/nginx/html/",如果是基于源码方式安装,则在安装目录的下,如"/yanhuihuang/softwares/nginx/html/"),当然咱们也可以使用绝对路径配置。
            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,这个跟对应其server中定义的目录下。
        location = /50x.html {

            #定义默认页面所在的目录
            root   html;
        }

        #以http的方式转发php请求到指定web服务器
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ .php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        #以fastcgi的方式转发php请求到php处理
        # 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 {
             #拒绝web形式访问指定文件,如很多的网站都是通过.htaccess文件来改变自己的重定向等功能。
        #    deny  all;
        #}
    }

    #自定义虚拟server
    # 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;
             #指定默认网页文件,此指令由ngx_http_index_module模块提供
    #        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;
    #    }
    #}

#和邮件相关的配置
#mail {
# ...         #mail 协议相关配置段
# } 

#tcp代理配置,1.9版本以上支持
#stream {
# ...         #stream 服务器相关配置段
# } 

#导入其他路径的配置文件
#include /yanhuihuang/softwares/nginx/conf.d/*.conf;
 
}
[root@nginx-test-10e0e0e22 ~]# cat /yanhuihuang/softwares/nginx/conf/nginx.conf
[root@nginx-test-10e0e0e22 ~]#  cat /yanhuihuang/softwares/nginx/conf/mime.types

types {
    text/html                                        html htm shtml;
    text/css                                         css;
    text/xml                                         xml;
    image/gif                                        gif;
    image/jpeg                                       jpeg jpg;
    application/javascript                           js;
    application/atom+xml                             atom;
    application/rss+xml                              rss;

    text/mathml                                      mml;
    text/plain                                       txt;
    text/vnd.sun.j2me.app-descriptor                 jad;
    text/vnd.wap.wml                                 wml;
    text/x-component                                 htc;

    image/png                                        png;
    image/svg+xml                                    svg svgz;
    image/tiff                                       tif tiff;
    image/vnd.wap.wbmp                               wbmp;
    image/webp                                       webp;
    image/x-icon                                     ico;
    image/x-jng                                      jng;
    image/x-ms-bmp                                   bmp;

    font/woff                                        woff;
    font/woff2                                       woff2;

    application/java-archive                         jar war ear;
    application/json                                 json;
    application/mac-binhex40                         hqx;
    application/msword                               doc;
    application/pdf                                  pdf;
    application/postscript                           ps eps ai;
    application/rtf                                  rtf;
    application/vnd.apple.mpegurl                    m3u8;
    application/vnd.google-earth.kml+xml             kml;
    application/vnd.google-earth.kmz                 kmz;
    application/vnd.ms-excel                         xls;
    application/vnd.ms-fontobject                    eot;
    application/vnd.ms-powerpoint                    ppt;
    application/vnd.oasis.opendocument.graphics      odg;
    application/vnd.oasis.opendocument.presentation  odp;
    application/vnd.oasis.opendocument.spreadsheet   ods;
    application/vnd.oasis.opendocument.text          odt;
    application/vnd.openxmlformats-officedocument.presentationml.presentation
                                                     pptx;
    application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
                                                     xlsx;
    application/vnd.openxmlformats-officedocument.wordprocessingml.document
                                                     docx;
    application/vnd.wap.wmlc                         wmlc;
    application/x-7z-compressed                      7z;
    application/x-cocoa                              cco;
    application/x-java-archive-diff                  jardiff;
    application/x-java-jnlp-file                     jnlp;
    application/x-makeself                           run;
    application/x-perl                               pl pm;
    application/x-pilot                              prc pdb;
    application/x-rar-compressed                     rar;
    application/x-redhat-package-manager             rpm;
    application/x-sea                                sea;
    application/x-shockwave-flash                    swf;
    application/x-stuffit                            sit;
    application/x-tcl                                tcl tk;
    application/x-x509-ca-cert                       der pem crt;
    application/x-xpinstall                          xpi;
    application/xhtml+xml                            xhtml;
    application/xspf+xml                             xspf;
    application/zip                                  zip;

    application/octet-stream                         bin exe dll;
    application/octet-stream                         deb;
    application/octet-stream                         dmg;
    application/octet-stream                         iso img;
    application/octet-stream                         msi msp msm;

    audio/midi                                       mid midi kar;
    audio/mpeg                                       mp3;
    audio/ogg                                        ogg;
    audio/x-m4a                                      m4a;
    audio/x-realaudio                                ra;

    video/3gpp                                       3gpp 3gp;
    video/mp2t                                       ts;
    video/mp4                                        mp4;
    video/mpeg                                       mpeg mpg;
    video/quicktime                                  mov;
    video/webm                                       webm;
    video/x-flv                                      flv;
    video/x-m4v                                      m4v;
    video/x-mng                                      mng;
    video/x-ms-asf                                   asx asf;
    video/x-ms-wmv                                   wmv;
    video/x-msvideo                                  avi;
}
[root@nginx-test-10e0e0e22 ~]# cat /yanhuihuang/softwares/nginx/conf/mime.types
Nginx的配置文件的组成部分: 
    主配置文件:nginx.conf
    子配置文件 include conf.d/*.conf

Nginx主配置文件的配置指令方式:
    directive value [value2 ...];
    注意:
        (1) 指令必须以分号结尾
        (2) 支持使用配置变量
            内建变量:由Nginx模块引入,可直接引用
            自定义变量:由用户使用set命令定义
                set variable_name value;
            引用变量:$variable_name    

MIME参考文档:https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Basics_of_HTTP/MIME_Types

2.Nginx新建一个web站点案例实战

2.1.编辑主配置文件并检查语法格式是否正确

[root@nginx-test-10e0e0e22 ~]# cat  /yanhuihuang/softwares/nginx/conf/nginx.conf
user  nginx;
worker_processes  1;
#worker_cpu_affinity 00000001 ; 


error_log  /yanhuihuang/softwares/nginx/logs/error.log warn;
pid        /yanhuihuang/softwares/nginx/logs/nginx.pid;


events {
    worker_connections  1024;
    use epoll;
    accept_mutex on;
    multi_accept on; 
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
     
   #导入其他路径的配置文件
     include /yanhuihuang/softwares/nginx/conf.d/*.conf;
 }
[root@nginx-test-10e0e0e22 ~]# cat /yanhuihuang/softwares/nginx/conf/nginx.conf
[root@nginx-test-10e0e0e22 ~]# /yanhuihuang/softwares/nginx/sbin/nginx  -t
nginx: the configuration file /yanhuihuang/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yanhuihuang/softwares/nginx/conf/nginx.conf test is successful

2.2.在配置子目录中编写配置文件并检查语法

[root@nginx-test-10e0e0e22 ~]# cat /yanhuihuang/softwares/nginx/conf.d/web.conf
server {
    listen 80;
    server_name 10.0.0.22;

    location / {
        root /yanhuihuang/data/web/nginx/html/java;
        index index.html;
    }

    location /python {
        root /yanhuihuang/data/web/nginx/html;
        index index.html;
    }

    location /golang {
        root /yanhuihuang/data/web/nginx/html;
        index index.html;
    }

}
[root@nginx-test-10e0e0e22 ~]# /yanhuihuang/softwares/nginx/sbin/nginx  -t
nginx: the configuration file /yanhuihuang/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yanhuihuang/softwares/nginx/conf/nginx.conf test is successful

 

2.3.创建测试数据

[root@nginx-test-10e0e0e22 ~]# mkdir -pv /yanhuihuang/data/web/nginx/html/{java,python,golang}
mkdir: created directory ‘/yanhuihuang/data’
mkdir: created directory ‘/yanhuihuang/data/web’
mkdir: created directory ‘/yanhuihuang/data/web/nginx’
mkdir: created directory ‘/yanhuihuang/data/web/nginx/html’
mkdir: created directory ‘/yanhuihuang/data/web/nginx/html/java’
mkdir: created directory ‘/yanhuihuang/data/web/nginx/html/python’
mkdir: created directory ‘/yanhuihuang/data/web/nginx/html/golang’
[root@nginx-test-10e0e0e22 ~]# echo "<h1 style='color:rgb(255,0,0)'>Java</h1>" > /yanhuihuang/data/web/nginx/html/java/index.html
[root@nginx-test-10e0e0e22 ~]# echo "<h1 style='color:rgb(0,255,0)'>Python</h1>" > /yanhuihuang/data/web/nginx/html/python/index.html
[root@nginx-test-10e0e0e22 ~]# echo "<h1 style='color:rgb(0,0,255)'>Golang</h1>" > /yanhuihuang/data/web/nginx/html/golang/index.html
[root@nginx-test-10e0e0e22 ~]# ll /yanhuihuang/data/web/nginx/html/ -R
/yanhuihuang/data/web/nginx/html/:
total 0
drwxr-xr-x. 2 root root 24 Mar  9 05:33 golang
drwxr-xr-x. 2 root root 24 Mar  9 05:33 java
drwxr-xr-x. 2 root root 24 Mar  9 05:33 python

/yanhuihuang/data/web/nginx/html/golang:
total 4
-rw-r--r--. 1 root root 43 Mar  9 05:33 index.html

/yanhuihuang/data/web/nginx/html/java:
total 4
-rw-r--r--. 1 root root 41 Mar  9 05:33 index.html

/yanhuihuang/data/web/nginx/html/python:
total 4
-rw-r--r--. 1 root root 43 Mar  9 05:33 index.html
创建测试数据

2.4.启动nginx服务

[root@nginx-test-10e0e0e22 ~]# /yanhuihuang/softwares/nginx/sbin/nginx  -t
nginx: the configuration file /yanhuihuang/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yanhuihuang/softwares/nginx/conf/nginx.conf test is successful
[root@nginx-test-10e0e0e22 ~]# ss -lntp 
State       Recv-Q Send-Q                                                                  Local Address:Port                                                                                 Peer Address:Port              
LISTEN      0      128                                                                                 *:42344                                                                                           *:*                   users:(("rpc.statd",pid=7107,fd=9))
LISTEN      0      64                                                                                  *:42411                                                                                           *:*                  
LISTEN      0      128                                                                                 *:111                                                                                             *:*                   users:(("rpcbind",pid=6177,fd=8))
LISTEN      0      128                                                                                 *:80                                                                                              *:*                   users:(("nginx",pid=19773,fd=6),("nginx",pid=19772,fd=6))
LISTEN      0      128                                                                                 *:20048                                                                                           *:*                   users:(("rpc.mountd",pid=8266,fd=8))
LISTEN      0      128                                                                                 *:22                                                                                              *:*                   users:(("sshd",pid=7101,fd=3))
LISTEN      0      100                                                                         127.0.0.1:25                                                                                              *:*                   users:(("master",pid=7291,fd=13))
LISTEN      0      64                                                                                  *:2049                                                                                            *:*                  
LISTEN      0      128                                                                                :::37356                                                                                          :::*                   users:(("rpc.statd",pid=7107,fd=11))
LISTEN      0      128                                                                                :::111                                                                                            :::*                   users:(("rpcbind",pid=6177,fd=11))
LISTEN      0      128                                                                                :::20048                                                                                          :::*                   users:(("rpc.mountd",pid=8266,fd=10))
LISTEN      0      128                                                                                :::22                                                                                             :::*                   users:(("sshd",pid=7101,fd=4))
LISTEN      0      100                                                                               ::1:25                                                                                             :::*                   users:(("master",pid=7291,fd=14))
LISTEN      0      64                                                                                 :::44187                                                                                          :::*                  
LISTEN      0      64                                                                                 :::2049                                                                                           :::*                
测试启动nginx

2.5.访问web服务器

 

 

原文地址:https://www.cnblogs.com/huihuangyan/p/14506509.html