编译安装nginx

1.基础配置

# Nginx 服务配置及证书目录
mkdir -p /opt/env/nginx/servs
mkdir -p /opt/env/nginx/certs

# Nginx 日志目录
mkdir -p /ware/logs/nginx/
chmod -R 777 /ware/logs/nginx/

# 下载及操作目录
mkdir /root/nginx
cd /root/nginx

2.安装依赖

# 安装依赖
yum install -y wget zip unzip patch
yum install -y gcc gcc-c++
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel

3.解压安装包

cd /root/nginx
unzip nginx_upstream_check_module.zip
unzip nginx-upstream-fair.zip
unzip nginx-module-vts.zip
unzip ngx_http_substitutions_filter_module.zip
tar zxvf nginx-1.16.1.tar.gz

(需要包的可以私信我)

4.编译安装

# 编译安装
cd /root/nginx/nginx-upstream-fair-master
sed -i 's/default_port/no_port/g' ngx_http_upstream_fair_module.c
patch -p1 < /root/nginx/nginx_upstream_check_module-master/upstream_fair.patch

cd /root/nginx/nginx-1.16.1
patch -p1 < /root/nginx/nginx_upstream_check_module-master/check_1.16.1+.patch

cd /root/nginx/nginx-1.16.1
./configure \
--prefix=/usr/local/nginx \
--with-http_realip_module \
--with-http_ssl_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-stream \
--with-http_stub_status_module \
--add-module=/root/nginx/nginx_upstream_check_module-master \
--add-module=/root/nginx/nginx-upstream-fair-master \
--add-module=/root/nginx/nginx-module-vts-master \
--add-module=/root/nginx/ngx_http_substitutions_filter_module-master

make && make install

5.配置文件:/usr/local/nginx/conf/nginx.conf

pid /usr/local/nginx/nginx.pid;

worker_processes 4;
worker_cpu_affinity 1000 0100 0010 0001;
worker_rlimit_nofile 102400;

events {
    worker_connections 102400;
    multi_accept on;
    use epoll;
}

http {

    vhost_traffic_status_zone;
    
    log_format json_format '{"timestamp":"$msec",'
        '"time_iso":"$time_iso8601",'
        '"time_local":"$time_local",'
        '"request_time":"$request_time",'
        '"remote_user":"$remote_user",'
        '"remote_addr":"$remote_addr",'
        '"http_x_forwarded_for":"$http_x_forwarded_for",'
        '"request":"$request",'
        '"status":"$status",'
        '"body_bytes_send":"$body_bytes_sent",'
        '"upstream_addr":"$upstream_addr",'
        '"upstream_response_time":"$upstream_response_time",'
        '"upstream_http_content_type":"$upstream_http_content_type",'
        '"upstream_http_content_disposition":"$upstream_http_content_disposition",'
        '"upstream_status":"$upstream_status",'
        '"http_user_agent":"$http_user_agent",'
        '"http_referer":"$http_referer",'
        '"connection":"$connection",'
        '"connection_requests":"$connection_requests",'
        '"scheme":"$scheme",'
        '"host":"$host",'
        '"http_via":"$http_via",'
        '"request_id":"$request_id"}';

    map $time_iso8601 $logdate {
        '~^(?<ymd>\d{4}-\d{2}-\d{2})' $ymd;
        default  'date-not-found';
    }

    # 日志文件名中带有变量时由子进程创建,需要子进程具有目录写入权限
    access_log /ware/logs/nginx/access-$logdate.log json_format;
    error_log /ware/logs/nginx/error.log error;

    server_tokens off;
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 30;
    keepalive_requests 100000;
    client_header_timeout 10;
    client_body_timeout 10;
    client_max_body_size 100m;
    reset_timedout_connection on;
    send_timeout 10;
    include mime.types;
    default_type application/octet-stream;
    charset UTF-8;

    gzip on;
    gzip_vary on;
    gzip_disable "MSIE [1-6].";
    gzip_http_version 1.0;
    gzip_comp_level 4;
    # gzip_static on;
    gzip_min_length 1024;
    gzip_buffers 4 16k;
    gzip_proxied expired no-cache no-store private auth;
    gzip_types text/plain text/css text/javascript application/x-javascript application/xml application/json application/xml+rss;

    open_file_cache max=100000 inactive=20s;
    open_file_cache_valid 30s;
    open_file_cache_min_uses 2;
    open_file_cache_errors on;
    proxy_connect_timeout 75;
    proxy_read_timeout 300;
    proxy_send_timeout 300;
    proxy_buffer_size 64k;
    proxy_buffers 4 64k;
    proxy_busy_buffers_size 128k;
    proxy_temp_file_write_size 128k;
   
    include /opt/env/nginx/servs/*.upstreams;
    include /opt/env/nginx/servs/http-*.conf;
}

stream {
    include /opt/env/nginx/servs/tcp-*.conf;
}

6.服务脚本:/usr/lib/systemd/system/nginx.service

[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

7.启动&自启动

systemctl enable nginx
systemctl start nginx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

运维小斌

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

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

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

打赏作者

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

抵扣说明:

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

余额充值