2020-10-16
linux
00
请注意,本文编写于 1260 天前,最后修改于 1260 天前,其中某些信息可能已经过时。

目录

1. 通用配置文件说明
2 全局配置
2.1 nginx进程和cpu绑定
2.2 docker启动nginx前台进程配置
2.3 开启events的epoll事件驱动
2.4 关闭“惊群”
2.5 一个进程可以处理多个请求
3 http配置段
3.1 location配置段的root和alias
3.2 location配置使用
3.3 Nginx 四层访问控制
3.4 Nginx账户认证功能
3.5 Nginx作为下载服务器配置
3.6 Nginx内置状态页
3.7 Nginx自定义json访问格式的日志
3.8 Nginx开启压缩
4. nginx高级配置
4.1 Nginx https配置

Ubuntu安装nginx1.16.1

1. 通用配置文件说明

nginx1.6通用配置文件

bash
user nginx nginx; work_processes auto; #可以为数量(<=实际cpu个数)或者auto(自动获取) error_log /var/log/nginx/error.log; pid /run/nginx.pid include /usr/share/nginx/modules/*.conf event { #事件驱动模型 worker_connection 1024; #每个进程可以处理的最大的进程 } http{# http的配置, ​ log_format main xxxx xxx; #日志文件格式 ​ access_log /xxx/access.log main; #访问日志位置#gzip on; #开启缓存#gzip_static on;#gzip_types xxxx; 需要缓存的类型 ​ sendfile on; ​ keepalive_timeout 65; ​ include /etc/nginx/mine.type; ​ default type application/octet-stream ​ server { #单个虚拟主机的配置 ​ listen 80; server_name www.mykernel.cn; #需要到域名解析到本机地址 root /usr/share/nginx/html; #该虚拟主机对应的位置(该虚拟主机的根目录) include /etc/nginx/default.d/*.conf; # 为默认服务器块加载配置文件 ​ location{ root html; # 此处建议使用绝对路径 index index.html index.htm; #默认的主页 ​ } error_page 404 /404.html; #自定义错误的配置文件 location = /40x.html { #根目录下的40x.html页面 } error_page 500 502 503 504 /50x.html; #自定义50x页面 location = /50x.html { } ​ } }

思考1:location默认的root在什么位置?

bash
# 请问该server访问的路径是什么地方? server { listen 8041; server_name www.xx.com; root /data/nginx; location /bb { root aa; index 2.html; } error_log /tmp/nginx/error.log; access_log /tmp/nginx/access.log; } # 如果在location中的root使用的相对路径,那么使用的是nginx编译时指定的--prefix=/usr/share/nginx目录下的aa/bb目录, # 所以访问www.xx.com/bb/2.html 完整路径为/usr/share/nginx/aa/bb/2,html,因此建议使用绝对路径。 # 当server和location都指定root的时候,默认以location的为准,server的root失效。 ##

2 全局配置

2.1 nginx进程和cpu绑定

bash
worker_cpu_affinity 00000001 00000010 00000100 00001000;

#将Nginx工作进程绑定到指定的CPU核心上,默认Nginx是不进行进程绑定的,绑定并不是意味着当前nginx进程独占一颗CPU,但是可以保证此进程不会运行到其他核心上,这就极大减少了nginx的工作进程在不同的cpu核心上的来回跳转,减少了CPU对进程的资源分配与回收以及内存管理等,因此可以有效的提升了nginx服务器的性能。

2.2 docker启动nginx前台进程配置

bash
daemon off;

2.3 开启events的epoll事件驱动

bash
events { use epoll; }

2.4 关闭“惊群”

bash
events { accept_mutex on; }

#优化同一时刻只有一个请求,避免多个睡眠进程被唤醒的设置,on为防止被同时唤醒,默认参数为off 全部唤醒的过程也成为"惊群",因此nginx刚安装完以后要进行适当的优化。

2.5 一个进程可以处理多个请求

bash
events { multi_accept on; }

#Nginx服务器的每个工作进程可以同时接受多个新的网络连接,但是需要在配置文件中配置,此指令默认为关闭,即默认为一个工作进程只能一次接受一个新的网络连接,打开后一个同时接受多个。

3 http配置段

3.1 location配置段的root和alias

root:指定web的家目录,在定义location的时候,文件的绝对路径等于:root+location

比如:

