nginx配置文件详解(含宝塔多站点配置文件结构)

文章目录

      • [nginx 配置文件位置](#nginx 配置文件位置)
      • [nginx 配置文件说明](#nginx 配置文件说明)
      • 设置多个配置文件

nginx 配置文件位置

  1. 源码编译安装方式

    在安装目录的conf目录下,比如安装目录是/etc/nginx,那么就在/etc/nginx/conf

  2. apt 安装或 yum 安装

    在安装目录的根路径下,通常在/etc/nginx/nginx.conf

  3. 使用宝塔安装

    一般在/www/server/nginx/conf/nginx.conf

  4. 使用命令查找

    sudu find / -name nginx.conf

nginx 配置文件说明

  1. 默认的 config

    shell 复制代码
    #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 文件结构

    shell 复制代码
    # 全局块
    # something
    
    # evnets块
    events {
        # something
    }
    
    # http块
    http {
        # http全局块
        # something
    
        # server块
        server {
            # location块
            location [PATTERN] {
                # something
            }
    
            # location块
            location [PATTERN] {
                # something
            }
        }
    
        # server块
        server {}
    }
    描述
    全局 影响 nginx 全局的指令
    events nginx 服务器或用户的网络连接
    http 可以嵌套多个 server,配置代理,缓存,日志定义等绝大多数功能和第三方模块配置
    server 配置虚拟主机的相关参数,可以配置多个 location
    location 配置请求的路由,以及各种页面的处理情况
  3. 配置详细说明

    shell 复制代码
    ########### 每个指令必须有分号结束。#################
    #user administrator administrators;  #配置用户或者组,默认为nobody nobody。
    #worker_processes 2;  #允许生成的进程数,默认为1
    #pid /nginx/pid/nginx.pid;   #指定nginx进程运行文件存放地址
    error_log log/error.log debug;  #制定日志路径,级别。这个设置可以放入全局块,http块,server块,级别以此为:debug|info|notice|warn|error|crit|alert|emerg
    events {
        accept_mutex on;   #设置网路连接序列化,防止惊群现象发生,默认为on
        multi_accept on;  #设置一个进程是否同时接受多个网络连接,默认为off
        #use epoll;      #事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
        worker_connections  1024;    #最大连接数,默认为512
    }
    http {
        include       mime.types;   #文件扩展名与文件类型映射表
        default_type  application/octet-stream; #默认文件类型,默认为text/plain
        #access_log off; #取消服务日志
        log_format myFormat '$remote_addr--$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; #自定义格式
        access_log log/access.log myFormat;  #combined为日志格式的默认值
        sendfile on;   #允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。
        sendfile_max_chunk 100k;  #每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。
        keepalive_timeout 65;  #连接超时时间,默认为75s,可以在http,server,location块。
    
        upstream mysvr {
        server 127.0.0.1:7878;
        server 192.168.10.121:3333 backup;  #热备
        }
        error_page 404 https://www.baidu.com; #错误页
        server {
            keepalive_requests 120; #单连接请求上限次数。
            listen       4545;   #监听端口
            server_name  127.0.0.1;   #监听地址
            location  ~*^.+$ {       #请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。
            #root path;  #根目录
            #index vv.txt;  #设置默认页
            proxy_pass  http://mysvr;  #请求转向mysvr 定义的服务器列表
            deny 127.0.0.1;  #拒绝的ip
            allow 172.18.5.54; #允许的ip
            }
        }
    }
    1. $remote_addr$http_x_forwarded_for:记录客户端的 ip 地址
    2. $remote_user:记录客户端用户名称
    3. $time_local:记录访问时间与时区
    4. $request:记录请求的 url 与 http 协议
    5. $status:记录请求状态,成功是 200
    6. $body_bytes_sent:记录发送给客户都安文件主体内容大小
    7. $http_referer:记录页面请求来源
    8. $http_user_agent:记录客户端浏览器的相关信息

设置多个配置文件

假设nginx配置文件路径为/etc/nginx/nginx.conf

  1. /etc/nginx下创建conf.d,用来存自定义conf文件

  2. nginx.confhttp块中加入include

    shell 复制代码
    #修改为auto
    worker_processes  auto;
    
    http {
        server {
            # something
        }
    
        ##加入以下神秘代码
        include /etc/nginx/conf.d/*.conf;
    }
  3. 然后在conf.d下定义*.conf文件

    shell 复制代码
    server {
        listen       80;
        server_name  127.0.0.1;
    
        location = /test {
            default_type text/html;
            return 200  'good';
        }
    }

拿宝塔的多站点nginx配置文件举例

  1. 宝塔有一个根nginx配置文件,在/www/server/nginx/conf/nginx.conf,他里面有一个配置项 include /www/server/panel/vhost/nginx/*.conf;
  2. 然后部署了多个站点的情况下,每个站点有一个单独的nginx配置文件,对应的就是在如下目录中有多个配置文件 /www/server/panel/vhost/nginx
  3. 每个站点的配置文件只是配置http中的1个server
相关推荐
Evan芙4 小时前
Nginx 安装教程(附Nginx编译安装脚本)
windows·nginx·postgresql
invicinble5 小时前
nginx的基本认识
运维·nginx
爆肝疯学大模型5 小时前
http转https,免费快速申请证书并实现nginx配置
nginx·http·https
qinyia5 小时前
通过 Wisdom SSH AI 助手部署和配置 Nginx Web 服务器
人工智能·nginx·ssh
嘻哈baby5 小时前
Nginx反向代理与负载均衡实战指南
运维·nginx·负载均衡
二哈喇子!19 小时前
openFuyao 容器平台快速入门:Nginx 应用部署全流程实操
运维·nginx·openfuyao
J2虾虾1 天前
上传文件出现“ 413 Request Entity Too Large“错误
nginx
枫叶梨花1 天前
Nginx HTTPS代理大文件加载失败的排查与解决方案
nginx
albert-einstein1 天前
Nginx越界读取缓存漏洞CVE-2017-7529(参考peiqi文库以及gpt)
gpt·nginx·缓存
serve the people1 天前
滑块验证完整实现教程(前端 + 后端 + Nginx 集成)
运维·前端·nginx