全局指令
| 指令 | 值 |
|---|---|
| user | 出于安全考虑,默认是nginx、nobody |
| worker_processes | 工作进程数,一般来说,设置与CPU的核心数相同即可 |
| error_log | 保存错误日志的路径,可以设置error_log的级别 |
| pid | nginx 进程id |
user nginx;
worker_processes 4;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
# 每秒处理多少个客户端连接
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
#与客户端保持连接的最长时间
keepalive_timeout 65;
gzip on;
include /etc/nginx/conf.d/*.conf;
}
日志分析
- ngxtop 流量实时监测 https://github.com/lebinh/ngxtop,这个简直就是神器(o^^o)
- goaccess 日志可视化 https://github.com/allinurl/g…
- goaccess 分析配置 https://github.com/stockrt/ng…
- goaccess 操作说明 https://www.fanhaobai.com/201…
server配置
server {
listen 80;
server_name localhost;
#nginx程序默认的web根目录
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
error_page 404 /404.html;
# 重定向服务器的错误页到指定的静态页面 /50x.html
error_page 500 502 503 504 404 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
当我们配置好了Nginx,就可以使用curl来进行初步的测试了
curl -v http://127.0.0.1/ > /dev/null
倘若我们想禁止用户通过IP或者未设置的域名来访问,可以采取如下措施
server {
listen 80 default;
rewrite ^(.*) http://www.example.com permanent;
}
变量
| 变量类型 | 变量列表 |
|---|---|
| 内置变量 | http://nginx.org/en/docs/vari… |
| HTTP请求变量 | http://nginx.org/en/docs/http… |
参考文档:http://www.ttlsa.com/nginx/ng…
路由
如果我们想要访问/path/to/name,那么 Nginx 配置中,路由匹配的优先级如下
| 修饰符 | 说明 | 优先级 |
|---|---|---|
| location = /path/to/name | 精确匹配 | 1 |
| location /path/to/name | 完整匹配 | 2 |
| location ^~ /path/to/name | 表示uri以某个常规字符串开头,非正则表达式 | 3 |
| location ~* /path/to/name | 使用不区分大小写的正则来进行匹配 | 4 |
| location ~ /path/to/name | 使用区分大小写的正则来进行匹配 | 4 |
| location /path | 部分匹配 | 5 |
| location / | 通用匹配 | 6 |
发表回复