bash
location /aa { root /webdata/bb; } ## 访问http://www.mykernel.cn/aa 的时候,对应的文件位置为:/webdata/bb/aa

alias:定义路径别名,会把访问的路径重新定义到其指定的路径 比如:

bash
location /aa { alias /webdata/bb; #最后的斜杠要和location段的一致 } ## 访问http://www.mykernel.cn/aa 的时候,对应的文件位置为:/webdata/bb

一般情况下使用root较多,而且使用绝对路径。

3.2 location配置使用

语法规则: location [=||*|^~] /uri/ { … }

bash
= #用于标准uri前,需要请求字串与uri精确匹配,如果匹配成功就停止向下匹配并且立即处理请求。 ~ #用于标准uri前,表示包含正则表达式并且区分大小写,并且匹配 !~#用于标准uri前,表示包含正则表达式并且区分大小写,并且不匹配 ~*#用于标准uri前,表示包含正则表达式并且不区分大小写,并且匹配 !~*#用于标准uri前,表示包含正则表达式并且不区分大小写,并且不匹配 ^~#用于标准uri前,表示包含正则表达式并且匹配以什么开头 $ #用于标准uri前,表示包含正则表达式并且匹配以什么结尾 \ #用于标准uri前,表示包含正则表达式并且转义字符。可以转. * ?等 * #用于标准uri前,表示包含正则表达式并且代表任意长度的任意字符

3.3 Nginx 四层访问控制

访问控制基于模块ngx_http_access_module实现,可以通过匹配客户端源IP地址进行限制。一般用于某些检测nginx状态页面的访问权限控制。

bash
location /about { alias /data/nginx/html/about; index index.html; deny 192.168.1.1; allow 192.168.1.0/24; allow 10.1.1.0/16; allow 2001:0db8::/32; deny all; #先允许小部分,再拒绝大部分,依次从上往下匹配 }

3.4 Nginx账户认证功能

bash
# 安装密码生产工具,借用httpd的工具 root@nginx:/etc/nginx/conf.d# apt-get install apache2-utils -y ### centos使用:yum install httpd-tools -y # 生成账号密码,第一次生成加-c root@nginx:/etc/nginx/conf.d# htpasswd -cbm /etc/nginx/conf.d/.htpasswd mykernel 123456 Adding password for user mykernel # 添加账号不需要使用-c root@nginx:/etc/nginx/conf.d# htpasswd -bm /etc/nginx/conf.d/.htpasswd mykernel1 123456 Adding password for user mykernel1 # 查看 root@nginx:/etc/nginx/conf.d# cat /etc/nginx/conf.d/.htpasswd mykernel:$apr1$XVpEhFzl$edvO.PolHN2hUl7VKlREO. mykernel1:$apr1$6wwY/Utm$822UIu.xlQm2s9r31W/C5. # nginx配置文件 root@nginx:/etc/nginx/conf.d# cat auth.conf server { listen 82; server_name auth.mykernel.cn; location /login { root /data/nginx/html; index index.html; auth_basic "login password"; auth_basic_user_file /etc/nginx/conf.d/.htpasswd; # 账号 } }

3.5 Nginx作为下载服务器配置

bash
server { listen 81; server_name download.mykernel.cn; location /download { autoindex on; #自动索引功能 autoindex_exact_size on; #计算文件确切大小(单位bytes),off只显示大概的大小(单位kb、mb、 gb) autoindex_localtime on; #显示本机时间 GMT(格林威治)时间 autoindex_format html; #显示风格 alias /data/nginx/html/download; charset utf-8; #解决中文乱码问题 # limit_rate 10k; #限制下载速率为10k/s } }

3.6 Nginx内置状态页

nginx状态页面需要做ip和密码认证

bash
server { listen 83; server_name status.mykernel.cn; location /nginx_status { stub_status; allow 192.168.0.0/16; allow 127.0.0.1; deny all; auth_basic "login password"; auth_basic_user_file /etc/nginx/conf.d/.htpasswd; # 账号 } } # 显示结果 Active connections: 1 # 活动连接数 server accepts handled requests # 接收的总数 处理完成的总数 客户端发起的总数 47 47 61 Reading: 0 Writing: 1 Waiting: 0 # Reading: 当前状态,正在读取客户端请求报文首部的连接的连接数。 # Writing:当前状态,正在向客户端发送响应报文过程中的连接数。 # Waiting:当前状态,正在等待客户端发出请求的空闲连接数,开启 keep-alive的情况下,这个值等于 active-(reading+writing)。

3.7 Nginx自定义json访问格式的日志

日志格式使用json,便于使用代码或者工具对日志进行统计和分析

bash
# 定义log_format,位置为server外,http段内 log_format access_json '{"@timestamp":"$time_iso8601",' '"host":"$server_addr",' '"clientip":"$remote_addr",' '"size":$body_bytes_sent,' '"responsetime":$request_time,' '"upstreamtime":"$upstream_response_time",' '"upstreamhost":"$upstream_addr",' '"http_host":"$host",' '"uri":"$uri",' '"domain":"$host",' '"xff":"$http_x_forwarded_for",' '"referer":"$http_referer",' '"tcp_xff":"$proxy_protocol_addr",' '"http_user_agent":"$http_user_agent",' '"status":"$status"}'; # 使用日志格式,位置server段内或者location段内 access_log /data/nginx/html/access_json.log access_json; root@nginx:/etc/nginx/conf.d# cat log_json.conf server { listen 84; server_name status.mykernel.cn; location /log_test { access_log /data/nginx/html/access_json.log access_json; root /data/nginx/html; index index.html; } } # 日志样式 root@nginx:/data/nginx/html# tail -f access_json.log {"@timestamp":"2020-10-21T17:56:44+08:00","host":"192.168.0.223","clientip":"192.168.0.199","size":555,"responsetime":0.000,"upstreamtime":"-","upstreamhost":"-","http_host":"192.168.0.223","uri":"/nginx_status","domain":"192.168.0.223","xff":"-","referer":"-","tcp_xff":"","http_user_agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36","status":"404"} {"@timestamp":"2020-10-21T17:56:44+08:00","host":"192.168.0.223","clientip":"192.168.0.199","size":555,"responsetime":0.000,"upstreamtime":"-","upstreamhost":"-","http_host":"192.168.0.223","uri":"/favicon.ico","domain":"192.168.0.223","xff":"-","referer":"http://192.168.0.223:84/nginx_status","tcp_xff":"","http_user_agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36","status":"404"} {"@timestamp":"2020-10-21T17:56:50+08:00","host":"192.168.0.223","clientip":"192.168.0.199","size":555,"responsetime":0.000,"upstreamtime":"-","upstreamhost":"-","http_host":"192.168.0.223","uri":"/1.html","domain":"192.168.0.223","xff":"-","referer":"-","tcp_xff":"","http_user_agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36","status":"404"} {"@timestamp":"2020-10-21T17:57:44+08:00","host":"192.168.0.223","clientip":"192.168.0.199","size":5,"responsetime":0.000,"upstreamtime":"-","upstreamhost":"-","http_host":"192.168.0.223","uri":"/log_test/log.html","domain":"192.168.0.223","xff":"-","referer":"-","tcp_xff":"","http_user_agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36","status":"200"}

3.8 Nginx开启压缩

依赖模块ngx_http_gzip_module 配置指令可以使用的范围:http, server, location

bash
#启用gzip压缩,默认关闭 gzip on | off; #压缩等级由低到高从1到9,默认为1 gzip_comp_level level; #禁用IE6 gzip功能 gzip_disable "MSIE [1-6]\."; #gzip压缩的最小文件大小,低于设置值的文件将不会压缩 gzip_min_length 1k; #启用压缩功能时,协议的最小版本,默认HTTP/1.1 gzip_http_version 1.0 | 1.1; #指定Nginx服务需要向服务器申请的缓存空间的个数*大小,默认32 4k|16 8k; gzip_buffers number size; #指明仅对哪些类型的资源执行压缩操作;默认为gzip_types text/html,不用显示指定,否则出错 gzip_types mime-type ...; #如果启用压缩,是否在响应报文首部插入“Vary: Accept-Encoding” gzip_vary on | off; ## root@nginx:/etc/nginx/conf.d# cat gzip.conf server { listen 85; server_name gzip.mykernel.cn; location /gzip { root /data/nginx/html; index index.html; gzip on; gzip_min_length 30k; gzip_comp_level 9; gzip_vary off; #未测试出效果 access_log /data/nginx/html/access_json.log access_json; } }

4. nginx高级配置

4.1 Nginx https配置

本文作者:mykernel

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!