nginx 404日志格式https配置

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx 404日志格式https配置相关的知识,希望对你有一定的参考价值。

yum install nginx -y
systemctl start nginx
systemctl enable nginx
ps -ef |grep nginx

nginx -t 检查nginx配置文件格式是否正确
nginx -s reload 重新加载配置,不用重启nginx进程

nginx静态资源存放路径:
/usr/share/nginx/html/404.html 50x.html index.html

nginx配置文件存放路径:
/etc/nginx/nginx.conf

404错误配置

当访问一个nginx 中不存在的路径时,就会报404错误
例如:
nginx.conf配置404的配置
技术图片

当访问http://192.168.115.128/ya 时,会出现以下错误页面
技术图片

json格式输出日志

配置:

# log_format  main  ‘$remote_addr - $remote_user [$time_local] "$request" ‘
   #                   ‘$status $body_bytes_sent "$http_referer" ‘
   #                   ‘"$http_user_agent" "$http_x_forwarded_for"‘;

     log_format json ‘{"remote_ip":"$remote_addr",‘
                     ‘"remote_user":"$remote_user",‘
                     ‘"time_local":"$time_local",‘
                     ‘"request":"$request",‘
                     ‘"status":"$status",‘
                     ‘"body_sent":"$body_bytes_sent",‘
                     ‘"referer":"$http_referer",‘
                     ‘"agent":"$http_user_agent",‘
                     ‘"forward":"$http_x_forwarded_for"}‘;

   # access_log  /var/log/nginx/access.log  main;
    access_log  /var/log/nginx/access.log  json;

日志输出显示:
技术图片

HTTPS

server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  www.ilinux.com;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
            rewrite (.*) https://www.ilinux.com$1 permanent;
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

server {
       listen          443 ssl;
       ssl_protocols   TLSv1.1 TLSv1.2;
       ssl_ciphers     HIGH:!aNULL:!MD5;
       ssl_certificate /etc/nginx/certs/nginx.crt;
       ssl_certificate_key /etc/nginx/certs/nginx.key;
       ssl_session_cache shared:SSL:10m;
       ssl_session_timeout 10m;

       location / {

       }

nginx.key获取方式:openssl genrsa -out /etc/nginx/certs/nginx.key 2048
nginx.crt 获取方式:openssl req -new -x509 -key nginx.key -out nginx.cert -days 3650 -subj "/CN=www.ilinux.com"

当访问www.ilinux.com时,就会跳转到https

以上是关于nginx 404日志格式https配置的主要内容,如果未能解决你的问题,请参考以下文章

Linux脚本练习之script084-nginx日志分析之统计爬虫抓取404的次数

自定义Nginx日志格式获取IP地址的省市份信息

Nginx日志配置

nginx rwrite及增加不记录特定状态日志nginx模块

nginx.conf配置格式

修改nginx配置文件访问后链接网页跳转nginx404错误页面