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

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

需要这份系统化的资料的朋友,可以点击这里获取!

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

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; # 引用日志格式名称

## 反向代理与负载均衡实战


熟悉我的朋友都知道,我讲完理论是要实践的,这里就来实践一下反向代理与负载均衡。整个环境如下:  
 ![在这里插入图片描述](https://img-blog.csdnimg.cn/335a127800e645ff96c1904d41beda11.png#pic_center)


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{
    server next_demo1:3000 weight=2;
    server next_demo2:3000 weight=1;
}

server {
listen       80;
listen  [::]:80;
server_name  localhost;

# 反向代理
location / {
    root   /usr/share/nginx/html;
    index  index.html index.htm;
    proxy_pass   http://next_demo;
}

location = /50x.html {
    root   /usr/share/nginx/html;
}

}

}


访问结果如下:  
 ![在这里插入图片描述](https://img-blog.csdnimg.cn/cc3f7026a99b4ab1a7fdc5ff01075fef.png#pic_center)  
 ![在这里插入图片描述](https://img-blog.csdnimg.cn/9a1b398fa17c4027a8f60f25ae190423.png#pic_center)  
 **这里只是演示哈,实际应该不暴露后面服务器的端口,两个服务器内容一致。**  
 访问80端口,刷新几次,可以看到 1 2的比例差不多是2:1,也就是负载均衡。查看Nginx日志也能看到。  
 ![在这里插入图片描述](https://img-blog.csdnimg.cn/92eb5001bfc748c49bc5a66567ad3c36.png#pic_center)  
 由于涉及到了docker-compose,搭建的的内容放到了这篇文章:[Docker-docker-compose学习笔记(yaml,实战)](https://bbs.csdn.net/forums/4f45ff00ff254613a03fab5e56a57acb)  
 留个作业,设置下动静分离、正向代理,可以把你的博客放到评论区。


## 参考


[nginx.com](https://bbs.csdn.net/forums/4f45ff00ff254613a03fab5e56a57acb)  
 [nginx.org](https://bbs.csdn.net/forums/4f45ff00ff254613a03fab5e56a57acb)  
 [菜鸟教程-Nginx配置详解](https://bbs.csdn.net/forums/4f45ff00ff254613a03fab5e56a57acb)







**先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前在阿里**

**深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!**

**因此收集整理了一份《2024年最新Linux运维全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。**
![img](https://img-blog.csdnimg.cn/img_convert/926da6a5f1d84628f1e8ebc2286ac6ef.png)
![img](https://img-blog.csdnimg.cn/img_convert/4c2275dd5950856e3625f279709041d4.png)
![img](https://img-blog.csdnimg.cn/img_convert/70c4ce8e17670baf9db993136dd33eaf.png)
![img](https://img-blog.csdnimg.cn/img_convert/67e5e77498d56d0f19f798dd3ef9d0d6.png)
![img](https://img-blog.csdnimg.cn/img_convert/5804e8a6d2e9a55831b35dcf8ce3dc6f.png)

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上运维知识点,真正体系化!**

**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**

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

-1715475357288)]
[外链图片转存中...(img-CdzFJBK2-1715475357289)]
[外链图片转存中...(img-uDMAO7un-1715475357289)]
[外链图片转存中...(img-mk1pnaAs-1715475357289)]

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上运维知识点,真正体系化!**

**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**

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

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值