静态编译nginx

目录

背景

难点

2种解决方法

1. 去掉NSS功能,缺点是不能以root权限运行

2. 依赖musl库


背景

为了便于离线安装nginx,不依赖任何第三方库

难点

官方的nginx依赖 Name Service Switch, 这个NSS允许系统管理员重新配置系统以使用外部源来获取通常从 /etc/passwd、/etc/shadow、/etc/hosts、/etc/groups、/etc/resolv.conf 等查询的内容。不能编译成完全静态的可执行文件。

2种解决方法

1. 去掉NSS功能,缺点是不能以root权限运行

需要打补丁:

diff --git a/src/core/nginx.c b/src/core/nginx.c
index 3213527..f61aed7 100644
--- a/src/core/nginx.c
+++ b/src/core/nginx.c
@@ -1030,29 +1030,10 @@ ngx_core_module_init_conf(ngx_cycle_t *cycle, void *conf)
 #if !(NGX_WIN32)
 
     if (ccf->user == (uid_t) NGX_CONF_UNSET_UINT && geteuid() == 0) {
-        struct group   *grp;
-        struct passwd  *pwd;
-
-        ngx_set_errno(0);
-        pwd = getpwnam(NGX_USER);
-        if (pwd == NULL) {
-            ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
-                          "getpwnam(\\"" NGX_USER "\\") failed");
-            return NGX_CONF_ERROR;
-        }
-
-        ccf->username = NGX_USER;
-        ccf->user = pwd->pw_uid;
-
-        ngx_set_errno(0);
-        grp = getgrnam(NGX_GROUP);
-        if (grp == NULL) {
-            ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
-                          "getgrnam(\\"" NGX_GROUP "\\") failed");
-            return NGX_CONF_ERROR;
-        }
-
-        ccf->group = grp->gr_gid;
+        ngx_set_errno(ENOSYS);
+        ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
+                      "getpwnam(\\"" NGX_USER "\\") disabled for static build");
+        return NGX_CONF_ERROR;
     }
 
 
