Centos 7 安装 Nginx - 配置 & 文件 & 说明

  • 安装基础包
    yum -y install gcc gcc-c++ lrzsz wget vim

  • 安装完成, 查看gcc版本
    gcc -v
    输出:gcc 版本 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)

  • 需要安装 spenssl
    yum install -y openssl openssl-devel
    如果没有可能会报如下错误:

./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.
  • 获取Nginx
    wget -c https://nginx.org/download/nginx-1.14.0.tar.gz

  • 解压 & 进入目录
    tar -zxvf nginx-1.14.0.tar.gz
    cd nginx-1.14.0

  • 使用默认位置
    ./configure

  • 编译安装
    make
    make install

  • 找到Nginx的目录
    whereis nginx

  • 启动Nginx
    /usr/local/nginx/sbin/nginx

Nginx 操作指令

  • nginx -c /etc/nginx/nginx.conf (启动Nginx)
  • nginx -s stop (快速停止)
  • nginx -s quit (完整有序的停止)
  • nginx -s reload (重新加载)
  • nginx -s reopen (重新打开日志文件)
  • nginx -s quit
  • kill -s QUIT 1628
  • ps -ax|grep nginx (获取所有正在运行的Nginx进程列表)
  • nginx -t (验证配置是否有语法错误)
  • nginx -v (查看Nginx的简介版本号)
  • nginx -V (查看Nginx的详细版本号)
  • /usr/local/nginx/sbin/nginx -t (检查配置文件是否正确)
  • /usr/local/nginx/sbin/nginx -s reload (刷新)

获取所有正在运行的Nginx进程列表

  • ps -ax|grep nginx

配置 Nginx 开机启动

修改权限:chmod 755 /etc/rc.d/rc.local
编辑:vim /etc/rc.d/rc.local 添加 /usr/local/nginx/sbin/nginx

#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.

touch /var/lock/subsys/local
/usr/local/nginx/sbin/nginx

Nginx配置文件的结构及说明:

# 运行用户
user root;

# 工作进程的数量 通常设置和CPU数量相等 auto:自动调整
worker_processes auto;

# 全局的错误日志
error_log /var/log/nginx/error.log;

# 日志级别, 从左到右(由低到高): debug做详细, crit最少, 默认是: crit
# debug|info|notice|warn|error|crit|alert|emerg:
# 生产场景一般用: warn|error|crit; 注意: 不要配置info级别较低的等级, 会带来大量的磁盘I/O消耗

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

# 日志没有配置 默认
#error_log  logs/error.log error;

# 不记录日志可以这么写
#error_log /dev/null

# PID文件 记录Nginx主进程的ID号
pid /run/nginx.pid;

# 指定工作模式及连接数上限
events {
	
	# use 用来指定Nginx的工作模式
	# Nginx支持的工作模式有select、poll、kqueue、epoll、rtsig和/dev/poll
	# epoll是多路复用IO(I/O Multiplexing)中的一种方式
    # 仅用于linux2.6以上内核,可以大大提高nginx的性能
    use epoll; 

    # 单个工作进程进程可以允许同时建立外部连接的数量
    # 进程的最大并发连接数: 并发总数 = worker_processes * worker_connections;
    worker_connections 1024;
}

