NGNIX 日常爬坑指南

372 阅读1分钟

查看ngnix配置文件位置:

找到nginx服务器的路径,然后执行nginx -t查看配置文件位置

[root@blue2 ~]# /usr/sbin/nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

nginx的日志位置: tail -f /var/log/nginx/access.log

配置ngnix,安装证书,生成basic auth密码
https://www.marksei.com/lets-encrypt-setup-centos/
https://www.cnblogs.com/AloneSword/p/5086918.html

location /
        {
                auth_basic "nginx basic http test for ttlsa.com";
                auth_basic_user_file conf/htpasswd;
                autoindex on;
        }
# printf "ttlsa:$(openssl passwd -crypt 123456)\n" >>conf/htpasswd
# cat conf/htpasswd
ttlsa:xyJkVhXGAZ8tM

nginx -s reload 重新加载配置

printf "blueo:$(openssl passwd -crypt Shi8eeh6)\n" >>/etc/nginx/conf/htpasswd

密码生成器pwgen

Nginx无法启动的解决方法

因为 nginx 启动需要一点点时间,而 systemd 在 nginx 完成启动前就去读取 pid file
造成读取 pid 失败

解决方法很简单,让 systemd 在执行 ExecStart 的指令后等待一点点时间即可
如果你的 nginx 启动需要时间更长,可以把 sleep 时间改长一点

mkdir -p /etc/systemd/system/nginx.service.d
printf "[Service]\nExecStartPost=/bin/sleep 0.1\n" > /etc/systemd/system/nginx.service.d/override.conf

然后

systemctl daemon-reload
systemctl restart nginx.service

Nginx 查看统计ip访问次数

sudo less /var/log/nginx/access.log | awk '{ print $1 }' | sort | uniq -c | sort -n