Nginx Log Rotation

Nginx 日志切割,在网上查了很多教程。还不如直接看 Ubuntu / CentOS 打包的 Nginx 日志切割配置靠谱。

CentOS 8

sudo dnf install nginx
cat /etc/logrotate.d/nginx
/var/log/nginx/*log {
     create 0664 nginx root
     daily
     rotate 10
     missingok
     notifempty
     compress
     sharedscripts
     postrotate
         /bin/kill -USR1 cat /run/nginx.pid 2>/dev/null 2>/dev/null || true
     endscript
 }

Ubuntu 20.10

sudo apt install nginx
cat /etc/logrotate.d/nginx
/var/log/nginx/*.log {
     daily
     missingok
     rotate 14
     compress
     delaycompress
     notifempty
     create 0640 www-data adm
     sharedscripts
     prerotate
         if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
             run-parts /etc/logrotate.d/httpd-prerotate; \
         fi \
     endscript
     postrotate
         invoke-rc.d nginx rotate >/dev/null 2>&1
     endscript
 }

参考:

https://www.nginx.com/resources/wiki/start/topics/examples/logrotation/

https://www.digitalocean.com/community/tutorials/how-to-configure-logging-and-log-rotation-in-nginx-on-an-ubuntu-vps

 211 total views,  2 views today

400 Bad Request The plain HTTP request was sent to HTTPS port

配置 https 的时候遇到这样一个问题,直接访问 https://www.domain.com 正常,访问 http://www.domain.com 报错:400 Bad Request The plain HTTP request was sent to HTTPS port。


nginx 配置如下:

server {
    listen 80 default_server;
    listen 443 default_server;

    ssl on;
    ssl_certificate /vagrant/cert/nginx.crt;
    ssl_certificate_key /vagrant/cert/nginx.key;
}

有两种解决方案:

server {
    listen 80 default_server;
    return 301 https://$host$request_uri;
}

server {
    listen 443 default_server;
    ssl on;
    ssl_certificate /vagrant/cert/nginx.crt;
    ssl_certificate_key /vagrant/cert/nginx.key;
}
server {
    listen 80 default_server;
    listen 443 ssl default_server;

    ssl_certificate /vagrant/cert/nginx.crt;
    ssl_certificate_key /vagrant/cert/nginx.key;

    if ($https != 'on') {
        return 301 https://$host$request_uri;
    }
}

 714 total views,  1 views today