ubuntu22.04部署nginx 1.22.1

发布时间 2023-03-31 11:34:09作者: 小吉猫

安装依赖

apt -y install gcc make libssl-dev zlib1g-dev libgd-dev libgeoip-dev  libpcre3-dev

创建用户

groupadd -g 33  -r www-data && useradd -M -N -g www-data  -r  -s /bin/false -c "NGINX Server" -u 33  www-data

下载nginx

wget http://nginx.org/download/nginx-1.22.1.tar.gz

修改版本号

cat src/core/nginx.h 

/*
 * Copyright (C) Igor Sysoev
 * Copyright (C) Nginx, Inc.
 */


#ifndef _NGINX_H_INCLUDED_
#define _NGINX_H_INCLUDED_


#define nginx_version      1020002
#define NGINX_VERSION      "1.20.2"
#define NGINX_VER          "nginx/" NGINX_VERSION

#ifdef NGX_BUILD
#define NGINX_VER_BUILD    NGINX_VER " (" NGX_BUILD ")"
#else
#define NGINX_VER_BUILD    NGINX_VER
#endif

#define NGINX_VAR          "NGINX"
#define NGX_OLDPID_EXT     ".oldbin"


#endif /* _NGINX_H_INCLUDED_ */

安装nginx

./configure  --prefix=/usr/local/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --with-poll_module --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-stream --with-stream_ssl_module --with-cc-opt=-Wno-error --with-ld-opt= --user=nginx --group=nginx --with-threads --with-file-aio --http-client-body-temp-path=/usr/local/nginx/client/ --http-proxy-temp-path=/usr/local/nginx/proxy/ --http-fastcgi-temp-path=/usr/local/nginx/fcgi/ --http-uwsgi-temp-path=/usr/local/nginx/uwsgi --http-scgi-temp-path=/usr/local/nginx/scgi --error-log-path=/usr/local/nginx/logs/error.log --http-log-path=/usr/local/nginx/logs/access.log --pid-path=/usr/local/nginx/nginx.pid --lock-path=/usr/local/nginx/nginx.lock --with-pcre
make -j 8 && make install

nginx.service

[Unit]
Description=A high performance web server and a reverse proxy server
Documentation=man:nginx(8) http://nginx.org/en/docs/control.html
After=network.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/nginx.pid
ExecStartPre=//usr/local/nginx/sbin/nginx -t -q -g 'daemon on; master_process on;'
ExecStart=/usr/local/nginx/sbin/nginx -g 'daemon on; master_process on;'
ExecReload=/usr/local/nginx/sbin/nginx -g 'daemon on; master_process on;' -s reload
ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /usr/local/nginx/nginx.pid
TimeoutStopSec=5
KillMode=mixed

[Install]
WantedBy=multi-user.target

nginx.conf

user  www-data;
worker_processes  auto;

worker_rlimit_nofile 65535;
events {
    worker_connections  65535;
    use epoll;
    accept_mutex on;
}


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


    log_format json escape=json '{'
                        '"@timestamp":"$time_iso8601",'
                        '"@source":"$server_addr",'
                        '"@nginx_fields":{'
                            '"http_x_forwarded_for":"$http_x_forwarded_for",'
                            '"request":"$request",'
                            '"status":"$status",'
                            '"body_bytes_sent":"$body_bytes_sent",'
                            '"http_referer":"$http_referer",'
                            '"client":"$remote_addr",'
                            '"request_time":"$request_time",'
                            '"upstream_response_time":"$upstream_response_time",'
                            '"upstream_addr":"$upstream_addr",'
                            '"request_method":"$request_method",'
                            '"domain":"$host",'
                            '"url":"$uri",'
                            '"args":"$args",'
                            '"request_body":"$request_body",'
                            '"http_user_agent":"$http_user_agent",'
                            '"remote_addr":"$remote_addr",'
                            '"proxy_add_x_forwarded_for":"$proxy_add_x_forwarded_for"'
                        '}'
                    '}';


    access_log  logs/access.log  json;

    sendfile        on;
    tcp_nopush     on;
    types_hash_max_size 2048;
    keepalive_timeout  65;
    client_header_buffer_size 4k;
    client_max_body_size 512M;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    aio on;
    gzip  on;
    gzip_vary on;  
    gzip_proxied any;
    gzip_min_length 1k;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_comp_level 6;
    gzip_types  text/plain application/json application/javascript application/x-javascript application/css application/xml application/xml+rss text/javascript application/x-httpd-php image/jpeg image/gif image/png image/x-ms-bmp;

 
    include /usr/local/nginx/conf/vhosts/*.conf;

    server {
        listen       80;
        server_name  localhost;


        location / {
            root   html;
            real_ip_header     X-Forwarded-For;
            index  index.html index.htm index.php;
        }

        location /basic_status {
           stub_status on;
        }


        location ~ ^/(pm_status|ping)$ {
          fastcgi_pass 127.0.0.1:9000;
          fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
          include        fastcgi_params;
        }

        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
   }
}

nginx使用帮助

nginx version: nginx/1.22.1
Usage: nginx [-?hvVtTq] [-s signal] [-p prefix]
             [-e filename] [-c filename] [-g directives]

Options:
  -?,-h         : this help
  -v            : show version and exit
  -V            : show version and configure options then exit
  -t            : test configuration and exit
  -T            : test configuration, dump it and exit
  -q            : suppress non-error messages during configuration testing
  -s signal     : send signal to a master process: stop, quit, reopen, reload
  -p prefix     : set prefix path (default: /usr/local/nginx/)
  -e filename   : set error log file (default: /usr/local/nginx/logs/error.log)
  -c filename   : set configuration file (default: /usr/local/nginx/conf/nginx.conf)
  -g directives : set global directives out of configuration file