最近有好几个地方用到了nginx,但是一直还没时间记录下nginx的安装、配置和使用,这篇文章可以将这块内容整理出来,方便大家一起学习~
安装
安装是相对简单一些的,直接使用yum即可。
yum install -y nginx
默认安装位置在/usr/sbin/nginx这个位置下
也可以通过命令查询
whereis nginx
会有多个输出

配置
配置文件默认在/etc/nginx下,主配置文件是:nginx.conf

配置内容解读
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/user nginx; #默认用户
worker_processes auto; # 工作进程设定,auto含义是cpu数量-1
error_log /var/log/nginx/error.log; # 定义错误日志的位置
pid /run/nginx.pid; #设定pid的位置# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf; # 加载支持的模块events { #事件worker_connections 1024;
}http { #配置定义的http的信息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; #http请求访问日志的位置sendfile on;tcp_nopush on;tcp_nodelay on;keepalive_timeout 65;types_hash_max_size 4096;include /etc/nginx/mime.types;default_type application/octet-stream;# Load modular configuration files from the /etc/nginx/conf.d directory.# See http://nginx.org/en/docs/ngx_core_module.html#include# for more information.include /etc/nginx/conf.d/*.conf;#扩展配置的路径,例如在对应的路径下增加配置文件即可。server {# 默认监听的端口listen 80;listen [::]:80;server_name _;root /usr/share/nginx/html;# Load configuration files for the default server block.include /etc/nginx/default.d/*.conf; #扩展配置的路径error_page 404 /404.html;location = /404.html { #错误的默认访问页面}error_page 500 502 503 504 /50x.html;location = /50x.html { #错误的默认访问页面}}
worker_processes指定 Nginx 使用的 worker 进程数量。使用auto可以自动根据 CPU 核心数进行调整。events部分定义了 Nginx 的事件模型相关的设置,例如worker_connections指定每个 worker 进程可同时处理的连接数。http部分是配置 HTTP 服务相关的配置项。include mime.types指定了 MIME 类型的配置文件路径,用于解析文件类型。default_type指定了默认的 MIME 类型。sendfile on开启了 sendfile 功能,可以提高文件传输效率。keepalive_timeout指定了 keepalive 连接的超时时间。server定义了一个虚拟主机(server block),其中listen指定了监听的端口和 IP。location /是一个 location 块,指定了请求的 URI 以/开头时的处理方式。root指定了网站文件的根目录。index指定了默认的主页文件。error_page定义了一些错误页面的处理方式,例如 404 页面和 50x(500、502、503、504)错误页面。