Ubuntu nginx 配置指导一
- 1. nginx 自定义日志
- 2. nginx自定义错误页面
- 3. nginx长连接
- 4. nginx下载服务器配置
- 5. 隐藏Nginx版本信息
- 6. nginx状态页
- 7. nginx 使用第三方模块
- 8. nginx 升级openssl
- 9. nginx 压缩功能
- 10. nginx 关于favicon.ico
1. nginx 自定义日志
- 自定义格式
log_format 这个参数用在http{}的块里
access_log 用来调用可以写在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;
- 自定义nginx日志格式为json格式
通过重启查看日志,然后可以把日志放在在线网址上查看是否是完整json格式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"}'; access_log /apps/nginx/logs/access_json.log access_json;
2. nginx自定义错误页面
配置写在server{}块里或者http{}块里
error_page 404 /no.html;
location /no.html {
root /data/html;
}
3. nginx长连接
Syntax: keepalive_timeout timeout [header_timeout];
Default: keepalive_timeout 75s;
Context: http, server, location
# 范例
keepalive_timeout 65 60;
# 如果这样写的话,第一个数字为生效的数字,第二个数字为给客户端看的数字
#Response Headers(响应头信息):
HTTP/1.1 200 OK
Server: nginx/1.14.2
Date: Thu, 14 Mar 2019 17:23:46 GMT
Content-Type: text/html
Content-Length: 7
Last-Modified: Thu, 14 Mar 2019 14:54:50 GMT
Connection: keep-alive
Keep-Alive: timeout=60
ETag: "5c8a6b3a-7"
Accept-Ranges: bytes
4. nginx下载服务器配置
[root@s2 about]# mkdir /data/nginx/html/pc/download
#download不需要index.html文件
[root@s2 about]# vim /apps/nginx/conf/conf.d/pc.conf
location /download {
autoindex on; #自动索引功能
autoindex_exact_size on; #计算文件确切大小(单位bytes),off只显示大概大小(单位kb、mb、gb)
autoindex_localtime on; #显示本机时间而非GMT(格林威治)时间
root /data/nginx/html/pc;
}
5. 隐藏Nginx版本信息
- nginx隐藏版本号
在server{}或者http块里,插入指令Syntax: server_tokens on | off | build | string; Default: server_tokens on; Context: http, server, location #默认为on 修改为off 即关闭了版本信息 root@youyou:/apps/nginx/conf.d# vi test.conf server_tokens off; root@youyou:/apps/nginx/conf.d# nginx -s reload root@youyou:~# curl -i 192.168.19.30/dsf HTTP/1.1 404 Not Found Server: nginx Date: Tue, 07 Jan 2020 03:40:38 GMT Content-Type: text/html Content-Length: 16 Connection: keep-alive ETag: "5e13f009-10" 页面走丢了 root@youyou:~#
- nginx隐藏版本内容
重新编译nginx# 修改编译目录下的,这个模块内容改成自己想要的版本信息 root@youyou:/apps/nginx-1.16.1# vi +49 src/http/ngx_http_header_filter_module.c static u_char ngx_http_server_string[] = "Server: xiapi" CRLF; # 查看上次编译的编译选项 root@youyou:/apps/nginx-1.16.1# nginx -V nginx version: nginx/1.16.1 built by gcc 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04) built with OpenSSL 1.1.1 11 Sep 2018 TLS SNI support enabled configure arguments: --prefix=/apps/nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_image_filter_module --with-http_geoip_module --with-http_gunzip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module # 停止服务 root@youyou:~# /apps/nginx/sbin/nginx -s stop # 重新开始编译 root@youyou:/apps/nginx-1.16.1# ./configure --参数复制下来 root@youyou:/apps/nginx-1.16.1# make root@youyou:/apps/nginx-1.16.1# make install # 开启服务 root@youyou:~# /apps/nginx/sbin/nginx # 测试 root@youyou:~# curl -i 192.168.19.30/dsf HTTP/1.1 404 Not Found Server: xiapi Date: Tue, 07 Jan 2020 03:47:11 GMT Content-Type: text/html Content-Length: 16 Connection: keep-alive ETag: "5e13f009-10" 页面走丢了 root@youyou:~#
6. nginx状态页
基于nginx模块ngx_http_auth_basic_module实现,在编译安装nginx的时候需要添加编译参数–withhttp_stub_status_module,否则配置完成之后监测会是提示语法错误。
root@youyou:/apps/nginx/conf.d# vi test.conf
location /status
{
stub_status;
allow 192.168.19.0/24;
deny all;
}
# 测试
root@youyou:/apps/nginx/conf.d# curl 192.168.19.30/status
Active connections: 2
server accepts handled requests
21 21 28
Reading: 0 Writing: 1 Waiting: 1
root@youyou:/apps/nginx/conf.d#
- Active connections: 当前处于活动状态的客户端连接数,包括连接等待空闲连接数。
- accepts:统计总值,Nginx自启动后已经接受的客户端请求的总数。
- handled:统计总值,Nginx自启动后已经处理完成的客户端请求的总数,通常等于accepts,除非有因worker_connections限制等被拒绝的连接。
- requests:统计总值,Nginx自启动后客户端发来的总的请求数。
- Reading:当前状态,正在读取客户端请求报文首部的连接的连接数。
- Writing:当前状态,正在向客户端发送响应报文过程中的连接数。
- Waiting:当前状态,正在等待客户端发出请求的空闲连接数,开启 keep-alive的情况下,这个值等于 active –(reading+writing)。
7. nginx 使用第三方模块
使用add-module来指定第三方模块的路径。
[root@s2 src]# yum install git -y
[root@s2 src]# git clone https://github.com/openresty/echo-nginx-module.git
[root@s2 src]# cd nginx-1.12.2/
[root@s2 src]# ./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-http_perl_module \
--add-module=/usr/local/src/echo-nginx-module
[root@s2 src]# make && make install
8. nginx 升级openssl
使用–with-openssl 参数来指定解压的 ssl源码包路径
准备OpenSSL源码包:
root@youyou:~# tar xvf openssl-1.1.1d -C /usr/local/src/
编译安装Nginx并制定新版本OpenSSL路径:
root@youyou:~# cd /usr/local/src/nginx-1.16.1/
root@youyou:~#./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 --withhttp_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --withstream_realip_module --with-select_module --with-file-aio --addmodule=/usr/local/src/echo-nginx-module --with-openssl=/usr/local/src/openssl-1.1.1d
root@youyou:~# make && make install
9. nginx 压缩功能
Nginx对文件的压缩功能是依赖于模块ngx_http_gzip_module,使用nginx -V 查看是否编译进去此选项,代码块选项放在http, server, location都行,一般压缩比到三即可
root@youyou:/apps/nginx-1.16.1# ./configure --help| grep gzip
--with-http_gzip_static_module enable ngx_http_gzip_static_module
--without-http_gzip_module disable ngx_http_gzip_module
root@youyou:/apps/nginx-1.16.1#
#启用或禁用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;
实例:
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;
10. nginx 关于favicon.ico
favicon.ico 文件是浏览器收藏网址时显示的图标,当客户端使用浏览器问页面时,浏览器会自己主动发起请求获取页面的favicon.ico文件,但是当浏览器请求的favicon.ico文件不存在时,服务器会记录404日志,而且浏览器打开f12 也会显示404报错。
- 禁用把此日志放在error.log 中
把代码块放在server{}中location /favicon.ico { log_not_found off; access_log off; }
- 找一个favicon.ico 的图标放在网页目录中
location /favicon.ico { root /data/html; }