千锋教育-做有情怀、有良心、有品质的职业教育机构

手机站
千锋教育

千锋学习站 | 随时随地免费学

千锋教育

扫一扫进入千锋手机站

领取全套视频
千锋教育

关注千锋学习站小程序
随时随地免费学习课程

当前位置:首页  >  技术干货  > Nginx默认配置详解

Nginx默认配置详解

来源:千锋教育
发布人:xqq
时间: 2023-11-21 15:53:29 1700553209

一、nginx默认配置文件

Nginx 安装后默认会提供一个全局的配置文件 nginx.conf。配置文件可以通过命令行指定,也可以由运行 ./nginx 命令的用户名所指定,除此之外,Nginx 可以使用附加的配置文件(.conf)。

nginx.conf 配置文件位于 /etc/nginx 或 /usr/local/nginx/conf 目录中,不同的 Linux 发行版安装路径可能有所不同。通过配置文件的方式,我们可以对 Nginx 进行全面的配置,以满足不同场景的需求。

user  nginx;
worker_processes  auto;
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;
}

二、nginx默认配置报错

当我们在编写 Nginx 配置文件时,如果存在字符错误,语法错误或者引用了不存在目录或文件的路径时,会导致 Nginx 启动失败。

在配置文件或者启动命令出现问题时,可以通过以下方式获取错误日志,以便检查错误:

#检查语法错误
nginx -t

#启动 nginx
nginx

#查看启动是否成功
ps -ef | grep nginx

#获取错误日志文件路径
tail -f /var/log/nginx/error.log

三、nginx配置user值

用户指令,用于指定 Nginx 进程的运行用户。

例如,我们可以通过以下方式让 Nginx 进程以用户名“www”启动:

user www;

四、nginx默认配置文件路径

在 Linux 系统中,Nginx 的默认配置文件存放在 /etc/nginx/nginx.conf。

这里有几种方法可以查找配置文件,如:

使用 find 命令查找:find / -name nginx.conf 使用 which 命令查找可执行文件路径:which nginx 查看 Nginx 的启动配置:cat /lib/systemd/system/nginx.service

五、nginx配置server

在 Nginx 配置文件中,我们需要为每个域名设置一个相应的 server 块配置。

例如,我们可以使用以下代码将 Nginx 配置为监听 80 端口,并处理 example.com 的请求:

server {
    listen 80;
    server_name  example.com;
    root /home/example.com;
    index index.php index.html index.htm;
}

六、nginx默认配置路径

Nginx 默认情况下会在 /etc/nginx 中寻找配置文件,如果有指定 -c 参数,将优先使用指定的配置文件。

七、nginx默认配置文件设置

Nginx 配置文件包含多个配置块,每个块通过花括号进行封闭。

例如,在 http 配置块中添加 server 块:

http {
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }
    }
}

八、nginx 配置详解

Nginx 配置文件分为以下几个部分:

启动配置:

        user username;
        worker_processes num;
        error_log path [level];
        pid path;
  

events 配置:

        events {
            worker_connections num;
            multi_accept on|off;
            use epoll|kqueue|rt|/dev/poll|select;
            accept_mutex on|off;
            accept_mutex_delay time;
        }
  

http 配置:

        http {
            include mime.types;
            default_type application/octet-stream;
            access_log path [format [buffer=size]];
            sendfile on|off;
            tcp_nopush on|off;
            tcp_nodelay on|off;
        }
  

九、nginx负载均衡配置详解

Nginx 负载均衡配置指的是通过 Nginx 配置,将请求分发到多个后端服务器。常见的负载均衡配置方式有:

轮询

    upstream backend {
        server dfault_server_ip:port;
        server second_server_ip:port;
        server thrid_server_ip:port;
    }

    server {
        listen 80;

        location / {
            proxy_pass http://backend;
        }
    } 
  

权重

    upstream backend {
        server dfault_server_ip:port weight=5;
        server second_server_ip:port weight=3;
        server thrid_server_ip:port weight=2;
    }

    server {
        listen 80;

        location / {
            proxy_pass http://backend;
        }
    } 
  

IP 哈希

    upstream backend {
        ip_hash;
        server dfault_server_ip:port;
        server second_server_ip:port;
        server thrid_server_ip:port;
    }

    server {
        listen 80;

        location / {
            proxy_pass http://backend;
        }
    } 
  

tags: map迭代器
声明:本站稿件版权均属千锋教育所有,未经许可不得擅自转载。
10年以上业内强师集结,手把手带你蜕变精英
请您保持通讯畅通,专属学习老师24小时内将与您1V1沟通
免费领取
今日已有369人领取成功
刘同学 138****2860 刚刚成功领取
王同学 131****2015 刚刚成功领取
张同学 133****4652 刚刚成功领取
李同学 135****8607 刚刚成功领取
杨同学 132****5667 刚刚成功领取
岳同学 134****6652 刚刚成功领取
梁同学 157****2950 刚刚成功领取
刘同学 189****1015 刚刚成功领取
张同学 155****4678 刚刚成功领取
邹同学 139****2907 刚刚成功领取
董同学 138****2867 刚刚成功领取
周同学 136****3602 刚刚成功领取
相关推荐HOT