nginx-1.24编译安装

本文讲述了在Linux环境下,如何上传文件、初始化环境,配置并编译Nginx1.24.0及其模块过程中遇到的错误,涉及ngx_http_sticky_module.c中的编译错误,以及通过替换配置文件和应用patch解决的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.创建压缩包存放路径

[root@localhost /]# mkdir -p /apps/scripts
[root@localhost /]# cd /apps/scripts/

2.上传所需文件

在这里插入图片描述
在这里插入图片描述

3.初始化环境

[root@localhost scripts]# yum install -y yum-utils device-mapper-persistent-data lvm2 wget net-tools nfs-utils lrzsz gcc gcc-c++ make cmake libxml2-devel openssl-devel curl curl-devel unzip sudo libaio-devel wget vim ncurses-devel autoconf automake zlib-devel  python-devel epel-release openssh-server socat  ipvsadm conntrack telnet ipvsadm vim

在这里插入图片描述

4.压缩包解压

[root@localhost scripts]# tar -zxf nginx-1.24.0.tar.gz 
[root@localhost scripts]# tar -zxf nginx-module-vts-0.2.2.tar.gz 
[root@localhost scripts]# tar -zxf ngx_brotli.tgz 
[root@localhost scripts]# tar -zxf openssl-1.1.1w.tar.gz 
[root@localhost scripts]# tar -zxf pcre-8.45.tar.gz 
[root@localhost scripts]# tar -zxf zlib-1.3.tar.gz 
[root@localhost scripts]# unzip nginx-sticky-module-ng-08a395c66e42.zip 
[root@localhost scripts]# unzip ngx_http_proxy_connect_module-master.zip

5.检测编译环境加模块

[root@localhost scripts]# cd /apps/scripts/nginx-1.24.0
[root@localhost nginx-1.24.0]# ./configure --prefix=/apps/nginx --modules-path=/apps/nginx/modules --with-compat --with-stream --with-stream_ssl_module --with-stream_realip_module --with-stream_ssl_preread_module --with-threads --with-file-aio --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_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-zlib=/apps/scripts/zlib-1.3  --with-pcre=/apps/scripts/pcre-8.45  --with-openssl=/apps/scripts/openssl-1.1.1w  --add-module=/apps/scripts/nginx-goodies-nginx-sticky-module-ng-08a395c66e42  --add-module=/apps/scripts/nginx-module-vts-0.2.2 --add-module=/apps/scripts/ngx_brotli  

在这里插入图片描述

6.编译

[root@localhost nginx-1.24.0]# make

6.1 编译出现异常
出现报错,详细如下:

