初识nginx
Posted 小张敲代码
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了初识nginx相关的知识,希望对你有一定的参考价值。
一、用GoAccess监控access.log
goaccess access.log -o ../html/goaccess_report.html --real-time-html --time-format='%H:%M:%S' --date-format='%d/%b/%Y' --log-format=COMBINED
命令参数简单介绍
-o :指定输出的文件路径
--real-time-html:实时更新
--time-format:定义时间格式
--date-format:定义日期格式
--log-format:定义日志格式
[root@iZwz9he4lwjhlld6pjd1yaZ logs]# ls -al
total 408
drwxr-xr-x 3 root root 4096 Mar 31 11:36 .
drwxr-xr-x 12 root root 4096 Mar 30 00:16 ..
-rw-r--r-- 1 nobody root 179269 Mar 31 11:36 access.log
-rw-r--r-- 1 nobody root 217209 Mar 31 11:36 error.log
drwxr-xr-x 2 root root 4096 Mar 30 23:32 history
-rwxrwxrwx 1 root root 304 Mar 30 22:46 rotate.sh
[root@iZwz9he4lwjhlld6pjd1yaZ logs]# pwd
/usr/local/nginx/logs
[root@iZwz9he4lwjhlld6pjd1yaZ logs]# goaccess access.log -o ../html/goaccess_report.html --real-time-html --time-format='%H:%M:%S' --date-format='%d/%b/%Y' --log-format=COMBINED
WebSocket server ready to accept new client connections
Accepted: 13 183.250.89.190
Active: 1
CLOSE
Active: 0
在nginx.conf加一条配置
location /goaccess_report.html {
alias /usr/local/nginx/html/goaccess_report.html;
}
重启nginx,在浏览器打开该页面(http://localhost/goaccess_report.html)看看吧
二、用免费SSL证书实现一个HTTPS站点
centos用户使用yum安装
[root@iZwz9he4lwjhlld6pjd1yaZ conf]# yum install python2-certbot-nginx
Loaded plugins: fastestmirror, product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
Determining fastest mirrors
base | 3.6 kB 00:00:00
epel | 4.7 kB 00:00:00
extras | 2.9 kB 00:00:00
updates | 2.9 kB 00:00:00
(1/5): epel/x86_64/group_gz | 96 kB 00:00:00
(2/5): epel/x86_64/updateinfo | 1.0 MB 00:00:00
(3/5): extras/7/x86_64/primary_db | 230 kB 00:00:00
(4/5): epel/x86_64/primary_db | 6.9 MB 00:00:00
(5/5): updates/7/x86_64/primary_db | 6.5 MB 00:00:00
Package python2-certbot-nginx-1.11.0-1.el7.noarch already installed and latest version
Nothing to do
安装完以后可以执行certbot命令
[root@iZwz9he4lwjhlld6pjd1yaZ html]# certbot --nginx --nginx-server-root=/usr/local/openresty/nginx/conf/ -d www.你的域名.com
安装成功后,会在你的nginx.conf新增下面几行配置
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/www.zhanghq.xin/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/www.zhanghq.xin/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
这时候就可以在浏览器端使用https://www.你的域名.com访问试试看了
三、在openresty中尝试Lua脚本
server {
listen 80;
server_name www.zhanghq.xin;
#nginx的配置文件没法直接写lua脚本,因为格式还是有所不同的
#可以写在content_by_lua里面,这边写个简单的例子,将default_type设
#为text/html。方便浏览器直接输出
location /lua {
default_type text/html;
content_by_lua '
ngx.say("User-Agent:", ngx.req.get_headers()["User-Agent"])
';
}
location / {
root /usr/local/openresty/nginx/html;
index index.html index.htm index.php;
}
}
在浏览器中输入http://你的域名/lua试试看,下面是我的浏览器输出的内容
User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36
以上是关于初识nginx的主要内容,如果未能解决你的问题,请参考以下文章
初识OpenGL 片段着色器(Fragment Shader)