全国旗舰校区

不同学习城市 同样授课品质

北京

深圳

上海

广州

郑州

大连

武汉

成都

西安

杭州

青岛

重庆

长沙

哈尔滨

南京

太原

沈阳

合肥

贵阳

济南

下一个校区
就在你家门口
+
当前位置:首页  >  技术干货

Nginx默认配置详解

发布时间:2023-11-21 15:53:29
发布人:xqq

一、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;
        }
    } 
  

map迭代器

相关文章

Vim字符串替换详细指南

Vim字符串替换详细指南

2023-11-21
CSS Blur用法介绍

CSS Blur用法介绍

2023-11-21
详解192.168.81

详解192.168.81

2023-11-21
SQL中不等于的表示方法

SQL中不等于的表示方法

2023-11-21

最新文章

武汉新媒体行业公司排名

武汉新媒体行业公司排名

2023-11-01
武汉新媒体就业现状好吗

武汉新媒体就业现状好吗

2023-11-01
武汉全媒体行业发展现状及趋势

武汉全媒体行业发展现状及趋势

2023-10-31
武汉全媒体现状

武汉全媒体现状

2023-10-31
在线咨询 免费试学 教程领取