Nginx的基础安装与升级改造

659 阅读4分钟

环境准备

  • 操作系统: CentOS 7.7
  • Nginx 版本: 1.24.0

基础安装

  1. 安装依赖

    yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
    
  2. 创建并进入文件夹

    mkdir -p /data/software/nginx && cd /data/software/nginx
    
  3. 下载并解压安装包

    wget http://nginx.org/download/nginx-1.24.0.tar.gz && tar -zxvf nginx-1.24.0.tar.gz && rm -rf nginx-1.24.0.tar.gz
    
  4. 编译并安装

    cd /data/software/nginx/nginx-1.24.0
    ./configure --prefix=/data/software/nginx --with-http_ssl_module --with-http_realip_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_secure_link_module --with-http_v2_module --with-http_stub_status_module --with-http_sub_module --with-http_auth_request_module
    make
    make install
    
  5. 添加环境变量,实现全局配置

    编辑 ~/.bash_profile 文件:

    vim ~/.bash_profile
    

    添加如下命令:

    # nginx
    PATH=$PATH:/data/software/nginx/sbin
    export PATH
    

    然后执行以下命令使变更生效:

    source ~/.bash_profile
    
  6. 启动

    nginx
    

升级改造

升级

设置运行用户

nginx.conf 文件中,将 user 设置为 root

user root;

调整主进程PID路径

nginx.conf 文件中,重新指定进行PID路径

pid /data/software/nginx/sbin/nginx.pid;

调整工作线程

nginx.conf 文件中,将worker_processes设置为auto

worker_processes  auto;

开启访问日志并设置日志格式与路径

http 块中添加:

log_format  main  escape=json '$remote_addr - $remote_user [$time_local.$msec] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" '
'$request_time $upstream_response_time '
'$request_body ';

access_log  /data/logs/nginx/access.log  main;

开启zip压缩

http 块中添加:

gzip  on;
gzip_comp_level 5;
gzip_min_length 1024;
gzip_types application/json text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
gzip_disable "MSIE [1-6]\.";
gzip_vary on;

开启文件上传大小限制

http 块中添加:

client_max_body_size 20m;

改造

基于使用过程中的漏洞修复进行的nginx配置改造

去除接口响应中的Server响应头

截屏2024-12-10 21.49.07.png 如截图所示,响应头中的Server会暴露Nginx与版本,需要进行去除。使用过以下两个方法:

  1. 使用server_tokens参数。可以在http块中配置如下命令:

    server_tokens off;
    

    但是此方案仅会隐藏Server响应头中的Nginx版本而已。

  2. 使用headers-more-nginx-module模块。此方案修复较为复杂,但能完全去除Server响应头:

    1. 创建插件目录

      mkdir -p /data/software/nginx/plugins
      
    2. 下载插件

      cd /data/software/nginx/plugins
      wget https://github.com/openresty/headers-more-nginx-module/archive/refs/tags/v0.34.tar.gz
      tar -zxvf v0.34.tar.gz
      rm -rf v0.34.tar.gz
      
    3. 升级模块

      1. 进入nignx源码
        mkdir -p /data/software/nginx/nginx-1.24.0
        
      2. 获取nginx已编译的内容
        nginx -V
        
      3. 复制【configure arguments】中的内容,并添加新的模块,最后进行编译

        无需执行make install命令

        ./configure --prefix=/data/software/nginx --with-http_ssl_module --with-http_realip_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_secure_link_module --with-http_v2_module --with-http_stub_status_module --with-http_sub_module --with-http_auth_request_module --add-module=/data/software/nginx/plugins/headers-more-nginx-module-0.34
        make
        
      4. 将旧版本的nginx二进制文件进行备份
        mv /data/software/nginx/sbin/nginx /data/software/nginx/sbin/nginx.old
        
      5. 将新版本的nginx二进制文件进行拷贝
        cp /data/software/nginx/nginx-1.24.0/objs/nginx /data/software/nginx/sbin
        
      6. 执行更新安装命令
        make upgrade
        
      7. 执行命令,查看是否有新添加的模块
        nginx -V
        
      8. 重启nginx服务即生效
        nginx -s reload
        

      4.去除Server响应头

      http 块中添加:

      more_clear_headers 'Server';
      

配置HSTS请求头

HTTP Strict Transport Security(HSTS)是一种安全策略,用于强制客户端(如浏览器)只通过HTTPS与服务器建立连接。这种机制有助于保护网站免受SSL剥离攻击和会话劫持等威胁

一般在 http 块中添加:

add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload";

配置Content-Security-Policy请求头

用于提供额外的安全性层,通过制定有效的源列表来限制可以加载资源类型。这是 Web 安全策略的一部分,用于控制页面是否可以在 iframe、object 或 embed 等 HTML 元素中被嵌入。

一般在 location 块中添加:

add_header Content-Security-Policy "frame-ancestors none;";

页面使用Basic Auth

需要使用到http_auth_request_module模块,如果按照上文安装步骤,则无需在意,否则可以参考上文的【升级模块】的步骤来添加此模块

location 块中添加:

auth_basic           "basic auth";
auth_basic_user_file /data/software/nginx/pw/htpasswd; # 指定密码文件

其中htpasswd文件可以使用服务器的httpd-tools软件包来生成账户与密码,可以简单参考一下命令:

  1. 下载安装httpd-tools

    yum install httpd-tools
    
  2. 生成文件

    示例如下,账户为admin、密码为12345test

    htpasswd -c -b /data/software/nginx/pw/htpasswd admin 12345test
    

版本升级

准备

  • 旧版本的 nginx 为 1.26.2,且安装在/data/software/nginx-1.26.2 目录下
  • 新版本的 nginx 为 1.27.0,且安装在/data/software/nginx-1.27.0 目录下

步骤

  1. 下载最新的 nginx 文件

     wget -c http://nginx.org/download/nginx-1.27.0.tar.gz
    
  2. 解压并进入解压后的目录

     tar -zxvf nginx-1.27.0.tar.gz
     cd nginx-1.27.0/
    
  3. 查看 nginx 的配置信息

     nginx -V
    

    复制 configure arguments 中的参数

    202501201104631.png

  4. 编译与安装

     ./configure 后面跟着上一步复制的configure arguments中的参数
     make
    
  5. 关闭旧的 nginx 进程并备份 nginx 文件

     nginx -s stop
     mv /data/software/nginx-1.26.2/sbin/nginx /data/software/nginx-1.26.2/sbin/nginx.old
    
  6. 移动新的 nginx 文件,到旧的 nginx 目录下

     cp /data/software/nginx-1.27.0/objs/nginx /data/software/nginx-1.26.2/sbin/
    
  7. 查看 nginx 的配置信息,检查是否升级成功

     nginx -V
    

    可以查看到 nginx 的版本号

  8. 检查 nginx 配置是否正常

     nginx -t
    
  9. 启动新的 nginx

     nginx