从01开始 从01开始
首页
  • 计算机科学导论
  • 数字电路
  • 计算机组成原理

    • 计算机组成原理-北大网课
  • 操作系统
  • Linux
  • Docker
  • 计算机网络
  • 计算机常识
  • Git
  • JavaSE
  • Java高级
  • JavaEE

    • Ant
    • Maven
    • Log4j
    • Junit
    • JDBC
    • XML-JSON
  • JavaWeb

    • 服务器软件
    • Servlet
  • Spring
  • 主流框架

    • Redis
    • Mybatis
    • Lucene
    • Elasticsearch
    • RabbitMQ
    • MyCat
    • Lombok
  • SpringMVC
  • SpringBoot
  • 学习网课的心得
  • 输入法
  • 节假日TodoList
  • 其他
  • 关于本站
  • 网站日记
  • 友人帐
  • 如何搭建一个博客
GitHub (opens new window)

peterjxl

人生如逆旅,我亦是行人
首页
  • 计算机科学导论
  • 数字电路
  • 计算机组成原理

    • 计算机组成原理-北大网课
  • 操作系统
  • Linux
  • Docker
  • 计算机网络
  • 计算机常识
  • Git
  • JavaSE
  • Java高级
  • JavaEE

    • Ant
    • Maven
    • Log4j
    • Junit
    • JDBC
    • XML-JSON
  • JavaWeb

    • 服务器软件
    • Servlet
  • Spring
  • 主流框架

    • Redis
    • Mybatis
    • Lucene
    • Elasticsearch
    • RabbitMQ
    • MyCat
    • Lombok
  • SpringMVC
  • SpringBoot
  • 学习网课的心得
  • 输入法
  • 节假日TodoList
  • 其他
  • 关于本站
  • 网站日记
  • 友人帐
  • 如何搭建一个博客
GitHub (opens new window)
  • JavaSE

  • JavaSenior

  • JavaEE

  • JavaWeb

    • 服务器软件

      • 什么是服务器软件
      • Nginx介绍
      • Nginx的安装和启停
      • Nginx配置
        • 使用配置文件
        • 默认配置文件
        • 全局块
        • events 块
        • http 块
        • 可视化配置Nginx
      • Nginx配置反向代理
      • Nginx配置多个反向代理
      • Nginx配置负载均衡
      • Nginx配置动静分离
      • Nginx集群概念
      • Nginx配置高可用(主从)
      • Nginx原理
      • Nginx日志管理
      • Nginx手册
      • Nginx系列完结
      • Tomcat介绍
      • Tomcat安装和启停
      • Tomcat配置
      • Tomcat部署项目
      • IDEA新建JavaWeb项目
      • Tomcat集群
      • 服务器软件
    • 环境管理和配置管理-科普篇
    • Servlet入门

    • JavaWeb
  • Spring

  • 主流框架

  • SpringMVC

  • SpringBoot

  • Java并发

  • Java源码

  • JVM

  • 韩顺平

  • Java
  • Java
  • JavaWeb
  • 服务器软件
2023-04-17
目录

Nginx配置

# 03.Nginx配置

接下来我们简单讲讲Nginx的配置文件

‍

# 使用配置文件

安装了Nginx后,Nginx的目录结构如下:

# cd /opt/nginx
# ll
总用量 4
drwx------. 2 nobody root    6 3月  26 22:31 client_body_temp
drwxr-xr-x. 2 root   root 4096 3月  26 22:21 conf
drwx------. 2 nobody root    6 3月  26 22:31 fastcgi_temp
drwxr-xr-x. 2 root   root   40 3月  26 22:21 html
drwxr-xr-x. 2 root   root   41 3月  26 22:31 logs
drwx------. 2 nobody root    6 3月  26 22:31 proxy_temp
drwxr-xr-x. 2 root   root   19 3月  26 22:21 sbin
drwx------. 2 nobody root    6 3月  26 22:31 scgi_temp
drwx------. 2 nobody root    6 3月  26 22:31 uwsgi_temp
1
2
3
4
5
6
7
8
9
10
11
12

其中

  • sbin 存放的是可执行文件,也就是我们平时启动Nginx的地方
  • conf 存放的则是各种配置文件
  • logs 存放的是日志文件,例如访问日志,错误日志

‍

我们启动Nginx的时候,可以使用-c​选项指定配置文件:

cd /opt/nginx/sbin
./nginx -c /opt/nginx/conf/nginx.conf
1
2

注意,如果配置文件所在的目录没有权限访问,则不能正常使用Nginx

‍

‍

# 默认配置文件

默认的配置文件是conf/nginx.conf,其中井号 #​开头的是注释,注释有非常多,这里为了方便,我们去掉注释,此时默认配置文件内容如下:

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

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

‍

根据上述文件,我们可以很明显的将 nginx.conf 配置文件分为三部分:

  • 全局块:配置服务器整体运行的配置指令
  • events 块:影响 Nginx 服务器与用户的网络连接
  • http 块:分为 http全局块 和 server块

‍

# 全局块

