Nginx 高级配置

目录

一、状态页

二、使用第三方模块

三、Nginx变量

3.1 内置变量

3.2 自定义变量

四、Nginx 自定义访问日志

4.1 访问日志默认格式

4.2 访问日志自定义JSON格式

五、Nginx压缩功能

六、HTTPS功能

6.1 实现单域名HTTPS

6.2 实现多域名HTTPS

七、关于浏览器小图标(favicon.ico)

八、隐藏Nginx以及Nginx版本号

九、升级OpenSSL版本,防止心脏出血

十、编译参数总结

一、状态页

模块:ngx_http_stub_status_module

编译参数:--with_http_stub_status_module,yum安装自动开启,编译安装需要手动开启

指令:stub_status

作用域:location、server

location /nginx_status {
    stub_status;
    allow 192.168.0.0/16;
    allow 127.0.0.1;
    deny all;
}

访问http://IP/nginx_status

参数说明:

        Active connections:活动连接数。主要用来查看Nginx访问量,如果有四个Nginx,就把四个Nginx的Active connections加起来。

        accepts:统计总值,Nginx⾃启动后已经接受的客户端请求的总数。

        handled:统计总值,Nginx⾃启动后已经处理完成的客户端请求的总数,通常等于accepts,除⾮有因worker_connections限制等被拒绝的连接。

        requests:统计总值,Nginx⾃启动后客户端发来的总请求数

        Reading:当前状态,正在读取客户端请求报⽂⾸部的连接的连接数。    #不能太多,太多说明并发数太大,现象:用户一直在转圈圈

        Writing:当前状态,正在向客户端发送响应报⽂过程中的连接数。    #过多考虑磁盘I/O太低了或磁盘I/O出现堵塞,现像:图片加载不出来

        Waiting:当前状态,正在等待客户端发出请求的空闲连接数,开启 keep-alive的情况下,这个值等于active – (reading+writing)

二、使用第三方模块

以使用echo模块为例,echo主要用来打印出Nginx内置变量

先进行模块的下载

[root@centos7 ~]# cd /usr/local/src/
[root@centos7 src]# git clone https://github.com/openresty/echo-nginx-module.git

重新编译安装Nginx

nginx -V    #获取之前的编译参数

nginx -s stop    #先停止服务

cd /usr/local/src/nginx-1.18.0/

./configure --prefix=/apps/nginx \
--user=nginx /--group=nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-pcre /--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module \
--add-module=/usr/local/src/echo-nginx-module

make

make install
location /main {
    index index.html;
    default_type text/html;
    echo "hello world,main-->";
    echo_reset_timer;
    echo_location /sub1;
    echo_location /sub2;
    echo "took $echo_timer_elapsed sec for total.";
}

location /sub1 {
    echo_sleep 1;
    echo sub1;
}

location /sub2 {
    echo_sleep 1;
    echo sub2;
}

访问 IP/main 输出以下内容

location /test {
    default_type text/html;
    echo "n50";
}

访问 IP/test 输出 "n50"

三、Nginx变量

3.1 内置变量

$remote_addr;    #客户端的地址
$remote_port;   #客户端请求Nginx服务器时随机打开的端⼝
$remote_user;    #已经经过Auth Basic Module验证的⽤户名
$document_root;   #保存了针对当前资源的请求的系统根⽬录,其就是location下root的目录,如/apps/nginx/html
$document_uri;   #保存了当前请求中不包含指令的URI,注意是不包含请求的指令,⽐如http://www.magedu.net/main/index.do?id=20190221&partner=search会被定义为/main/index.do
$args;   #变量中存放了URL中的指令,例如http://www.magedu.net/main/index.do?id=20190221&partner=search中的id=20190221&partner=search,问号后面的参数
$http_user_agent;   #客户端浏览器的详细信息。浏览器类型
$http_cookie;   #客户端的cookie信息。
$request_body_file;    #做反向代理时发给后端服务器的本地资源的名称。例如index.php
$request_method;    #客户端请求资源的⽅式,GET/PUT/DELETE等
$request_filename;    #当前请求的资源⽂件的路径名称,由root或alias指令与URI请求⽣成的⽂件绝对路径,如/apps/nginx/html/main/index.html
$request_uri;    #包含请求参数的原始URI,不包含主机名,如:/main/index.do?id=20190221&partner=search 。
$server_protocol;    #保存了客户端请求资源使⽤的协议的版本,如HTTP/1.0,HTTP/1.1,HTTP/2.0等。
$server_addr;    #服务器的IP地址。
$server_name;    #服务器的主机名。一般用$host
$server_port;    #请求的器的端⼝号。
$host;   #存放了请求的host名称。浏览器输入的域名。
limit_rate 10240;
echo $limit_rate;    #如果nginx服务器使⽤limit_rate配置了显示⽹络速率,则会显示,如果没有设置,则显示0。
$scheme;    #请求的协议,如ftp,https,http等。用来rewrite跳转,先判断是否https,不是进行rewrite

