安全运维-Nginx服务器就该这么玩~_安全角度考虑 nginx 如何运行(1)

docker exec -it container_id bash

使用

nginx -h

查看命令帮助

nginx version: nginx/1.21.6
Usage: nginx [-?hvVtTq] [-s signal] [-p prefix]
             [-e filename] [-c filename] [-g directives]

Options:
  -?,-h         : this help
  -v            : show version and exit
  -V            : show version and configure options then exit
  -t            : test configuration and exit
  -T            : test configuration, dump it and exit
  -q            : suppress non-error messages during configuration testing
  -s signal     : send signal to a master process: stop, quit, reopen, reload
  -p prefix     : set prefix path (default: /etc/nginx/)
  -e filename   : set error log file (default: /var/log/nginx/error.log)
  -c filename   : set configuration file (default: /etc/nginx/nginx.conf)
  -g directives : set global directives out of configuration file

常用关闭 检查配置文件 重载配置文件

nginx -s stop
nginx -t
nginx -s reload

Nginx配置文件

nginx.conf和default.conf,一般default.conf是写全局配置,nginx.conf会include一下conf.d目录下的default.conf

Nginx配置文件结构

...              #全局块

events {         #events块
   ...
}

http      #http块
{
    ...   #http全局块
    server        #server块
    { 
        ...       #server全局块
        location [PATTERN]   #location块
        {
            ...
        }
        location [PATTERN] 
        {
            ...
        }
    }
    server
    {
      ...
    }
    ...     #http全局块
}

1、全局块:配置影响nginx全局的指令。一般有运行nginx服务器的用户组,nginx进程pid存放路径,日志存放路径,配置文件引入,允许生成worker process数等。
2、events块:配置影响nginx服务器或与用户的网络连接。有每个进程的最大连接数,选取哪种事件驱动模型处理连接请求,是否允许同时接受多个网路连接,开启多个网络连接序列化等。
3、http块:可以嵌套多个server,配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置。如文件引入,mime-type定义,日志自定义,是否使用sendfile传输文件,连接超时时间,单连接请求数等。
4、server块:配置虚拟主机的相关参数,一个http中可以有多个server。
5、location块:配置请求的路由,以及各种页面的处理情况。

全局块

user:配置用户或者组
worker_processes:允许生成的进程数,默认auto
error_log:指定错误日志路径,级别。这个设置可以放入全局块,http块,server块,级别为:

  1. debug
  2. info
  3. notice
  4. warn
  5. error
  6. crit
  7. alert
  8. emerg
    pid:指定nginx进程运行文件存放地址

events块

accept_mutex:设置网路连接序列化,防止惊群现象发生,默认为on
multi_accept :设置一个进程是否同时接受多个网络连接,默认为off
use:事件驱动模型,
9. select
10. poll
11. kqueue
12. epoll resig
13. dev
14. poll
15. eventport
worker_connections :最大连接数,默认为1024

http全局块

include:文件扩展名与文件类型映射表,默认/etc/nginx/mime.types
default_type:默认文件类型,默认为application/octet-stream
sendfile:允许sendfile方式传输文件,默认为on,可以在http块,server块,location块。
sendfile_max_chunk :每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。
keepalive_timeout:连接超时时间,默认为65s,可以在http,server,location块。
log_format:日志格式,常见的有:

  • 1.$remote_addr 与 $http_x_forwarded_for 用来记录nginx反向代理的ip地址和真实的客户端的ip地址;
  • 2.$remote_user :用来记录客户端用户名称;
  • 3.$time_local : 用来记录访问时间与时区;
  • 4.$request : 用来记录请求的url与http协议;
  • 5.$status : 用来记录请求状态,例如,成功是200;
  • 6.$body_bytes_sent :记录发送给客户端文件主体内容大小;
  • 7.$http_referer :用来记录从那个页面链接访问过来的;
  • 8.$http_user_agent :记录客户端浏览器的相关信息;

access_log:访问日志设置**

server全局块

keepalive_requests:单连接请求上限次数。
listen:监听端口,默认80
server_name:监听地址。默认localhost
error_page:错误页

location块

root:网站根目录,默认/usr/share/nginx/html
index:设置默认页 index.html
deny:拒绝的ip
allow:允许的ip
proxy_pass:请求转向mysvr 定义的服务器列表

proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_connect_timeout 60:nginx跟后端服务器连接超时时间(代理连接超时)
proxy_read_timeout 60:连接成功后,与后端服务器两个成功的响应操作之间超时时间(代理接收超时)
proxy_buffer_size 4k:设置代理服务器(nginx)从后端realserver读取并保存用户头信息的缓冲区大小,默认与proxy_buffers大小相同,其实可以将这个指令值设的小一点
proxy_buffers 4 32k:proxy_buffers缓冲区,nginx针对单个连接缓存来自后端realserver的响应,网页平均在32k以下的话,这样设置
proxy_busy_buffers_size 64k:高负荷下缓冲大小(proxy_buffers*2)
proxy_max_temp_file_size:当 proxy_buffers 放不下后端服务器的响应内容时,会将一部分保存到硬盘的临时文件中,这个值用来设置最大临时文件大小,默认1024M,它与 proxy_cache 没有关系。大于这个值,将从upstream服务器传回。设置为0禁用。
proxy_temp_file_write_size 64k:当缓存被代理的服务器响应到临时文件时,这个选项限制每次写临时文件的大小。
proxy_temp_path(可以在编译的时候)指定写到哪那个目录。

