Nginx学习三

一 、常用nginx常用命令

在nginx部署目录:/home/admin/soft/nginx/sbin/下

1、查看 nginx 版本号

./nginx -v

2、启动 nginx

./nginx
查看是否启动成功:ps -ef|grep nginx
在这里插入图片描述

3、停止 nginx

./nginx -s stop

4、重新加载 nginxv

./nginx -s reload

5、查看版本号

nginx -v

6、检查配置文件是否有误

nginx –t

二、nginx 日志配置不生效的问题

2.1、log_format 有个默认的日志格式:

log_format  combined  '$remote_addr - $remote_user  [$time_local]  '
                       ' "$request"  $status  $body_bytes_sent  '
                       ' "$http_referer"  "$http_user_agent" ';

2.2 nginx 默认调用 combined 格式来记录日志,即默认调用:(默认记录在access.log文件中)

access_log  logs/access.log  combined;

2.3、nginx允许自定义日志格式,例如:

log_format  main  '$remote_addr - $remote_user [$time_local] "$http_host" "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" $http_x_forwarded_for $request_time';

2.4以上是自定义了日志格式:main(main名称可以自定义)要想使其生效,就必须用access_log指定调用:

access_log  logs/xx.log  main;

否则,nginx仍然会去调用combined格式来记录日志。注意,http段也必须明确指定调用main格式才会生效,否则还是会调用默认的combined格式。

三、打印自定义header

3.1 nginx.conf

  log_format  accesslog  '$http_x_forwarded_for`$remote_addr`$proxy_add_x_forwarded_for`[$time_local]`"$request"`'
                      '$status`$body_bytes_sent`"$http_referer"`'
                      '"$http_user_agent"`"$request_time"`'
                      '$request_id`$upstream_response_time`$upstream_addr`$upstream_connect_time`$upstream_status';

3.2在自定义的conf/web.conf 中配置nginx 的server

server{
listen 80;
server_name _;
access_log /home/admin/logs/nginx/web_access.log accesslog;

location ~*~/login/{
rewrite ^/login/(.*) /$1 break;
proxy_pass http://xx.xx.xx.xx;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

注:

header必须用减号“-”分隔单词,nginx里面会转换为对应的下划线“_”连接的小写单词。
1、nginx是支持读取非nginx标准的用户自定义header的,但是需要在http或者server下开启header的下划线支持:
underscores_in_headers on;
2、比如我们自定义header为X-Real-IP,通过第二个nginx获取该header时需要这样:
$http_x_real_ip; (一律采用小写,而且前面多了个http_,减号转成下划线)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值