Nginx配置

一、默认配置文件

1、nginx默认配置文件(全)

[root@web01 ~]# cat /application/nginx/conf/nginx.conf.default                

#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、nginx默认配置文件(过滤空格和注释)

[root@web01 ~]# egrep -v "^$|#" /application/nginx/conf/nginx.conf.default 
worker_processes  1;
events {
    worker_connections  1024;
}
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;
        }
    }
}

二、Nginx配置文件分析

1、Nginx配置文件的整体结构
在这里插入图片描述
在这里插入图片描述
从图中可以看出主要包含以下几大部分内容:

2、全局块,该部分配置主要影响Nginx全局,通常包括下面几个部分:

  • 配置运行Nginx服务器用户(组)
  • worker process数
  • 错误日志的存放路径
  • Nginx进程PID存放路径
  • 配置文件的引入
user  nginx;    #配置用户或者组
worker_processes;    #允许生成的进程数
error_log;    #日志文件目录
pid;    #指定Nginx进程文件的存放位置

在这里插入图片描述
3、events块

  • 该部分配置主要影响Nginx服务器与用户的网络连接,主要包括:
  • 设置网络连接的序列化
  • 是否允许同时接收多个网络连接
  • 事件驱动模型的选择
  • 最大连接数的配置
use  epoll;    #事件驱动模型
worker_connections;    #最大连接数

在这里插入图片描述

4、http块

  • 定义MIMI-Type
  • 自定义服务日志
  • 允许sendfile方式传输文件
  • 连接超时时间
  • 单连接请求数上限
    在这里插入图片描述

(1)include mime.types;
default_type; #默认文件类型

MIME-Type指的是网络资源的媒体类型,也即前端请求的资源类型,include指令将mime.types文件包含进来

(2)log_format; #自定义日志格式

1.$remote_addr 与$http_x_forwarded_for 用以记录客户端的ip地址; 
2.$remote_user :用来记录客户端用户名称; 
3.$time_local : 用来记录访问时间与时区;
4.$request : 用来记录请求的url与http协议;
5.$status : 用来记录请求状态;成功是200;
6.$body_bytes_s ent :记录发送给客户端文件主体内容大小;
7.$http_referer :用来记录从那个页面链接访问过来的; 
8.$http_user_agent :记录客户端浏览器的相关信息;

在这里插入图片描述

(3)Nginx访问日志轮询切割:
默认情况Nginx会把所有的访问日志生成到一个指定的访问日志文件access.log里,但这样一来,时间长了就会导致日志个头很大,不利用日志的分析和处理。因此,有必要对Nginx日志,按天或小时进行切割,使其分成不同的文件保存,现在Nginx是自动切割日志。

(4)开启高效文件传输模式
sendfile指令指定nginx是否调用sendfile函数来输出文件,减少用户空间到内核空间的上下文切换。对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。

sendfile on;

(5)长连接超时时间
单位是秒,这个参数很敏感,涉及浏览器的种类、后端服务器的超时设置、操作系统的设置,可以另外起一片文章了。长连接请求大量小文件的时候,可以减少重建连接的开销,但假如有大文件上传,65s内没上传完成会导致失败。如果设置时间过长,用户又多,长时间保持连接会占用大量资源。

keepalive_timeout 65;

(6)用于指定响应客户端的超时时间。
这个超时仅限于两个连接活动之间的时间,如果超过这个时间,客户端没有任何活动,Nginx将会关闭连接。

send_timeout;

(7)允许客户端请求的最大单文件字节数。
如果有上传较大文件,请设置它的限制值

client_max_body_size 10m;

(8)缓冲区代理缓冲用户端请求的最大字节数

client_body_buffer_size 128k;

(9)开启gzip压缩输出,减少网络传输

gzip on;

(10)include 文件映射,可以把server的配置文件写在某个文件中然后在http块中include

include extra/blog.conf;

4、server块

  • 配置网络监听
  • 基于名称的虚拟主机配置
  • 基于IP的虚拟主机配置

在这里插入图片描述

listen;    #监听端口,默认80
server_name;    #服务器名,如localhost、www.xxxxx.com,可以通过正则匹配

5、location块,http服务中,某些特定的URL对应的一系列配置项。

  • location配置
  • 请求根目录配置
  • 更改location的URI
  • 网站默认首页配置
    在这里插入图片描述

(1)location配置
指令格式为:

location [ = | ~ | ~* | ^~ ] uri {...}

uri前面的方括号中的内容是可选项,解释如下:

  • “=”:用于标准uri前,要求请求字符串与uri严格匹配,一旦匹配成功则停止
  • “~”:用于正则uri前,并且区分大小写
  • “~*”:用于正则uri前,但不区分大小写
  • “^~”:用于标准uri前,要求Nginx找到标识uri和请求字符串匹配度最高的location后,立即使用此 location处理请求,而不再使用location块中的正则uri和请求字符串做匹配

这里的uri分为标准uri和正则uri,两者的唯一区别是uri中是否包含正则表达式

(2)请求根目录配置
指令格式:

root path;

path:Nginx接收到请求以后查找资源的根目录路径

当然,还可以通过alias指令来更改location接收到的URI请求路径,指令为:

alias path;    

(3)设置网站的默认首页
指令格式:

index file1 file2 ......

file可以包含多个用空格隔开的文件名,首先找到哪个页面,就使用哪个页面响应请求

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值