自带配置文件解读

default.conf
server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    #access\_log /var/log/nginx/host.access.log main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #error\_page 404 /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/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;
    #}
}

监听端口80,服务器为localhost,这也是为什么我前面让各位读者打开80端口访问Nginx
网站根目录为:/usr/share/nginx/html,主页为index.html或index.htm,错误页类似

nginx.conf
user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

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

    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  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp\_nopush on;

    keepalive_timeout  65;

    #gzip on;

    include /etc/nginx/conf.d/*.conf;
}

用户为nginx,worker进程数自动
错误日志默认目录为/var/log/nginx/error.log,级别为notice
nginx运行文件存储位置:/var/run/nginx.pid
最大连接数为1024
包含MIME类型:/etc/nginx/mime.types
默认类型为:application/octet-stream
log日志格式为:请查看前面http全局块
json格式比较方便,现在常用,Nginx日志也可以设置为json格式,例如

    log_format log_json '{"@timestamp": "$time\_local", '
                        '"remote\_addr": "$remote\_addr", '
                        '"referer": "$http\_referer", '
                        '"request": "$request", '
                        '"status": $status, '
                        '"bytes": $body\_bytes\_sent, '
                        '"agent": "$http\_user\_agent", '
                        '"x\_forwarded": "$http\_x\_forwarded\_for", '
                        '"up\_addr": "$upstream\_addr",'
                        '"up\_host": "$upstream\_http\_host",'
                        '"up\_resp\_time": "$upstream\_response\_time",'
                        '"request\_time": "$request\_time"'
                        ' }';
 
    access_log  logs/access.log log_json; # 引用日志格式名称

反向代理与负载均衡实战

熟悉我的朋友都知道,我讲完理论是要实践的,这里就来实践一下反向代理与负载均衡。整个环境如下:
在这里插入图片描述

80端口可以访问到两个服务器,8080端口和8081端口分别能访问next_demo1和next_demo2
nginx.conf 如下:

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

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

    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  /var/log/nginx/access.log  main;

    sendfile        on;

    keepalive_timeout  65;

    # 负载均衡
    upstream next_demo{


为了做好运维面试路上的助攻手,特整理了上百道 **【运维技术栈面试题集锦】** ,让你面试不慌心不跳,高薪offer怀里抱!

这次整理的面试题,**小到shell、MySQL,大到K8s等云原生技术栈,不仅适合运维新人入行面试需要,还适用于想提升进阶跳槽加薪的运维朋友。**

![](https://img-blog.csdnimg.cn/img_convert/9034f5b2a2acd63846e471ec5b65df8f.png)

本份面试集锦涵盖了

*   **174 道运维工程师面试题**
*   **128道k8s面试题**
*   **108道shell脚本面试题**
*   **200道Linux面试题**
*   **51道docker面试题**
*   **35道Jenkis面试题**
*   **78道MongoDB面试题**
*   **17道ansible面试题**
*   **60道dubbo面试题**
*   **53道kafka面试**
*   **18道mysql面试题**
*   **40道nginx面试题**
*   **77道redis面试题**
*   **28道zookeeper**

**总计 1000+ 道面试题, 内容 又全含金量又高**

*   **174道运维工程师面试题**

> 1、什么是运维?

> 2、在工作中,运维人员经常需要跟运营人员打交道,请问运营人员是做什么工作的?

> 3、现在给你三百台服务器,你怎么对他们进行管理?

> 4、简述raid0 raid1raid5二种工作模式的工作原理及特点

> 5、LVS、Nginx、HAproxy有什么区别?工作中你怎么选择?

> 6、Squid、Varinsh和Nginx有什么区别,工作中你怎么选择?

> 7、Tomcat和Resin有什么区别,工作中你怎么选择?

> 8、什么是中间件?什么是jdk?

> 9、讲述一下Tomcat8005、8009、8080三个端口的含义?

> 10、什么叫CDN?

> 11、什么叫网站灰度发布?

> 12、简述DNS进行域名解析的过程?

> 13、RabbitMQ是什么东西?

> 14、讲一下Keepalived的工作原理?

> 15、讲述一下LVS三种模式的工作过程?

> 16、mysql的innodb如何定位锁问题,mysql如何减少主从复制延迟?

> 17、如何重置mysql root密码?

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化的资料的朋友,可以点击这里获取!](https://bbs.csdn.net/topics/618542503)**

**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**
么叫网站灰度发布?

> 12、简述DNS进行域名解析的过程?

> 13、RabbitMQ是什么东西?

> 14、讲一下Keepalived的工作原理?

> 15、讲述一下LVS三种模式的工作过程?

> 16、mysql的innodb如何定位锁问题,mysql如何减少主从复制延迟?

> 17、如何重置mysql root密码?

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化的资料的朋友,可以点击这里获取!](https://bbs.csdn.net/topics/618542503)**

**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**
  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值