安装nginx并安全地配置和启动


摘要:centos中的nginx安装教程,以低权限用户启动nginx,使用防火墙转发流量解决非root用户无法使用1024以下端口问题,nologin用户执行命令的方法。
文章目录见右侧



一、安装nginx

>>参考文章<<

1.1 下载

# 如果centos服务器是最小体量安装,则先安装weget

yum install -y wget

#下载nginx,截至写稿最新是1.18.0

cd /home

wget http://nginx.org/download/nginx-1.18.0.tar.gz

tar -zxvf nginx-1.18.0.tar.gz

cd nginx-1.18.0



1.2 安装编译依赖

yum install -y pcre pcre-devel openssl openssl-devel gcc gcc gcc-c++ ncurses-devel perl



1.3 提前下载第三方依赖

编译时的模块越多越好,如果安装了以后还想加模块,就得重新编译

第三方依赖:

# more_clear_headers
wget https://github.com/openresty/headers-more-nginx-module/archive/v0.33.tar.gz
#解压
tar -zxvf v0.33.tar.gz



1.4 编译

#可选编译步骤:去掉debug,提高一点编译速度

#vim auto/cc/gcc

#在179行这样注释掉 CFLAGS="$CFLAGS -g" 使用:set nu显示行号

./configure --prefix=/usr/local/nginx --user=nobody --group=nobody --with-http_ssl_module --with-http_sub_module \
--with-http_gzip_static_module --with-http_stub_status_module --with-http_gunzip_module \
--with-stream --with-stream_ssl_module \
--add-module=/headers-more-nginx-module-0.33

ssl是支持https,sub_module和gzip、gunzip用于代理时修改报文中src的链接地址什么的

headers-more-nginx-module用于清除响应的http headers

详细信息使用./configure --help

#编译

make -j8  # 根据cpu最大线程数决定,为cpu核心的2倍,减少编译时间



1.5 安装

make install

#安装成功

#安装目录

cd /usr/local/nginx



二、配置教程

安全地配置nginx,主要是以低权限用户运行nginx,这样即使出现漏洞,攻击者也难以反弹shell



2.1 创建用户和组

groupadd nologin

useradd nginx-nologin -M -s /sbin/nologin -g nologin

#-M 不指定家目录

#-s 指定shell ,nologin代表该用户无法用于登录,但是可以指定以此用户执行命令,详情在后面

#-g 指定组

觉得麻烦直接用系统自带的 nobody 用户,本文后续使用nobody用户



2.2 获取https证书

这是可选的,如果你还没有域名,则需先购买域名,国内??购买域名还需要备案。

如果你有域名,那么推荐使用https,证书可以免费申请,无需备案。

以阿里云为例,你使用域名申请后,推荐使用文件验证,因为dns验证的话需要等dns传播。验证通过一分钟左右就会审核通过,接着下载证书文件,要下载nginx的。下载后解压重命名为cert.pemcert.key,传到服务器/usr/local/nginx/conf/目录下。
后续配置中取消掉有关ssl的配置的注释。



2.3 配置防火墙

如果操作系统是最小安装,没有防火墙的话,还需要安装防火墙

可以运行命令检测是否安装

iptables        
firewalld        #centos7默认

如果两个都报错找不到命令的话需要安装,推荐firewalld

yum install -y firewalld 

systemctl enable firewalld        #开机启动

systemctl start firewalld        #启动

接下来是防火墙配置

#开放端口

firewall-cmd --zone=public --add-port=80/tcp --permanent

firewall-cmd --zone=public --add-port=443/tcp --permanent

#流量转发

firewall-cmd --add-forward-port=port=80:proto=tcp:toport=8800 --permanent

firewall-cmd --add-forward-port=port=443:proto=tcp:toport=4433--permanent

#配置立即生效

firewall-cmd --reload



2.4 配置文件权限

这是最后一步,推荐把这些命令存为sh文件,并设置权限755,因为改动文件时可能需要重复执行。

chown -R nobody:nobody /usr/local/nginx

#chmod -R 755 /usr/local/nginx



三、配置nginx.conf

vi /usr/local/nginx/conf/nginx.conf

如果不想麻烦的话可以直接备份原文件,然后使用我下面的配置

#运行用户和组
#user  nginx-nologin nologin;
user nobody;
worker_processes  1;

#去掉注释
error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#去掉注释
pid        logs/nginx.pid;