@@ -1122,9 +1103,6 @@ ngx_set_user(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
 
     ngx_core_conf_t  *ccf = conf;
 
-    char             *group;
-    struct passwd    *pwd;
-    struct group     *grp;
     ngx_str_t        *value;
 
     if (ccf->user != (uid_t) NGX_CONF_UNSET_UINT) {
@@ -1143,29 +1121,10 @@ ngx_set_user(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
 
     ccf->username = (char *) value[1].data;
 
-    ngx_set_errno(0);
-    pwd = getpwnam((const char *) value[1].data);
-    if (pwd == NULL) {
-        ngx_conf_log_error(NGX_LOG_EMERG, cf, ngx_errno,
-                           "getpwnam(\\"%s\\") failed", value[1].data);
-        return NGX_CONF_ERROR;
-    }
-
-    ccf->user = pwd->pw_uid;
-
-    group = (char *) ((cf->args->nelts == 2) ? value[1].data : value[2].data);
-
-    ngx_set_errno(0);
-    grp = getgrnam(group);
-    if (grp == NULL) {
-        ngx_conf_log_error(NGX_LOG_EMERG, cf, ngx_errno,
-                           "getgrnam(\\"%s\\") failed", group);
-        return NGX_CONF_ERROR;
-    }
-
-    ccf->group = grp->gr_gid;
-
-    return NGX_CONF_OK;
+    ngx_set_errno(ENOSYS);
+    ngx_conf_log_error(NGX_LOG_EMERG, cf, ngx_errno,
+                       "getpwnam(\\"%s\\") disabled for static build", value[1].data);
+    return NGX_CONF_ERROR;
 
 #endif
 }
diff --git a/src/core/ngx_inet.c b/src/core/ngx_inet.c
index 96a04fd..4ab2f7f 100644
--- a/src/core/ngx_inet.c
+++ b/src/core/ngx_inet.c
@@ -1111,95 +1111,9 @@
 }
 
 
-#if (NGX_HAVE_GETADDRINFO && NGX_HAVE_INET6)
-
-ngx_int_t
-ngx_inet_resolve_host(ngx_pool_t *pool, ngx_url_t *u)
-{
-    u_char           *host;
-    ngx_uint_t        n;
-    struct addrinfo   hints, *res, *rp;
-
-    host = ngx_alloc(u->host.len + 1, pool->log);
-    if (host == NULL) {
-        return NGX_ERROR;
-    }
-
-    (void) ngx_cpystrn(host, u->host.data, u->host.len + 1);
-
-    ngx_memzero(&hints, sizeof(struct addrinfo));
-    hints.ai_family = AF_UNSPEC;
-    hints.ai_socktype = SOCK_STREAM;
-#ifdef AI_ADDRCONFIG
-    hints.ai_flags = AI_ADDRCONFIG;
-#endif
-
-    if (getaddrinfo((char *) host, NULL, &hints, &res) != 0) {
-        u->err = "host not found";
-        ngx_free(host);
-        return NGX_ERROR;
-    }
-
-    ngx_free(host);
-
-    for (n = 0, rp = res; rp != NULL; rp = rp->ai_next) {
-
-        switch (rp->ai_family) {
-
-        case AF_INET:
-        case AF_INET6:
-            break;
-
-        default:
-            continue;
-        }
-
-        n++;
-    }
-
-    if (n == 0) {
-        u->err = "host not found";
-        goto failed;
-    }
-
-    /* MP: ngx_shared_palloc() */
-
-    for (rp = res; rp != NULL; rp = rp->ai_next) {
-
-        switch (rp->ai_family) {
-
-        case AF_INET:
-        case AF_INET6:
-            break;
-
-        default:
-            continue;
-        }
-
-        if (ngx_inet_add_addr(pool, u, rp->ai_addr, rp->ai_addrlen, n)
-            != NGX_OK)
-        {
-            goto failed;
-        }
-    }
-
-    freeaddrinfo(res);
-    return NGX_OK;
-
-failed:
-
-    freeaddrinfo(res);
-    return NGX_ERROR;
-}
-
-#else /* !NGX_HAVE_GETADDRINFO || !NGX_HAVE_INET6 */
-
 ngx_int_t
 ngx_inet_resolve_host(ngx_pool_t *pool, ngx_url_t *u)
 {
-    u_char              *host;
-    ngx_uint_t           i, n;
-    struct hostent      *h;
     struct sockaddr_in   sin;
 
     /* AF_INET only */
@@ -1210,37 +1124,8 @@
     sin.sin_addr.s_addr = ngx_inet_addr(u->host.data, u->host.len);
 
     if (sin.sin_addr.s_addr == INADDR_NONE) {
-        host = ngx_alloc(u->host.len + 1, pool->log);
-        if (host == NULL) {
-            return NGX_ERROR;
-        }
-
-        (void) ngx_cpystrn(host, u->host.data, u->host.len + 1);
-
-        h = gethostbyname((char *) host);
-
-        ngx_free(host);
-
-        if (h == NULL || h->h_addr_list[0] == NULL) {
-            u->err = "host not found";
-            return NGX_ERROR;
-        }
-
-        for (n = 0; h->h_addr_list[n] != NULL; n++) { /* void */ }
-
-        /* MP: ngx_shared_palloc() */
-
-        for (i = 0; i < n; i++) {
-            sin.sin_addr.s_addr = *(in_addr_t *) (h->h_addr_list[i]);
-
-            if (ngx_inet_add_addr(pool, u, (struct sockaddr *) &sin,
-                                  sizeof(struct sockaddr_in), n)
-                != NGX_OK)
-            {
-                return NGX_ERROR;
-            }
-        }
-
+        u->err = "gethostbyname disabled for static build";
+        return NGX_ERROR;
     } else {
 
         /* MP: ngx_shared_palloc() */
@@ -1256,8 +1141,6 @@
     return NGX_OK;
 }
 
-#endif /* NGX_HAVE_GETADDRINFO && NGX_HAVE_INET6 */
-
 
 static ngx_int_t
 ngx_inet_add_addr(ngx_pool_t *pool, ngx_url_t *u, struct sockaddr *sockaddr,
diff --git a/src/os/unix/ngx_process_cycle.c b/src/os/unix/ngx_process_cycle.c
index 5817a2c..8262993 100644
--- a/src/os/unix/ngx_process_cycle.c
+++ b/src/os/unix/ngx_process_cycle.c
@@ -833,12 +833,6 @@ ngx_worker_process_init(ngx_cycle_t *cycle, ngx_int_t worker)
             exit(2);
         }
 
-        if (initgroups(ccf->username, ccf->group) == -1) {
-            ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
-                          "initgroups(%s, %d) failed",
-                          ccf->username, ccf->group);
-        }
-
 #if (NGX_HAVE_PR_SET_KEEPCAPS && NGX_HAVE_CAPABILITIES)
         if (ccf->transparent && ccf->user) {
             if (prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0) == -1) {

参考: https://github.com/ericpruitt/static-glibc-nginx

2. 依赖musl库

musl libc

musl 从头开始设计用于静态链接,功能和效率跟glibc一样,宗旨:“一次编写到处运行”

wanglong001/static-glibc-nginx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 下载nginx源码 首先,我们需要从nginx官网下载最新版本的nginx源码,下载地址:http://nginx.org/en/download.html 2. 安装依赖 在编译安装nginx之前,需要安装一些依赖库,如gcc、pcre、openssl、zlib等。 可以使用以下命令安装这些依赖: ``` sudo apt-get update sudo apt-get install gcc sudo apt-get install libpcre3 libpcre3-dev sudo apt-get install libssl-dev sudo apt-get install zlib1g-dev ``` 3. 解压源码 将下载好的nginx源码包解压到指定目录中,如: ``` tar -zxvf nginx-1.16.1.tar.gz ``` 4. 配置编译选项 进入解压后的nginx源码目录,执行以下命令进行配置: ``` ./configure --prefix=/usr/local/nginx \ --with-http_ssl_module \ --with-http_stub_status_module \ --with-http_gzip_static_module \ --with-pcre \ --with-stream \ --with-stream_ssl_module \ --with-http_realip_module \ --with-http_v2_module ``` 上述命令中,各选项的含义如下: --prefix:指定nginx安装目录。 --with-http_ssl_module:启用SSL支持。 --with-http_stub_status_module:启用stub_status模块,用于显示nginx的状态信息。 --with-http_gzip_static_module:启用gzip_static模块,用于预压缩静态文件。 --with-pcre:启用PCRE库,用于正则表达式匹配功能。 --with-stream:启用stream模块,用于TCP/UDP流量控制。 --with-stream_ssl_module:启用stream模块的SSL支持。 --with-http_realip_module:启用realip模块,用于获取真实客户端IP。 --with-http_v2_module:启用HTTP/2协议支持。 5. 编译安装 配置完成后,执行以下命令进行编译安装: ``` make sudo make install ``` 6. 启动nginx服务 安装完成后,可以使用以下命令启动nginx服务: ``` sudo /usr/local/nginx/sbin/nginx ``` 7. 检查nginx是否启动成功 可以在浏览器中输入 http://localhost,如果出现"Welcome to nginx!"的页面,则表示nginx已经成功安装和启动。 至此,nginx的源码编译安装完成。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值