一、nginx搭建https协议支持
https://blog.youkuaiyun.com/huanger_/article/details/113184950
二、nginx配置日志打印
access_log用来定义日志级别,日志位置。语法如下:
日志级别: debug > info > notice > warn > error > crit > alert > emerg
1、使用默认格式的日志
把默认配置文件中http模块的日志配置放开即可
#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 /usr/local/nginx/logs/access.log main;
main 代表格式的名称
access_log [配置文件的路径] [格式的名称]
2、使用json格式的配置
log_format main escape=json '{ "@timestamp": "$time_local", '
'"remote_addr": "$remote_addr",'
'"costime": "$request_time",'
'"status": $status,'
'"x_forwarded": "$http_x_forwarded_for",'
'"referer": "$http_referer",'
'"request": "$request",'
'"dm":$request_body,'
'"agent": "$http_user_agent" }';
access_log /usr/local/nginx/logs/access.log main;
escape=json:解决打印的中文乱码问题,需要nginx版本在1.15.1以上
后面传入json串,key自定义,value使用nginx的表达式,按需配置即可
常用的表达式
$remote_addr
,$http_x_forwarded_for
记录客户端IP地址$remote_user
记录客户端用户名称$request
记录请求的URL和HTTP协议(GET,POST,DEL,等)$status
记录请求状态$body_bytes_sent
发送给客户端的字节数,不包括响应头的大小; 该变量与Apache模块mod_log_config里的“%B”参数兼容。$bytes_sent
发送给客户端的总字节数。$connection
连接的序列号。$connection_requests
当前通过一个连接获得的请求数量。$msec
日志写入时间。单位为秒,精度是毫秒。$pipe
如果请求是通过HTTP流水线(pipelined)发送,pipe值为“p”,否则为“.”。$http_referer
记录从哪个页面链接访问过来的$http_user_agent
记录客户端浏览器相关信息$request_length
请求的长度(包括请求行,请求头和请求正文)。$request_time
请求处理时间,单位为秒,精度毫秒; 从读入客户端的第一个字节开始,直到把最后一个字符发送给客户端后进行日志写入为止。$time_iso8601 ISO8601
标准格式下的本地时间。$time_local
通用日志格式下的本地时间。
三、nginx配置http重定向到https
1、方法1
在server的80端口下添加如下即可
return 301 https://$host$request_uri;
配置好后,会发现重定向时post请求会被转为get请求,这是因为301是永久重定向,将301改成307即可解决
$host:请求的服务端的地址
$request_uri:请求中usl中的参数,不配置此项则会在重定向时丢失参数,或者直接使用http代理后端服务也会丢失参数部分,导致参数无法传递
2、方法二
在server的80端口下添加如下即可
#rewrite ^(.*)$ https://$host$1?$args;
$args:代表参数
四、nginx代理vue项目刷新报错404问题
在对应的代理配置下添加如下代码即可
try_files $uri $uri/ /index.html; #解决刷新报404问题