/apps/scripts/nginx-goodies-nginx-sticky-module-ng-08a395c66e42/ngx_http_sticky_module.c: In function ‘ngx_http_init_sticky_peer’:
/apps/scripts/nginx-goodies-nginx-sticky-module-ng-08a395c66e42/ngx_http_sticky_module.c:207:54: error: ‘ngx_http_headers_in_t’ has no member named ‘cookies’
  if (ngx_http_parse_multi_header_lines(&r->headers_in.cookies, &iphp->sticky_conf->cookie_name, &route) != NGX_DECLINED) {
                                                      ^
/apps/scripts/nginx-goodies-nginx-sticky-module-ng-08a395c66e42/ngx_http_sticky_module.c:207:2: error: passing argument 2 of ‘ngx_http_parse_multi_header_lines’ from incompatible pointer type [-Werror]
  if (ngx_http_parse_multi_header_lines(&r->headers_in.cookies, &iphp->sticky_conf->cookie_name, &route) != NGX_DECLINED) {
  ^
In file included from /apps/scripts/nginx-goodies-nginx-sticky-module-ng-08a395c66e42/ngx_http_sticky_module.c:9:0:
src/http/ngx_http.h:106:18: note: expected ‘struct ngx_table_elt_t *’ but argument is of type ‘struct ngx_str_t *’
 ngx_table_elt_t *ngx_http_parse_multi_header_lines(ngx_http_request_t *r,
                  ^
/apps/scripts/nginx-goodies-nginx-sticky-module-ng-08a395c66e42/ngx_http_sticky_module.c:207:2: error: too few arguments to function ‘ngx_http_parse_multi_header_lines’
  if (ngx_http_parse_multi_header_lines(&r->headers_in.cookies, &iphp->sticky_conf->cookie_name, &route) != NGX_DECLINED) {
  ^
In file included from /apps/scripts/nginx-goodies-nginx-sticky-module-ng-08a395c66e42/ngx_http_sticky_module.c:9:0:
src/http/ngx_http.h:106:18: note: declared here
 ngx_table_elt_t *ngx_http_parse_multi_header_lines(ngx_http_request_t *r,
                  ^
cc1: all warnings being treated as errors
make[1]: *** [objs/addon/nginx-goodies-nginx-sticky-module-ng-08a395c66e42/ngx_http_sticky_module.o] Error 1
make[1]: Leaving directory `/apps/scripts/nginx-1.24.0'
make: *** [build] Error 2

在这里插入图片描述
6.2 异常处理
查询相关资料,该报错可能是因为nginx版本与ngx_http_proxy_connect_module-master中patch版本不符导致。以下为版本对应图:
在这里插入图片描述
参考上述对应图,部署nginx-1.24需指定为proxy_connect_rewrite_102101.patch
安装patch命令
[root@localhost nginx-1.24.0]# yum -y install patch.x86_64

[root@localhost nginx-1.24.0]# patch -p1 < /apps/scripts/ngx_http_proxy_connect_module-master/patch/proxy_connect_rewrite_102101.patch
在这里插入图片描述
再次:检测编译环境 加模块及编译
依旧报错:

/apps/scripts/nginx-goodies-nginx-sticky-module-ng-08a395c66e42/ngx_http_sticky_module.c: In function ‘ngx_http_init_sticky_peer’:
/apps/scripts/nginx-goodies-nginx-sticky-module-ng-08a395c66e42/ngx_http_sticky_module.c:207:54: error: ‘ngx_http_headers_in_t’ has no member named ‘cookies’
  if (ngx_http_parse_multi_header_lines(&r->headers_in.cookies, &iphp->sticky_conf->cookie_name, &route) != NGX_DECLINED) {
                                                      ^
/apps/scripts/nginx-goodies-nginx-sticky-module-ng-08a395c66e42/ngx_http_sticky_module.c:207:2: error: passing argument 2 of ‘ngx_http_parse_multi_header_lines’ from incompatible pointer type [-Werror]
  if (ngx_http_parse_multi_header_lines(&r->headers_in.cookies, &iphp->sticky_conf->cookie_name, &route) != NGX_DECLINED) {
  ^
In file included from /apps/scripts/nginx-goodies-nginx-sticky-module-ng-08a395c66e42/ngx_http_sticky_module.c:9:0:
src/http/ngx_http.h:106:18: note: expected ‘struct ngx_table_elt_t *’ but argument is of type ‘struct ngx_str_t *’
 ngx_table_elt_t *ngx_http_parse_multi_header_lines(ngx_http_request_t *r,
                  ^
/apps/scripts/nginx-goodies-nginx-sticky-module-ng-08a395c66e42/ngx_http_sticky_module.c:207:2: error: too few arguments to function ‘ngx_http_parse_multi_header_lines’
  if (ngx_http_parse_multi_header_lines(&r->headers_in.cookies, &iphp->sticky_conf->cookie_name, &route) != NGX_DECLINED) {
  ^
In file included from /apps/scripts/nginx-goodies-nginx-sticky-module-ng-08a395c66e42/ngx_http_sticky_module.c:9:0:
src/http/ngx_http.h:106:18: note: declared here
 ngx_table_elt_t *ngx_http_parse_multi_header_lines(ngx_http_request_t *r,
                  ^
cc1: all warnings being treated as errors
make[1]: *** [objs/addon/nginx-goodies-nginx-sticky-module-ng-08a395c66e42/ngx_http_sticky_module.o] Error 1
make[1]: Leaving directory `/apps/scripts/nginx-1.24.0'
make: *** [build] Error 2

在这里插入图片描述
继续查询相关资料,有一种思路为替换配置文件,尝试一下是否可行

[root@localhost nginx-1.24.0]# cd /apps/scripts/nginx-goodies-nginx-sticky-module-ng-08a395c66e42/
[root@localhost nginx-goodies-nginx-sticky-module-ng-08a395c66e42]# sed -i "s/ngx_http_parse_multi_header_lines.*/ngx_http_parse_multi_header_lines(r, r->headers_in.cookie, \&iphp->sticky_conf->cookie_name, \&route) != NULL){/g" ngx_http_sticky_module.c

再次:检测编译环境 加模块及编译
在这里插入图片描述
方法可行,没有报错了

7.安装

[root@localhost nginx-1.24.0]# make install

在这里插入图片描述

8.验证

在这里插入图片描述

9.将nginx服务注册为系统服务

编写服务nginx.service,将该文件放置于/etc/systemd/system/下,执行:systemctl daemon-reload重新加载systemd守护进程
系统服务文件中,路径需要根据实际情况调整

[Unit]
Description=nginx web server
Documentation=https://nginx.org/en/docs/
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/apps/nginx/logs/nginx.pid
ExecStartPost=/bin/sleep 0.1
ExecStartPre=/apps/nginx/sbin/nginx -t -c /apps/nginx/conf/nginx.conf
ExecStart=/apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
ExecReload=/apps/nginx/sbin/nginx -s reload -c /apps/nginx/conf/nginx.conf
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=false
[Install]
WantedBy=multi-user.target

10.常用命令

开机自启动:systemctl enable nginx
禁用开机自启动:systemctl disable nginx
启动nginx:systemctl start nginx
停止nginx:systemctl stop nginx
重启nginx:systemctl restart  nginx

nginx范例

worker_processes auto; 
worker_rlimit_nofile 20960; 
error_log  logs/error.log  crit;
events {
	worker_connections  4096;
	multi_accept on;
	accept_mutex on;
	accept_mutex_delay 500ms;
}
http {
    server_tokens off;
    sendfile on;
    tcp_nopush on; 
    tcp_nodelay on;
    access_log off;
    include       mime.types;
    default_type  application/octet-stream;
    keepalive_timeout  300;
    client_max_body_size 10240M;
    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    gzip_comp_level 3;
    gzip_types text/xml text/plain text/css text/javascript application/x-javascript application/javascript application/xml application/json;
    gzip_disable "MSIE [1-6]\.";
    upstream app_server{
        sticky;
		server 13.11.101.11:80 max_fails=300 fail_timeout=30s;
		server 13.11.101.12:80 max_fails=300 fail_timeout=30s;
    }

    # http配置
	server {
	    listen 80;
	    server_name 13.11.103.13;  
	    charset utf-8;
	    location  / {
            proxy_pass http://app_server;
            proxy_set_header Host $host:$server_port;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_redirect off;
            proxy_connect_timeout 300;
            proxy_read_timeout 300;
            proxy_send_timeout 300;
            proxy_buffer_size 2048k;
            proxy_buffers 32 2048k;
            proxy_busy_buffers_size 2048k;
            proxy_max_temp_file_size 1024m;
	    }
	    error_page   500 502 503 504  /50x.html;
	    location = /50x.html {
	        root   html;
	    }
	}
	
	# https配置
	server {
	    listen 443 ssl;
	    ssl_certificate     "/apps/nginx/ssl/app_server.crt";
	    ssl_certificate_key "/apps/nginx/ssl/app_server.key";
	    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
		ssl_session_cache shared:SSL:5m;
        ssl_session_timeout 5m;
        ssl_prefer_server_ciphers on;
	    server_name 13.11.103.13;
	    charset utf-8;
	    location / {
	        proxy_pass http://app_server;
			absolute_redirect off;
	        proxy_set_header Host $http_host; 
	        proxy_set_header X-Real-IP $remote_addr;
	        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
	        proxy_set_header X-Forwarded-Proto $scheme;
	        proxy_redirect     off;
	        proxy_connect_timeout 300;
	        proxy_read_timeout 300;
	        proxy_send_timeout 300;
            proxy_buffers 32 2048k;
            proxy_busy_buffers_size 2048k;
            proxy_max_temp_file_size 1024m;            
        }
		error_page   500 502 503 504  /50x.html;
		location = /50x.html {
		    root   html;
		}
	}
}
### JDK8 和 Nginx-1.24 的配置及兼容性说明 #### JDK8 安装与环境变量设置 为了确保 Java 应用程序能够正常运行,在安装 JDK8 时,需按照特定流程操作。首先解压安装包至指定路径: ```bash tar zxvf jdk-8u361-linux-x64.tar.gz rm -rf /usr/local/java mkdir /usr/local/java mv jdk1.8.0_361 /usr/local/java ``` 随后更新 `/etc/profile` 文件来设定必要的环境变量[^4]。 #### Nginx-1.24 安装与启动 对于 Nginx-1.24 版本而言,其安装过程相对简单直接。完成编译构建之后,通过执行以下指令可实现服务的初始化加载与激活: ```bash cd /usr/local/nginx1.24.0/sbin sudo nginx ``` 这一步骤确保了 Web Server 可以顺利上线提供 HTTP 请求处理能力[^1]。 #### 配合使用场景下的注意事项 当两者共同部署于同一台机器上时,通常情况下不会存在明显的冲突或不兼容现象。然而值得注意的是,如果计划利用 Tomcat 或其他基于 JVM 的应用服务器作为后端处理器,则建议调整 Nginx 的工作模式以及优化参数配置,以便更好地支持高并发请求转发需求。此外,还需关注内存分配策略、线程池大小等方面的内容,从而保障整个系统的稳定性和响应速度。 针对具体的应用场景,可能还需要进一步定制化配置文件中的各项属性值,比如 `worker_processes`, `worker_connections` 等核心选项,这些都取决于实际业务负载特征和个人偏好等因素的影响。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值