events {
    worker_connections  1024;
}

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

    #隐藏Server信息,再处理好各报错页面,nmap也扫不出你是啥服务
    #more_clear_headers Server;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        #使用8800,非root用户无法使用1024以下端口,后续使用防火墙转发流量
        listen       8800;
        server_name  localhost;

        #charset koi8-r;

        ###强制http转https
        #return 301 https://blog.yunmuq.xyz:4433;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        error_page  404              /404.html;
        # http请求https端口
        error_page  497              /400.html;
        # 错误的请求,http不规范,比如用nc连接,设置这个可以避过nmap扫描
        error_page  400              /400.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

    ###流量控制,没有https也可以用在http
    #开辟10m内存存储相关信息,访问频率限制为一秒3次,访问频率超过会返回500状态码
    #limit_req_zone $binary_remote_addr zone=mylimit:10m rate=3r/s;

    # HTTPS server
    #
    #server {
    #    listen      4433  ssl;
    #    server_name  localhost;

        #流量控制
        #继承上面的配置,并且新增一个缓冲区能缓存2次请求
        #limit_req zone=mylimit burst=2;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #    error_page  404              /404.html;
    
    ### 497错误即400状态码中的,错误地使用http协议请求https端口
    #    error_page  497              /400.html;

        # redirect server error pages to the static page /50x.html
        #
    #    error_page   500 502 503 504  /50x.html;
    #    location = /50x.html {
    #        root   html;
    #    }

    #}

}



3.1 配置路由,添加前后缀

location 是匹配请求,语法:

location [=|~|~*|^~] /uri/ {… }

符号 含义
= 精确匹配
^~ 非正则匹配
~ 正则匹配(区分大小写)
~* 正则匹配(不区分大小写)
!~ 正则不匹配(区分大小写)
!~* 正则不匹配(不区分大小写)
普通匹配(这里没有符号的时候)

匹配后缀可以用:location ~* \.(gif|jpg|png)$ { ... }

匹配并代理具有前缀的接口:

比如想通过http://127.0.0.1:80/lib访问http://127.0.0.1:8080/lib

        location ^~ /prefix/ {
            # 设置路由的目标(上游)地址
            proxy_pass http://127.0.0.1:3000/;
        }

而接口本身没有前缀,代理时为其添加前缀就复杂一点

比如想通过http://127.0.0.1:80/lib访问http://127.0.0.1:8080/

        location ^~ /prefix/ {
            # 设置路由的目标(上游)地址
            proxy_pass http://127.0.0.1:3000/prefix/;
            # 当出现跳转时,修改报文的Location header
            proxy_redirect / /prefix/;

            # 如果sub_filter不生效,可能是服务器启用了zip压缩
            proxy_set_header Accept-Encoding '';
            #gunzip on;
            #gzip off;
            # 修改html中的链接
            sub_filter 'href="/' 'href="/prefix/';
            sub_filter 'src="/' 'src="/prefix/';
            sub_filter_types *;
            sub_filter_once  off;
        }



3.2 添加http header

header配置location内的会覆盖server中的

使用

add_header Content-Type text/html;

default_type 'text/html; charset=UTF-8';

真正要设置Content-Type第一句是无法生效的,因为nginx默认配置有default_type application/octet-stream;,使用add_header只会让响应出现两个Content-Type

所以我们要设置Content-Type的话要在location中使用default_type去覆盖



3.3 清除http header

为什么要删除http header,为了安全,最常见的就是隐去Server不暴露nginx版本

其次,还有一些高级玩法,比如静态页面伪装成动态

more_clear_headers

使用模块headers-more-nginx-module

前文描述了如何下载安装,如果已经安装了nginx,想添加模块,重新编译,但是使用--add-dynamic-module动态模块,这样在编译目录下找到/objs/ngx_http_headers_more_filter_module.so

配置文件中加上

load_module /home/nginx-1.20.2/objs/ngx_http_headers_more_filter_module.so;

http {

   more_clear_headers Server;

......

        location ~ ^.*\.jsp$ {
            # 把静态资源伪装成动态
            # 浏览器缓存相关
            more_clear_headers Last-Modified;
            more_clear_headers ETag;
            # 静态文件下载-断点续传
            more_clear_headers Accept-Ranges;



四、启动nginx

一切准备就绪,现在可以启动,推荐把启动命令保存为sh文件,并设置权限755

这里演示了nologin用户如何执行命令

启动和重启:


#su即使用另一个用户执行命令,-s指定了shell,-c是命令内容

#nginx -c是检查配置是否正确

su -s /bin/bash -c "/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf" nobody

#-s reload用于运行时重载配置文件无需停止,也可以用于启动

su -s /bin/bash -c "/usr/local/nginx/sbin/nginx -s reload" nobody

建议修改配置后都 -c 重新加载配置文件再 -s reload

如果要停止,可以使用

su -s /bin/bash -c "/usr/local/nginx/sbin/nginx -s quit" nobody

查看版本用-v,查看详细信息,安装了哪些模块用-V




end

往期精彩文章推荐: