Nginx 源码编译和安装

源码编译安装

准备环境:

yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel

我演示使用默认参数安装:

./configure --prefix=/home/zdsoft/nginx

没有报错代表安装完成。

备注:日常生产环境使用nginx,编译模块按照nginx官方yum安装的模块,基本能满足95%以上的生产需求。yum安装模块如下,可自行参考:

# 生产环境使用
./configure --prefix=/opt/xxx/xxx --user=zdsoft --group=zdsoft --with-compat --with-debug --with-file-aio --with-google_perftools_module --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_degradation_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_mp4_module --with-http_perl_module=dynamic --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-http_xslt_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-stream_ssl_preread_module --with-threads --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
  1. ./configure执行完成后,会生成很多中间文件,放在objs目录下面

  2. 在当前命令执行make命令

    make #没有报错代表执行成功

    备注:这个时候,如果是第一次安装,下一步可以执行make install命令,如果是升级,就不能执行install命令。

    这个时候,需要把objs目录下生成nginx二进制文件拷贝到原老版本的nginx目录下

  3. 执行 make install 安装命令

    make install #执行安装命令,第一次安装可以执行,如果是升级,谨慎执行。 make执行完成后生成的中间件文件,都会放在objs/src目录下面

    [root@localhost src]# ll
    总用量 8
    drwxr-xr-x 2 root root 4096 10月 18 17:15 core
    drwxr-xr-x 3 root root  191 10月 18 17:15 event
    drwxr-xr-x 4 root root 4096 10月 18 17:15 http
    drwxr-xr-x 2 root root    6 10月 18 16:37 mail
    drwxr-xr-x 2 root root    6 10月 18 16:37 misc
    drwxr-xr-x 4 root root   31 10月 18 16:37 os
    drwxr-xr-x 2 root root    6 10月 18 16:37 stream
    [root@localhost src]# pwd
    /opt/nginx-1.20.1/objs/src
    [root@localhost src]# 
    
  4. 安装完成,安装目录为:/home/zdsoft/nginx/

    nginx -v  #查看nginx版本
    nginx -V  #查看nginx编译参数,如果没有额外的参数,只会显示configure arguments: --prefix=/home/zdsoft/nginx
    #这情况下,代表是默认安装,可以查看源码目录auto/options 文件。默认安装了哪些模块,哪些没有安装。
    
  5. nginx进程启动

/home/zdsoft/nginx/sbin/nginx  -c /home/zdsoft/nginx/conf/nginx.conf

遇到的问题

  1. ./configure: error: the HTTP rewrite module requires the PCRE library.

    解决:yum -y install pcre-devel

  2. ./configure: error: the HTTP gzip module requires the zlib library.

    解决:yum install -y zlib-devel

UDP转发问题

版本一定要大于1.9.1,编译的时候需要 --with-stream 这个模块支持

使用nginx -V 进行检查

server {
        listen 53 udp reuseport;
        proxy_timeout 20s;
        proxy_pass 192.168.1.23:53;
    }

nginx自启动(centos7)

  1. 在系统服务目录中创建nginx.service文件

vi /usr/lib/systemd/system/nginx.service

  1. 写入内容:
	[Unit]
	Description=nginx
	After=network.target
	
	[Service]
	Type=forking
	ExecStart=/home/zdsoft/nginx/sbin/nginx
	ExecReload=/home/zdsoft/nginx/sbin/nginx -s reload
	ExecStop=/home/zdsoft/nginx/sbin/nginx -s quit
	PrivateTmp=true
	
	[Install]
	WantedBy=multi-user.target
  1. 更新设置

    systemctl daemon-reload

  2. 设置开机自动启动

    systemctl enable nginx.service

  3. 查看nginx状态

    systemctl status nginx.service

  4. 启动nginx

systemctl start nginx.service

websocket 配置

map $http_upgrade $connection_upgrade {
           default upgrade;
           '' close;
       }
location /bcPublishing/ {
               proxy_pass http://192.168.4.121:3721/bcPublishing/;
               keepalive_timeout  3600;
               proxy_http_version  1.1;
               proxy_set_header Upgrade    $http_upgrade;
               proxy_set_header Connection "upgrade";
               proxy_set_header Host   $host;
               proxy_set_header X-Real-IP  $remote_addr;
               proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
           }