3.2 自定义变量

Syntax: set $variable value;

Default: —

Context: server, location, if

set $name lck;
echo $name;    #打印"lck"

set $my_port $server_port;
echo $my_port;
echo "$server_name:$server_port";

set $IP $remote_addr;
echo $IP;    #打印客户端IP

server {
    set $docroot $document_root

    location / {
        rewrite (.+)? /index.html break;
        root $docroot
    }

    location /index.html {
        root $docroot
    }

}

四、Nginx 自定义访问日志

Nginx错误日志只有一个,一般写在作用域main上。

error_log /var/log/nginx/error.log info;

Nginx的访问日志可以只写一个(写在作用域http);如果server数量较多,可以每个server写一个access_log(写在作用域server)。

指令:access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];
    access_log off;

作用域:httpserverlocationif in locationlimit_except

4.1 访问日志默认格式

log_format nginx_format1 '$remote_addr - $remote_user [$time_local] "$request" '
                         '$status $body_bytes_sent "$http_referer" '
                         '"$http_user_agent" "$http_x_forwarded_for"'
                         '$server_name:$server_port';
access_log logs/access.log nginx_format1;

访问测试

192.168.0.1 - - [22/Feb/2019:08:44:14 +0800] "GET /favicon.ico HTTP/1.1" 404 162 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:65.0) Gecko/2 0100101 Firefox/65.0" "-"www.lck.net:80

4.2 访问日志自定义JSON格式

一般将JOSN格式的日志模板放在http上,供多个server调用。

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",'
                       '"server_name":"$host",'
                       '"xff":"$http_x_forwarded_for",'
                       '"referer":"$http_referer",'
                       '"tcp_xff":"$proxy_protocol_addr",'
                       '"http_user_agent":"$http_user_agent",'
                       '"status":"$status"}';
access_log /apps/nginx/logs/access_json.log access_json;
$time_iso8601:本地时间(ISO 8601标准格式)
$server_addr:服务器IP
$remote_addr:客户端IP
$body_bytes_sent:给用户响应的报文大小
$request_time:nginx 收到请求开始到将响应数据包发送到本地网卡的时间
$upstream_response_time:后端服务器的响应时间
$upstream_addr:后端服务器IP地址
$host:客户端请求的域名
$uri:域名后面所有字符串
$http_x_forwarded_for:客户端真实IP,一般配置在代理服务器上
$http_referer:是从哪里跳转过来的
$proxy_protocol_addr:tcp负载均衡过来的真实客户端IP
$http_user_agent:用户浏览器类型
$status":"$status:状态码

访问测试

{"@timestamp":"2019-02-22T08:55:32+08:00","host":"192.168.7.102","clientip":"192.168.0.1","size":162,"responsetime":0.000,"upstreamtime":"-","upstreamhost":"-","http_host":"www.lck.net","uri":"/favicon.ico","server_name":"www.lck.net","xff":"-","referer":"-","tcp_xff":"","http_user_agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0","status":"404"}

五、Nginx压缩功能

Nginx⽀持对指定类型的⽂件进⾏压缩然后再传输给客户端,⽽且压缩还可以设置压缩⽐例,压缩后的⽂件⼤⼩将⽐源⽂件显著变⼩,这样有助于降低出⼝带宽的利⽤率,降低企业的IT⽀出,不过会占⽤相应的CPU资源。

