linux下编译安装nginx编译常用命令增加模块

已邀请:
yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel gcc 


1、下载nginx,以1.19.6为例

http://nginx.org/en/download.html

2、解压到任意目录,例如/data/nginx


3、切换到nginx解压源码目录编译安装

#其中 --prefix=为编译后nginx安装到的目录



./configure --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module --with-mail --with-mail_ssl_module --user=www --group=www --with-http_gunzip_module --with-http_auth_request_module --with-stream --with-stream_ssl_module --with-threads --with-http_stub_status_module --add-module=/data/software/nginx-goodies-nginx-sticky-module-ng-08a395c66e42


make && make install


4、备份既有配置,添加NCC的推荐配置

#默认安装,nginx会安装到/usr/local/nginx/下

现以处理 /usr/local/nginx/conf/nginx.conf为例

cd  /usr/local/nginx/conf/ && mv nginx.conf nginx.conf_bak


cat  > /usr/local/nginx/conf/nginx.conf << EOF

worker_processes auto;
user nobody;
pid /usr/local/nginx/logs/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
#include /usr/share/nginx/modules/*.conf;

events {
use epoll;
worker_connections 20480;
multi_accept on;
accept_mutex on;
}

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

log_format main '\$remote_addr|\$remote_user|[\$time_local]|"\$request"|'
'\$status|\$body_bytes_sent|\$http_host|"\$http_referer"|'
'"\$http_user_agent"|\$http_x_forwarded_for|\$upstream_cache_status|'
'"\$upstream_http_content_type"|\$request_time|\$upstream_response_time|\$bytes_sent|\$request_length|'
'"\$upstream_addr"';

access_log /tmp/nginx_access.log main;
error_log /tmp/nginx_error.log notice;

sendfile on;
keepalive_timeout 999999;

charset utf-8;
server_tokens off;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 800m;
tcp_nopush on;

underscores_in_headers on;
client_body_buffer_size 512k;
proxy_headers_hash_max_size 51200;
proxy_headers_hash_bucket_size 6400;
proxy_connect_timeout 5;
proxy_read_timeout 999999;
proxy_send_timeout 999999;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
proxy_temp_path /tmp/nginx_proxy_temp;
proxy_cache_path /tmp/nginx_proxy_cache levels=1:2 keys_zone=content:20m inactive=1d max_size=100m;
proxy_cache_path /tmp/nginx_proxy_cache_image levels=1:2 keys_zone=content_image:20m inactive=1d max_size=100m;
proxy_cache_bypass \$http_secret_header;
proxy_ignore_client_abort on;

gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript application/javascript text/css;
gzip_vary on;


upstream nccloud {
sticky;
server 10.10.3.219:81 weight=1 max_fails=1 fail_timeout=5;
server 10.10.3.219:82 weight=1 max_fails=1 fail_timeout=5;
server 10.10.3.219:83 weight=1 max_fails=1 fail_timeout=5;
server 10.10.3.219:84 weight=1 max_fails=1 fail_timeout=5;
server 10.10.3.219:85 weight=1 max_fails=1 fail_timeout=5;
server 10.10.3.219:86 weight=1 max_fails=1 fail_timeout=5;
server 10.10.3.219:90 weight=1 max_fails=1 fail_timeout=5;
server 10.10.3.219:91 weight=1 max_fails=1 fail_timeout=5;
server 10.10.3.219:92 weight=1 max_fails=1 fail_timeout=5;

}

include conf.d/*.conf;


server {
listen 80;
server_name 10.10.3.219 default;
proxy_http_version 1.1;

location / {
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
if (\$request_method = 'OPTIONS'){
return 204;
}
proxy_set_header X-Forwarded-For \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header Connection keep-alive;
proxy_http_version 1.1;
proxy_set_header Upgrade \$http_upgrade;
proxy_set_header X-Forwarded-Host \$http_host;
proxy_set_header Host \$http_host;
proxy_redirect off;
proxy_pass http://nccloud;
}

}



}

EOF

5、处理path

echo "PATH=\$PATH:/usr/local/nginx/sbin" >> ~/.bash_profile

echo "export PATH" >> ~/.bash_profile  

注册成服务:


官方脚本连接:https://www.nginx.com/resources/wiki/start/topics/examples/initscripts/


vi /lib/systemd/system/nginx.service 

#注意:PID的地址一般在nginx.conf中配置,其他nginx 路径参考实际配置 

 
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID # 这行也可以用这个代替 ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target



设置nginx自启动

systemctl enable nginx




查看已编译的模块:nginx -V   



添加外部组件:需要在源码目录编译,通过-add-module=进行添加,例如./configure -add-module=/data/server/nginx-sticky-module 

要回复问题请先登录注册