http {
    include mime.types;
    default_type application/octet-stream;

    # 设定日志格式
    # 格式变量解释:
    # $remote_addr: 记录访问网站的客户端地址
    # $remote_user: 远程客户端用户名
    # $time_local: 记录访问时间和时区
    # $request: 用户的Http请求起始行信息
    # $status: http状态码, 记录请求返回的状态码
    # $body_bytes_sent: 服务器发送给客户端的相应body字节数
    # $http_referer: 记录此次请求是从那个连接访问过来的, 可以根据此参数进行防盗链设置
    # $http_user_agent: 记录客户端访问信息, 例如:浏览器、手机客户端等
    # $http_x_forwarded_for: 当前端有代理服务时, 设置Web节点记录客户端地址的配置, 此参数生效的前提是代理服务器也要进行相关的x_forwarded_for设置
    #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  logs/access.log  main

    # sendfile 是Linux 2.0+ 以后推出的一个系统调用
    # sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来传输文件
    # 对于普通应用, 必须设为 on
    # 如果用来进行下载等应用磁盘IO重负载应用, 可设置为 off
    # 对比一般的数据网络传输sendfile会有更少的切换和更少的数据拷贝
    # 在nginx配置文件里面, 打开sendfile on选项能够提高 web server 性能
    sendfile        on;

    # 在nginx中, tcp_nopush必须和sendfilep搭配使用, 它可以配置一次发送数据包的大小, 它不是按时间累计0.2秒后发送包, 而是当包累计到一定大小后就发送
    # 在nginx中, tcp_nopush和tcp_nodelay是互斥的
    #tcp_nopush     on;
    #tcp_nodelay     on;

    # 连接超时时间
    #keepalive_timeout  0;
    keepalive_timeout  65;

    # 开启 gzip 压缩 (on | off) 默认: off
    gzip  on;

    # 设置用于处理请求压缩的缓冲区数量和大小
    gzip_buffer  32 4k | 16 8k

    # 压缩级别(1-9), 越大的压缩率越高, 同时消耗CPU资源也越多
    gzip_comp_level  3;

    # 压缩协议版本, 默认是1.1 (1.0 | 1.1)
    gzip_http_version  1.1;

    # 小于1k的资源不压缩
    gzip_min_length  1k;

    # 当nginx处于反向代理的时候启用(off | expired | no-cache | no-store | private | no_last_modified | no_etag | auth | any ...)
    # off: 禁止所有代理请求的压缩
    # expired: 在Header中包含'Expires'头信息, 启用压缩
    # no-cache: 在Header中包含'Cache-Control:no-cache'头信息, 启用压缩
    # no-store: 在Header中包含'Cache-Control:no-store'头信息, 启用压缩
    # private: 在Header中包含'Cache-Control:private'头信息, 启用压缩
    # no_last_modified: 在Header中包含'Last-Modified'头信息, 启用压缩
    # no_etag: 在Header中包含'ETag'头信息, 启用压缩
    # auth: 在Header中包含'Authorization'头信息, 启用压缩
    # any: 无条件压缩所有结果数据
    # 默认: off
    gzip_proxied  off;

    # 需要压缩哪些响应类型的资源, 多个空格隔开, 不建议压缩图片(mime-type ...)
    # 默认: text/html
    gzip_types  text/plain application/javascript application/x-javascript text/javascript text/xml text/css;

    # 配置禁用gzip条件, 支持正则, 此处表示IE6以下不开启gzip, 因为低版本的IE不支持
    gzip_disable  "MSIE [1-6].";

    # 是否添加"Vary:Accept-Encoding"响应头(on | off)
    # 默认: off
    gzip_vary  on;

    # 设置客户端请求头的缓冲区大小
    client_header_buffer_size  1k;

    # 读取客户端读取请求头超时时间
    client_header_timeout  60s;

    # 设置用于读取大型客户端请求头的缓冲区的最大数 和 大小
    large_client_header_buffers 4 8k;

    # 设定虚拟主机配置
    server {
        
        # 侦听端口
        listen      80 default_server;

        # 服务的名称: 使用example.net www.example.net访问
        server_name example.net www.example.net;

        # 设置网站在服务器上的根目录
        root        /data/www

        # 设置缓冲日志写入的路径 格式 和 配置
        # 默认: access_log logs/access.log combined;
        access_log

        # 默认请求
        location = / {

            # 定义首页索引文件的名称
            # 默认: index index.html;
            index index.html;
        }

        # 定义错误页面
        error_page 404             /404.html;
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }
}
posted @ 2020-03-30 16:27  Mr-Lim  阅读(794)  评论(0编辑  收藏  举报