码迷,mamicode.com
首页 > 其他好文 > 详细

回炉重造之 nginx

时间:2020-07-07 00:12:15      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:expec   第一个   引用   用户   idle   pid   文件的   err   私钥   

一 模块介绍

核心模块

user: 用来指定运行 nginx worker 进程的用户名
daemon: 是否指定以后台的方式运行,默认为on
error_log: 用来定义nginx错误日志的输出位置以及格式 基本格式如下:

Syntax: error_log file [level];
Default: error_log logs/error.log error;
Context: main, http, mail, stream, server, location

默认输出到本地 也可以输出到文件系统

error_log syslog:server=192.168.1.1 debug;
access_log syslog:server=unix:/var/log/nginx.sock,nohostname;

另外,错误日志也可以发送到内存中。。。这个目前没用到,不谈

include:用来指定Nginx主配置文件中引入一些配置段

Default: —
Context: any

生产比较常用的是

include /etc/nginx/default.d/*.conf;
include /usr/share/nginx/modules/*.conf;
include mime.types;
include vhosts/*.conf;

load_module 用来加载nginx模块 基本用法

Syntax: load_module file;
Default: —
Context: main
This directive appeared in version 1.9.11.

实例

load_module modules/ngx_mail_module.so;

一般使用load——modele来手动加载某个后编译的模块,一般nginx的默认配置是先通过加载配置文件,然后再修改配置文件中引用load_module来指定具体加载的模块比如

include /usr/share/nginx/modules/*.conf

里面的

[root@localhost modules]# cat  mod-stream.conf 
load_module "/usr/lib64/nginx/modules/ngx_stream_module.so";

pid 用来指定nginx的pid文件,需要注意 我们再通过systemctl对nginx进程或直接使用nginx进行管理,是基于该配置文件的,如果删除了pid会报错这个适合只能kill 你滚下master

http 核心模块

通过epel下载的nginx 默认如下

 server {
 listen 80 default_server;
 listen [::]:80 default_server;
 server_name _;
 root /usr/share/nginx/html;
 # Load configuration files for the default server block.
 include /etc/nginx/default.d/*.conf;
 location / {

listen:用来指定虚拟主机的监听地址(可以是IPV4或者IPV6)与端口,以及
一些与套接字的相关配置

Syntax: listen address[:port] [default_server] [ssl] [http2 | spdy]
[proxy_protocol]
 [setfib=number] [fastopen=number] [backlog=number]
[rcvbuf=size]
 [sndbuf=size] [accept_filter=filter] [deferred] [bind]
[ipv6only=on|off]
 [reuseport] [so_keepalive=on|off|[keepidle]:[keepintvl]:
[keepcnt]];
 listen port [default_server] [ssl] [http2 | spdy] [proxy_protocol]
[setfib=number] [fastopen=number] [backlog=number]
[rcvbuf=size] [sndbuf=size] 
[accept_filter=filter] [deferred] [bind] [ipv6only=on|off] [reuseport] 
[so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt]];
 listen unix:path [default_server] [ssl] [http2 | spdy]
[proxy_protocol] [backlog=number] [rcvbuf=size]
[sndbuf=size] [accept_filter=filter]
 [deferred] [bind] [so_keepalive=on|off|[keepidle]:[keepintvl]:
[keepcnt]];
Default: listen *:80 | *:8000;
Context: server

示例

listen 127.0.0.1:8000;
listen 127.0.0.1;
listen 8000;
listen *:8000;
listen localhost:8000;

如果没有指定端口的话,则默认是再80端口; 另外,还可以监听再unix套接字上,实现本机的快速访问,如

listen unix:/var/run/nginx.sock;

在listen 指令中,有几个和调优相关的参数,比如:

listen 0.0.0.0:80 backlog=10240 reuseport so_keepalive=10m::10

default_server: 用来指定默认的虚拟主机,即当用户请求的域名和当前节点配置的域名均不符合时,由该虚拟主机进行响应,如果所有的主机都没有配置default_server,那么第一个虚拟主机作为默认的虚拟主机
ssl: 配置https类型的虚拟主机,指定ssl时,需要指定ssl监听端口,默认为443
使用openssl快速创建私有证书

openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout ./nginx.key
-out ./nginx.crt

查看证书信息:

openssl x509 -in nginx.crt -noout -text

使用ssl绘画加密时,我们至少要指定监听的端口号,域名证书以及域名私钥,ex

server {
 listen 443 ssl;
 server_name www.a.com a.com;
 ssl_certificate /data/nginx/a/ssl/nginx.crt;
 ssl_certificate_key /data/nginx/a/ssl/nginx.key;
}

证书和私钥我们最好设置为600或者400权限
server_name用来指定虚拟主机域名,可以指定多个域名,比如:

server_name{
 example.com www.example.com;
}

也支持使用*作为通配符 左侧通配符高于右侧通配符

server {
 server_name example.com *.example.com www.example.*;
}

也支持正则,但是非常影响性能

server {
 server_name www.example.com ~^www\d+\.example\.com$;
}

root:用以定义资源路径,该指令出现在http,server,location,if not location段中,内层未定义是会继承外层,内层会覆盖外层,基本语法

Syntax: root path;
Default: root html;
Context: http, server, location, if in location

示例

server {
 listen 443 ssl;
 server_name www.a.com a.com;
 ssl_certificate /data/nginx/a/ssl/nginx.crt;
 ssl_certificate_key /data/nginx/a/ssl/nginx.key;
 root /data/nginx/a;
}

此时:我们访问url:http://www.a.com/fstab.txt 那么访问文件路径为 /data/nginx/a/fstab.txt;

location: 用来设定不同URI 的文件系统的路径映射,一个server中可以设置多个location,nginx会根据URI来诸葛判断。语法:

Syntax: location [ = | ~ | ~* | ^~ ] uri { ... }
 location @name { ... }
Default: —
Context: server, location

其中:
=:是对URI的精确匹配,优先级最高,如果匹配成功,则停止向下搜索

location =/{
... ...
}

当请求 http://www.byte-edu.com/ 时匹配,但是 http://www.byte-edu.com/index.html则不匹配
^~:对URI起始字符做字符串匹配,区分大小写,会检测所有匹配项,以匹配长度为有限,一旦匹配上,则不再进行正则匹配
~ 对uri(可以不是起始字符)做正则匹配,区分大小写;
~对url(可以不是起始字符)做正则匹配,不区分大小写;
不带符号: 匹配
起始于此字符串的所有uri*,区分大小写;
注意 字符串匹配(无论是^~还是无符号匹配,后面都必须以为/开头
= > ^~ > ~= ~* > 不带符号
我们还可以以 @符号前缀来定义location段,这种定义 一般用于内部跳转!!
**
location @fallback{
proxy_pass http:backend;
}
**

需要注意的是,前缀匹配根据匹配长度作为优先级,正则表达式更具配置文件的顺序。

示例文件如下

server{
      listen 80;
       server_name www.a.com a.com *.a.com;
       root /data/nginx/domain/;
       location = /img {
        return 701;
        }
        location ^~ /img {
        return 702;
        }
        location ~ /img{
        return 703;
        }
        location ~* /img{
        return 704;
        }
        location /img{
        return 705;
        }
        location /{
        return 666;
        }
}

nginx -t 会报错

nginx: [emerg] unexpected "}" in /etc/nginx/conf.d/domain.conf:13

是因为 location /img{} 和 location ^~ /img {} nginx认为是一样的,唯一的区别是 带 ^~ 的优先级比正则表达式高
所以修改之后

server{
      listen 80;
       server_name www.a.com a.com *.a.com;
       root /data/nginx/domain/;
       location = /img {
        return 701;
        }
        location ^~ /img {
        return 702;
        }
        location ~ /img{
        return 703;
        }
        location ~* /img{
        return 704;
        }
        location /imgs{
        return 705;
        }
        location /{
        return 666;
        }
}

测试用例1

[root@localhost ~]# curl -I  www.a.com/img
HTTP/1.1 701 
Server: nginx/1.16.1
Date: Mon, 06 Jul 2020 15:01:29 GMT
Content-Length: 0
Connection: keep-alive

访问uri img 发现是精确匹配
修改配置文件

server{
      listen 80;
       server_name www.a.com a.com *.a.com;
       root /data/nginx/domain/;
       location = / {
        return 701;
        }
        location ^~ /img {
        return 702;
        }
        location ~ /img{
        return 703;
        }
        location ~* /img{
        return 704;
        }
        location /imgs{
        return 705;
        }
        location /{
        return 666;
        }
}

再次请求

[root@localhost ~]# curl -I  www.a.com/img
HTTP/1.1 701 
Server: nginx/1.16.1
Date: Mon, 06 Jul 2020 15:01:29 GMT
Content-Length: 0
Connection: keep-alive

[root@localhost ~]# curl -I  www.a.com/img
HTTP/1.1 702 
Server: nginx/1.16.1
Date: Mon, 06 Jul 2020 15:03:22 GMT
Content-Length: 0
Connection: keep-alive

** 说明^~ 这种字符串匹配优先级高于正则**
测试用例2

server{
      listen 80;
       server_name www.a.com a.com *.a.com;
       root /data/nginx/domain/;
       location = / {
        return 701;
        }
        location ^~ /img {
        return 702;
        }
        location ~ /img{
        return 703;
        }
        location ~* /img{
        return 704;
        }
        location /imgs{
        return 705;
        }
        location /{
        return 666;
        }
}

在以上配置文件的基础上

[root@localhost ~]# curl -I  www.a.com/imgs
HTTP/1.1 703 
Server: nginx/1.16.1
Date: Mon, 06 Jul 2020 15:06:18 GMT
Content-Length: 0
Connection: keep-alive

发现703 这里需要注意首先匹配到的应该是location ^~ /img 和 location /imgs这两个字符串匹配,在字符串匹配中,会继续匹配,按照谁匹配多的原则 应该是匹配到 location /imgs 但是!!!location /imgs 这种匹配它的特点就是比正则表达式的优先级低 所以它又会去找正则表达式, 这个时候就找到了 location ~ /img 和location ~* /img ,正则都匹配的情况下,去看谁的顺序在前面就选址谁!!
另外还需要注意的是字符串匹配 匹配的是以XXX开头的,XXX后面有没有都能匹配到
字符串匹配需要比较长度 正则比较看先后,location /imgs 这种优先级低于正则!!!!

location 示例
做为静态服务

location ^~ /static/ {
 root /webroot/static/;
}
location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {
 root /webroot/res/;
}

做为代理服务器
至少要有一条规则,可以在其他规则都不匹配时能生效

location / {
 proxy_pass http://tomcat:8080 ;
}

回炉重造之 nginx

标签:expec   第一个   引用   用户   idle   pid   文件的   err   私钥   

原文地址:https://www.cnblogs.com/ZFBG/p/13258268.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!