Nginx默认配置详解

发布时间:2023-05-18

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