一般用在静态服务器,浏览器需要支持解压缩。服务器会与浏览器进行协商用什么方式进行压缩,一般都是gzip压缩。

模块:ngx_http_gzip_module

#启用或禁用gzip压缩,一般在http全局配置
Syntax:gzip on | off;
Default:gzip off;
Context:http, server, location, if in location

gzip on;
#压缩比,一般是3~5之间
Syntax:gzip_comp_level level;
Default:gzip_comp_level 1;
Context:http, server, location

gzip_comp_level 5;
#对哪些浏览器关闭压缩功能
Syntax:gzip_disable regex ...;
Default:—
Context:http, server, location

gzip_disable "MSIE [1-6]\.";    #对IE6进行压缩
#小于设置的值,文件将不会压缩
Syntax:gzip_min_length length;
Default:gzip_min_length 20;
Context:http, server, location

gzip_min_length 1k;
#启用压缩时,协议最小版本,目前默认HTTP/1.1,所以可以不用写
Syntax:gzip_http_version 1.0 | 1.1;
Default:gzip_http_version 1.1;
Context:http, server, location

gzip_http_version 1.1;
#需要开启缓存的大小,个数*大小,其128可以是32*4k或16*8k
Syntax:gzip_buffers number size;
Default:gzip_buffers 32 4k|16 8k;
Context:http, server, locatio

gzip_buffers 32 4k;
#选择要压缩哪些类型的文件
Syntax:gzip_types mime-type ...;
Default:gzip_types text/html;
Context:http, server, location

gzip_types text/plain application/javascript application/x-javascript text/cssapplication/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
#是否在响应报文首部插入“Vary: Accept-Encoding”,一般都选择开启
Syntax:gzip_vary on | off;
Default:gzip_vary off;
Context:http, server, location

gzip_vary on;
gzip on;
gzip_comp_level 5;
gzip_min_length 1k;
gzip_types text/plain application/javascript application/x-javascript text/cssapplication/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
gzip_vary on;

命令行进行访问测试或者火狐浏览器进行测试

cp /apps/nginx/logs/access.log /data/nginx/html/pc/test.html    #size > 1k
curl --head --compressed http://www.lck.net/test.html    #查看响应报头,是否包含"Content-Encoding: gzip"进行压缩传输 

六、HTTPS功能

模块:ngx_http_ssl_module

编译参数:--ngx_http_ssl_module

#开启ssl功能
Syntax:ssl on | off;
Default:ssl off;
Context:http, server

ssl on;    #1.15版本之前
listen address ssl    #1.15版本之后

证书分为多域名证书和单域名证书,多域名证书较贵。

#当前虚拟主机使用的公钥文件(crt文件)
Syntax:ssl_certificate file;
Default:—
Context:http, server

ssl_certificate /path/to/file
#当前虚拟主机使用的私钥文件(key文件)
Syntax:ssl_certificate_key file;
Default:—
Context:http, server

ssl_certificate_key /path/to/file;
#支持SSL协议的版本
Syntax:ssl_protocols [SSLv2] [SSLv3] [TLSv1] [TLSv1.1] [TLSv1.2] [TLSv1.3];
Default:ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
Context:http, server

ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
#配置ssl缓存客户端session信息,服务访问量大的时候,可以开启
Syntax:ssl_session_cache off | none | [builtin[:size]] [shared:name:size];
Default:ssl_session_cache none;
Context:http, server

off:关闭缓存
none: 通知客户端⽀持ssl session cache,但实际不⽀持
builtin[:size]:使⽤OpenSSL内建缓存,为每worker进程私有
[shared:name:size]:在各worker之间使⽤⼀个共享的缓存,需要定义⼀个缓存名称和缓存空间⼤⼩,⼀兆可以存储4000个会话信息,多个虚拟主机可以使⽤相同的缓存名称。

ssl_session_cache shared:sslcache:20m;
#客户端连接可以复用ssl_session_cache中缓存的有效时长,默认5m
Syntax:ssl_session_timeout time;
Default:ssl_session_timeout 5m;
Context:http, server

