1.nginx日志
1.日志分离
[root@web02 ~]
http {
......
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
......
}
[root@web02 ~]
server {
listen 80;
server_name www.server1.com;
access_log /var/log/nginx/www.server1.com.log main;
location / {
root /code;
index index.html;
}
}
[root@web02 ~]
server {
listen 80;
server_name www.server2.com;
access_log /var/log/nginx/www.server2.com.log main;
location / {
root /code1;
index index.html;
}
}
2.日志切割
[root@web02 modules]
/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
}
[root@nginx conf.d]
/var/log/nginx/*.log {
daily
missingok
rotate 52
compress
delaycompress
notifempty
create 640 nginx adm
sharedscripts
postrotate
if [ -f /var/run/nginx.pid ]; then
kill -USR1 `cat /var/run/nginx.pid`
fi
endscript
}
2.Nginx 模块
1.nginx目录索引模块 ngx_http_autoindex_module
1)模块语法
Syntax: autoindex on | off;
Default:autoindex off;
Context:http, server, location
2)配置
server {
listen 80;
server_name www.mario.com;
access_log /var/log/nginx/www.mario.com.log main;
charset 'utf-8';
location / {
root /code;
index index.html;
}
location /download {
root /code;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
}
}
2.nginx访问控制模块 ngx_http_access_module
1)模块语法
Syntax: allow address | CIDR | unix: | all;
Default: —
Context: http, server, location, limit_except
Syntax: deny address | CIDR | unix: | all;
Default: —
Context: http, server, location, limit_except
2)访问控制实例
server {
listen 80;
server_name www.mario.com;
access_log /var/log/nginx/www.mario.com.log main;
charset 'utf-8';
location / {
root /code;
index index.html;
}
location /download {
root /code;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
allow 10.0.0.1;
deny all;
}
}
[root@web01 conf.d]
server {
listen 80;
server_name www.mario.com;
access_log /var/log/nginx/www.mario.com.log main;
charset 'utf-8';
location / {
root /code;
index index.html;
}
location /download {
root /code;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
deny 10.0.0.1;
allow all;
}
}
[root@web01 conf.d]
server {
listen 80;
server_name www.mario.com;
access_log /var/log/nginx/www.mario.com.log main;
charset 'utf-8';
location / {
root /code;
index index.html;
}
location /download {
root /code;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
allow 10.0.0.0/24;
deny all;
}
}
3)一般使用场景
在公司里面,访问我们的后台,只允许在公司内部使用公司的网络访问
拒绝其他地址访问进来,如果需要在家使用其他地址访问,需要使用VPN拨号访问
3.nginx访问认证模块 ngx_http_auth_basic_module
1)模块语法
Syntax: auth_basic string | off;
Default: auth_basic off;
Context: http, server, location, limit_except
Syntax: auth_basic_user_file file;
Default: —
Context: http, server, location, limit_except
2)配置密码认证模块
[root@web01 conf.d]
New password:
Re-type new password:
Adding password for user linux
[root@web01 conf.d]
[root@web01 conf.d]
Adding password for user linux
[root@web01 conf.d]
[root@web01 conf.d]
server {
listen 80;
server_name www.mario.com;
access_log /var/log/nginx/www.mario.com.log main;
charset 'utf-8';
location / {
root /code;
index index.html;
}
location /download {
root /code;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
allow 10.0.0.0/24;
deny all;
auth_basic "please don't like me!";
auth_basic_user_file /etc/nginx/conf.d/auth_basic;
}
}
4.nginx状态模块 ngx_http_stub_status_module
1)模块语法
Syntax: stub_status;
Default: —
Context: server, location
2)配置
location /nginx_status {
stub_status;
}
3)nginx七种状态
Active connections: 2
server accepts handled requests
2 2 2
Reading: 0 Writing: 1 Waiting: 1
Active connections
accepts
handled
requests
Reading
Writing
Waiting
keepalive_timeout 0;
keepalive_timeout 65;
5.nginx连接限制模块 ngx_http_limit_conn_module
1)模块语法
Syntax: limit_conn_zone key zone=name:size;
Default: —
Context: http
Syntax: limit_conn zone number;
Default: —
Context: http, server, location
2)配置
http{
......
limit_conn_zone $remote_addr zone=conn_zone:10m;
......
}
server{
......
limit_conn conn_zone 1;
......
}
6.nginx限制请求模块
1)模块语法
Syntax: limit_req_zone key zone=name:size rate=rate;
Default: —
Context: http
Syntax: limit_req zone number [burst=number] [nodelay];
Default: —
Context: http, server, location
2)配置
[root@web01 conf.d]
limit_req_zone $remote_addr zone=req_zone:10m rate=1r/s;
server {
listen 80;
server_name www.mario.com;
access_log /var/log/nginx/www.mario.com.log main;
charset 'utf-8';
location / {
root /code;
index index.html;
}
location /download {
root /code;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
allow 10.0.0.0/24;
deny all;
auth_basic "please don't like me!";
auth_basic_user_file /etc/nginx/conf.d/auth_basic;
limit_req zone=req_zone burst=5 nodelay;
}
location /nginx_status {
stub_status;
allow 127.0.0.1;
deny all;
}
}
3)验证请求限制
[root@web01 conf.d]
Server Software: nginx/1.16.1
Server Hostname: www.mario.com
Server Port: 80
Document Path: /download/
Document Length: 179 bytes
Concurrency Level: 2
Time taken for tests: 0.002 seconds
Complete requests: 20
Failed requests: 19
(Connect: 0, Receive: 0, Length: 19, Exceptions: 0)
[root@web01 code]
Server Software: nginx/1.16.1
Server Hostname: www.mario.com
Server Port: 80
Document Path: /download/
Document Length: 179 bytes
Concurrency Level: 2
Time taken for tests: 0.002 seconds
Complete requests: 20
Failed requests: 14
(Connect: 0, Receive: 0, Length: 14, Exceptions: 0)
Nginx Location
1.location语法
Syntax: location [ = | ~ | ~* | ^~ | / ] uri { ... }
location @name { ... }
Default: —
Context: server, location
2.location验证
[root@Nginx conf.d]
server {
listen 80;
server_name www.server.com;
location / {
default_type text/html;
return 200 "location /";
}
location =/ {
default_type text/html;
return 200 "location =/";
}
location ~ / {
default_type text/html;
return 200 "location ~/";
}
}
3.location 优先级
| 匹配符 |
匹配规则 |
优先级 |
| = |
精确匹配 |
1 |
| ^~ |
以某个字符串开头 |
2 |
| ~ |
区分大小写的正则匹配 |
3 |
| ~* |
不区分大小写的正则匹配 |
4 |
| / |
通用匹配,任何请求都会匹配到 |
5 |
4.验证
[root@web01 conf.d]
server {
listen 80;
server_name www.server.com;
location / {
root /code;
}
location ~ \.php$ {
root /php;
}
location ~ \.jsp$ {
root /jsp;
}
location ~* .*\.(jpg|gif|png|js|css)$ {
root /pic;
}
location ~* "\.(sql|bak|tgz|tar.gz|.git)$" {
root /package;
}
}
5.location扩展
[root@web01 conf.d]
limit_req_zone $remote_addr zone=req_zone:10m rate=1r/s;
server {
listen 80;
server_name www.mario.com;
access_log /var/log/nginx/www.mario.com.log main;
charset 'utf-8';
location / {
root /code;
index index.html;
}
location /download {
root /code;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
allow 10.0.0.0/24;
deny all;
auth_basic "please don't like me!";
auth_basic_user_file /etc/nginx/conf.d/auth_basic;
limit_req zone=req_zone;
error_page 503 /404.html;
}
location /nginx_status {
stub_status;
allow 127.0.0.1;
deny all;
}
}