Nginx系列(一)安装部署及基本配置解析

一、linux编译安装nginx

1.1 环境准备

shell>yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel

1.2 官方下载压缩包

官方地址:nginx.org

shell>mkdir -p /data/app
shell>wget http://nginx.org/download/nginx-1.23.1.tar.gz
shell>tar -zxvf nginx-1.23.1.tar.gz
shell>mv nginx-1.23.1 nginx-install
cd nginx-install

1.3 源码包安装

shell>./configure --prefix=/data/app/nginx --with-http_ssl_module --with-http_stub_status_module --with-http_v2_module --with-http_gzip_static_module --with-poll_module --with-http_realip_module --with-stream  --with-stream_ssl_module
shell>make install

1.4 设置环境变量

shell>vim /etc/profile
####追加一下内容####
export NGINX_HOME=/data/app/nginx
export PATH=$PATH:$NGINX_HOME/sbin
shell>source /etc/profile

1.5 nginx的相关命令

  • 启动命令:nginx
  • 重启命令:nginx -s reload
  • 关闭命令:nginx -s stop
  • 优雅退出:nginx -s quite(所有请求结束才关闭)

二、Nginx的最小配置


worker_processes  1;


events {
    worker_connections  1024;
}

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

    sendfile        on;

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }

        
        location = /50x.html {
            root   html;
        }    
    }

}

三 设置Nginx服务

3.1 创建nginx.service

进入/etc/systemd/system文件夹,新增文件 nginx.service

[Unit]
Description=nginx service
After=network.target

[Service]
User=root
Type=forking
ExecStart=/data/app/nginx/sbin/nginx
ExecReload=/data/app/nginx/sbin/nginx -s reload
ExecStop=/data/app/nginx/sbin/nginx -s stop
ExecStartPre=/bin/sleep 10

[Install]
WantedBy=multi-user.target

3.2设置开启开机启动

shell>systemctl enable nginx

3.3运行停止

3.3.1 启动nginx服务

shell>systemctl start nginx.service

3.3.2 重新启动服务

shell>systemctl restart nginx.service

3.3.3 查看服务当前状态

shell>systemctl status nginx.service

3.3.4 停止开机自启动

shell>systemctl disable nginx.service

四、Nginx按日期切割日志

worker_processes  1;


events {
    worker_connections  1024;
}

http {
    include       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" $upstream_addr $upstream_response_time $request_time $uri http_origin:$http_origin';

    #这里是启动日志以日期的方式存储
    map $time_iso8601 $logdate {
        '~^(?<ymd>\d{4}-\d{2}-\d{2})' $ymd;
        default 'date-not-found';
    }
    sendfile        on;

    server {
        listen       80;
        server_name  localhost;
        access_log  logs/doc.access-$logdate.log main;
        error_log   logs/doc.error-$logdate.log;
        location / {
            root   html;
            index  index.html index.htm;
        }

        
        location = /50x.html {
            root   html;
        }    
    }

}

发表回复