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
相关推荐
运维小白。。28 分钟前
Nginx 反向代理
运维·服务器·nginx·http
卑微的码蚁4 小时前
ngxin
nginx
BLEACH-heiqiyihu10 小时前
红帽9中nginx-源码编译php
运维·nginx·php
这个需求建议不做11 小时前
vue3打包配置 vite、router、nginx配置
前端·nginx·vue
&星辰入梦来&11 小时前
Nginx从入门到入土(三): 静态资源管理与代理服务
运维·nginx·负载均衡
苹果醋313 小时前
SpringCloud系列之一---搭建高可用的Eureka注册中心
运维·nginx
丶213616 小时前
【Nginx】在 Docker 上安装 Nginx 的详细指南
运维·nginx·docker
ZHOU西口1 天前
微服务实战系列之玩转Docker(十五)
nginx·docker·微服务·云原生·swarm·docker swarm·dockerui
苹果醋31 天前
react 路由 react-router/react-router-dom
运维·nginx
ps酷教程2 天前
nginx进阶篇(二)
nginx