nginx docker目录结构和配置文件(nginx.conf)

nginx目录结构

  • 配置文件目录:/etc/nginx/
  • 核心配置文件:/etc/nginx/nginx.conf

进入容器的bash环境

docker exec -it nginx bash

浏览/etc/nginx/目录

ls /etc/nginx/

nginx.conf内容

user  nginx;
worker_processes  1;   # 值越大并发能力越强,一般是cpu核心的2倍

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;   # 值越大并发能力越强
}


http {
    include       /etc/nginx/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  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;   # 引入该目录下的所有 .conf 后缀的文件
}

配置文件分为:

  • 全局块
  • event块
  • http块

全局块

从配置文件开始到events块之间的内容,主要包括:

  • user nginx;:配置运行 nginx 服务器的用户、用户组
  • worker_processes 1;:worker_processes表示并发处理量,值越大,处理量也越大。受硬件服务器、操作系统影响
  • pid /run/nginx.pid;:进程pid存放路径和名字

event块

影响nginx与用户的网络连接

  • worker_connections 1024 ;: 值越大并发能力越强

http块(重要)

用于配置:

  • 代理、反向代理
  • 缓存
  • 日志保存位置
  • 第三方模块配置

该部分又分为:

  • http全局块
  • server块,可有多个

http全局块

  • 文件引入,如:include /etc/nginx/conf.d/*.conf;(重要)
  • MIME-TYPE定义,include /etc/nginx/mime.types;
  • 日志定义,如:access_log /var/log/nginx/access.log main;
  • 连接超时时间,如:keepalive_timeout 65;

server块

与虚拟主机配置相关,如:将请求转发给 Tomcat

server块保存在 /etc/nginx/conf.d/目录下的xxx.conf文件中,这些文件很重要

/etc/nginx/conf.d/*.conf (重要)

浏览该目录:

ls /etc/nginx/conf.d/

执行结果如下:

default.conf

默认只有一个文件

default.conf 核心配置文件

查看该文件内容:

cat default.conf

执行结果如下:

server {
    listen       80;   # 监听所有ipv4的80端口
    listen  [::]:80;    # 监听所有ipv6的80端口
    server_name  localhost;   # 匹配域名。主机绑定多个域名时使用

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {   # 通用匹配路径
        root   /usr/share/nginx/html;   # 接收到请求后,到该路径查找资源
        index  index.html index.htm;   # 默认首页是 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;   # http错误代码对应的错误页面是50x.html
    location = /50x.html {
        root   /usr/share/nginx/html;   # 50x.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;
    #}
}

default.conf 配置了监听端口、匹配域名、查找资源的信息,是核心配置文件

注意: 该文件是包含在nginx.conf配置文件中的,是nginx.conf的一部分,从根本来说nginx.conf是真正的核心配置文件