全剧块指的是从配置文件开始到 events 块之间的内容,主要会设置一些影响 nginx 服务器整体运行的配置指令,主要包括以下配置:

  • 配置运行 Nginx 服务器的用户(组):例如是以root用户运行还是非root的其他用户运行
  • 允许生成的 worker process 数:默认配置里,只配了这个参数,后面会讲什么是worker以及该参数的作用
  • 进程 PID 存放路径
  • 日志存放路径和日志级别(日志级别可选)
  • ........

‍

例如这样配置:

user  nobody;
worker_processes  1;
pid        logs/nginx.pid;
# error_log  logs/error.log;
error_log  logs/error.log  info;
1
2
3
4
5

‍

‍

‍

# events 块

events 块涉及的指令主要影响 Nginx 与用户的网络连接,常用的设置包括

  • 是否开启对多 work process下的网络连接进行序列化,
  • 是否允许同时接收多个网络连接,选取哪种事件驱动模型来处理连接请求,
  • 每个 wordprocess 可以同时支持的最大连接数等。
events {
    worker_connections  1024;
}
1
2
3

上述例子就表示每个 work process 支持的最大连接数为 1024,这部分的配置对 Nginx 的性能影响较大,在实际中应该灵活配置,后续我们再详细介绍

‍

‍

# http 块

这算是 Nginx 服务器配置中最频繁的部分。代理、缓存和日志定义等绝大多数功能和第三方模块的配置都在这里。主要分为 http全局块 和 server块:

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        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;
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

‍

# http 全局块

http 全局块配置的指令包括文件引入、MIME-TYPE 定义、日志自定义、连接超时时间、单链接请求数上限等

include       mime.types;
default_type  application/octet-stream;

#log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
#access_log  logs/access.log  main;

sendfile        on;
#tcp_nopush     on;

#keepalive_timeout  0;
keepalive_timeout  65;

#gzip  on;
1
2
3
4
5
6
7
8
9
10
11
12
13

‍

# server 块

简单来说,server块就是处理请求的。一个server对应一个服务,可以配置多个server,例如一个server负责处理项目1的请求,另一个server负责项目2的请求,这样一个服务器可以处理多个项目的请求,而不用多台服务器分别处理各个项目的请求,节省服务器硬件成本。

用Nginx中一些专业的术语来说的话,每个 server 块就相当于一个虚拟的主机。

‍

每个 server 块也分为全局 server 块,以及可以同时包含多个 location块。

  1. 全局 server 块 最常见的配置是本虚拟机主机的监听配置,和本虚拟主机的名称,或 IP 配置。
  2. location 块 :一个 server 块可以配置多个 location 块。location块的主要作用是基于 Nginx 收到的请求字符串进行匹配,对特定的请求进行处理,例如地址定向、数据缓存和应答控制等功能,还有许多第三方模块的配置也在这里进行。

‍

举个例子:

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14

‍

这个http块中包含一个server块,这个server块监听80端口;然后有一个location块,路径是一个斜线 /​,也就是当用户访问 /​的时候,要怎么处理。

可以配置度多个server块,例如如下配置:

 location / {
    root   html;
    index  index.html index.htm;
}
 location /test {
    root   html;
    index  index.html index.htm;
}
1
2
3
4
5
6
7
8

这样配置了用户访问/​ 和 /test​时,要怎么处理请求,root指的是静态资源的路径(在这里是/opt/nginx/html),index指的是默认的文件。

这样,用户访问 /​ 的时候,Nginx会返回/opt/nginx/html/index.html,也就是我们看到的页面:

​​

‍

# cat /opt/nginx/html/index.html 
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

​​

‍

‍

‍

‍

# location 指令

location的语法如下:

location [ = | ~ | ~* | ^~ ] uri {
}
1
2

1、= :用于不含正则表达式的 uri 前,要求请求字符串与 uri 严格匹配,如果匹配成功,就停止继续向下搜索并立即处理该请求。

2、~:用于表示 uri 包含正则表达式,并且区分大小写。

3、~*:用于表示 uri 包含正则表达式,并且不区分大小写。

4、^~:用于不含正则表达式的 uri 前,要求 Nginx 服务器找到标识 uri 和请求字符串匹配度最高的 location 后,立即使用此 location 处理请求,而不再使用 location块中的正则 uri 和请求字符串做匹配。

注意:如果 uri 包含正则表达式,则必须要有 ~ 或者 ~* 标识。

‍

‍

其实,nginx.conf中的注释就是一个个Nginx的配置例子,后续我们会详细介绍。

‍

# 可视化配置Nginx

由于Nginx的配置非常多,因此市面上出现了不少可视化配置Nginx的工具和网站,例如:NGINXConfig | DigitalOcean (opens new window),开源地址digitalocean/nginxconfig.io (opens new window)(截止到2023-3-29,有25kstart),选择你的场景,填写好参数,就会自动生成配置文件

在GitHub上编辑此页 (opens new window)
上次更新: 2023/4/20 09:11:31
Nginx的安装和启停
Nginx配置反向代理

← Nginx的安装和启停 Nginx配置反向代理→

Theme by Vdoing | Copyright © 2022-2023 粤ICP备2022067627号-1 粤公网安备 44011302003646号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式