nginx之conf配置详解

十二 2022年05月16日 1474 4

前言

前些日子介绍了在linux下安装nginx进行反向代理linux下安装和配置nginx,当时说会更新一期配置文件的详解,它来了!🥳

配置文件详解

首先贴上刚安装好的nginx的配置文件代码

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

}

代码块中的events、http、server、location、upstream等都是块配置项。

块配置项可以嵌套。内层块直接继承外层快,例如:server块里的任意配置都是基于http块里的已有配置的。

user

nginx worker进程运行的用户及用户组。

语法:user username[groupname] 默认:user nobody nobody

worker_processes

每个worker进程都是单线程的进程,他们会调用各个模块以实现多种多样的功能。如果这些模块不会出现阻塞式的调用,那么,有多少CPU内核就应该配置多少个进程,反之,有可能出现阻塞式调用,那么,需要配置稍多一些的worker进程。

error_log

error日志的设置

语法: error_log /path/file level;

默认: error_log / log/error.log error;

当path/file 的值为 /dev/null时,这样就不会输出任何日志了,这也是关闭error日志的唯一手段;

leve的取值范围是debug、info、notice、warn、error、crit、alert、emerg从左至右级别依次增大。

当level的级别为error时,error、crit、alert、emerg级别的日志就都会输出。大于等于该级别会输出,小于该级别的不会输出。

如果设定的日志级别是debug,则会输出所有的日志,这一数据量会很大,需要预先确保/path/file所在的磁盘有足够的磁盘空间。级别设定到debug,必须在configure时加入 --with-debug配置项。

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

events

仅对指定的客户端输出debug级别的日志: 语法:debug_connection[IP|CIDR]。

这个设置项实际上属于事件类配置,因此必须放在events{……}中才会生效。它的值可以是IP地址或者是CIRD地址。

http

在http模块中,可以嵌入其他配置文件

例如: include /path/file

参数既可以是绝对路径也可以是相对路径

server

server块是我们用到最多的,常见的nginx反向代理时,就需要把解析写在server块中才会生效。

listen:我们要监听的端口,例如80、8081等,如果是https的话,则需要写443 ssl。

server_name:主机名称,可以写多个主机名称,只要写在此server块中的主机都会被解析到这里。

location:请求的地址。location是有顺序的,当一个请求匹配到多个location时,实际上这个请求会被第一个location处理。

server_name与host的匹配优先级

首先选择所有字符串完全匹配的server_name 如:www.testwab.com
其次选择通配符在前面的server_name 如:*.testwab.com
其次选择通配符在后面的server_name 如:www.testwab.*
最后选择使用正在表达式才匹配的server_name 如:~^.testwab.com$

location的使用实例

只有当用户请求是/时,才会使用该location下的配置

location = / {
# matches the query / only.
[ configuration A ]
}

可以匹配所有请求

location / {
# matches any query, since all queries begin with /, but regular
# expressions and any longer conventional blocks will be
# matched first.
[ configuration B ]
}

匹配以/images/开头的任何查询并停止搜索

表示匹配URL时忽略字母大小写问题

location ^~ /images/ {
# matches any query beginning with /images/ and halts searching,
# so regular expressions will not be checked.
[ configuration C ]
}

匹配任何以gif、jpg或jpeg结尾的请求。

location ~* \.(gif|jpg|jpeg)$ {
# matches any request ending in gif, jpg, or jpeg. However, all
# requests to the /images/ directory will be handled by
# Configuration C.
[ configuration D ]
}

以root方式设置资源路径

location /download/ {

           root    /opt/wab/html/;

}          [[[意思是有一个请求的URL是 /download/index/test.html, 那么Web服务器就会返回服务器上 /opt/wab/html/download/index/test.html 文件的内容]]]

以alias方式设置资源路径

alias也是用来设置文件资源路径的,它与root不同点主要在于如何解读紧跟location后面的uri参数,这将会致使alias与root以不同的方式将用户请求映射到真正的磁盘文件上。

例如:如果有一个请求的URI是/conf/nginx.conf,而用户实际想访问的是 /usr/local/nginx/conf/nginx.conf,则两种方式如下:

alias:
location  /conf {
	alias    /usr/local/nginx/conf
}

root:
location    /conf{
	root    /usr/local/nginx
}

使用alias时,在URI向实际文件路径的映射过程中,已经把location后配置的 /conf这部分字符串丢弃掉了,因此若path中不加/conf这部分,直接映射回的地址是/usr/local/nginx/nginx.conf 与用户实际想访问的路径不符。root可以放置在http、server、location或if块中,而alias只能放置在location块中。

Last Updated: 2022/05/16 21:01:58
一些常用的chatGPT调教指令 springboot使用swagger实现RestfulAPI