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.conf的http块中加入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块
相关推荐
xixingzhe29 小时前
Nginx 看懂请求耗时瓶颈
服务器·前端·nginx
北风toto12 小时前
Windows 下用 OpenSSL 生成 SSL 密钥并配置 Nginx 教程
windows·nginx·ssl
卜锦元14 小时前
Docker Compose 部署 Nginx 1.30.6 完整指南
nginx·docker·容器
小小龙学IT16 小时前
Nginx 开源 Web 服务器与反向代理深度解
前端·nginx·开源
Coisinier1 天前
docker命令管理镜像及容器
linux·运维·nginx·docker·容器·操作系统
2301_32241428041 天前
基于web的校园失物招领管理系统59695-计算机毕设 原创(免费领源码+部署教程+开发环境)
mysql·nginx·php·apache·thinkphp·phpstudy·wampserver
flytalei2 天前
从YAML到Nginx服务访问:动手理解 Kubernetes Pod、Deployment 与 Service
运维·nginx·kubernetes
健康活着就好2 天前
# Nginx负载均衡四种策略,从原理到配置
nginx
Vitaliy Kostrov(柯维远)3 天前
谁来承担额外工作?CDN 参与者如何协商资源
nginx·缓存·系统架构·开源·cdn