ssl_session_timeout 5m;

6.1 实现单域名HTTPS

server {
    listen 80;
    listen 443 ssl;
    server_name mobile.lck.net;
    ssl_certificate /apps/nginx/certs/mobile.lck.net.crt;
    ssl_certificate_key /apps/nginx/certs/mobile.lck.net.key;
    ssl_session_cache shared:sslcache:20m;
    ssl_session_timeout 5m;

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

}

查看443端口是否开启,然后进行测试访问。

6.2 实现多域名HTTPS

Nginx⽀持基于单个IP实现多域名的功能,并且还⽀持单IP多域名的基础之上实现HTTPS,其实是基于Nginx的SNIServer Name Indication)功能实现。

SNI是为了解决⼀个Nginx服务器内使⽤⼀个IP绑定多个域名和证书的功能,其具体功能是客户端在连接到服务器建⽴SSL链接之前先发送要访问站点的域名(Hostname),这样服务器再根据这个域名返回给客户端⼀个合适的证书。

Nginx具备该功能,apache不具备该功能。

server {
    listen 80;
    listen 443 ssl;
    server_name mobile.lck.net;
    ssl_certificate /apps/nginx/certs/mobile.lck.net.crt;
    ssl_certificate_key /apps/nginx/certs/mobile.lck.net.key;
    ssl_session_cache shared:sslcache:20m;
    ssl_session_timeout 5m;

    location / {
        root /data/nginx/html/mobile;
        index index.html;
    }

}

server {
    listen 80;
    listen 443 ssl;
    server_name www.lck.net;
    ssl_certificate /apps/nginx/certs/www.lck.net.crt;
    ssl_certificate_key /apps/nginx/certs/www.lck.net.key;
    ssl_session_cache shared:sslcache:20m;
    ssl_session_timeout 5m;

    location / {
        root /data/nginx/html/www;
        index index.html;
    }

}

七、关于浏览器小图标(favicon.ico)

favicon.ico⽂件是浏览器收藏⽹址时显示的图标,当客户端使⽤浏览器问⻚⾯时,浏览器会⾃⼰主动发起请求获取⻚⾯的favicon.ico⽂件,但是当浏览器请求的favicon.ico⽂件不存在时,服务器会记录404⽇志,⽽且浏览器也会显示404报错。

location = /favicon.ico {
    root /data/nginx/html/pc/images;
    expires 90d;    #设置⽂件过期时间
 }

八、隐藏Nginx以及Nginx版本号

需要更改Nginx源码、并且重新编译

cd /usr/local/src/nginx-1.18.0/

vim src/http/ngx_http_header_filter_module.c
49 static u_char ngx_http_server_string[] = "Server: lck" CRLF;    #定义响应报⽂中的server字段信息
50 static u_char ngx_http_server_full_string[] = "Server: lck" NGINX_VER CRLF; 
51 static u_char ngx_http_server_build_string[] = "Server: lck" NGINX_VER_BUILD CRLF; 

编译,重新 make && make install

出现404,也会显示nginx,需要将404重定向到其他页面,就不会显示nginx。

九、升级OpenSSL版本,防止心脏出血

查看Nginx使用的openssl版本

[root@centos7 ~]# nginx -V
nginx version: nginx/1.18.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module --with-file-aio
cd /usr/local/src

wget https://www.openssl.org/source/openssl-1.1.1d.tar.gz

tar xvf openssl-1.1.1d.tar.gz

nginx -V    #获取之前的编译参数

cd /usr/local/src/nginx-1.18.0/

#重新编译
./configure --prefix=/apps/nginx \
--user=nginx /--group=nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-pcre /--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module \
--add-module=/usr/local/src/echo-nginx-module \
--with-openssl=/usr/local/src/openssl-1.1.1d

make && make install

nginx -t

nginx

十、编译参数总结

./configure --prefix=/apps/nginx \
--user=nginx /--group=nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-pcre \
--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module \
--with-file-aio \
--add-module=/usr/local/src/echo-nginx-module \
--with-openssl=/usr/local/src/openssl-1.1.1d \

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值