Nginx的安装配置和使用

最近有好几个地方用到了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)错误页面。
  • 10
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

shanshan3003

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值