日志
Nginx java json
log_format
Syntax: log_format name [escape=default|json|none] string ...;
Default:
log_format combined "...";
Context: http
Syntax: access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];
access_log off;
Default:
access_log logs/access.log combined;
Context: http, server, location, if in location, limit_except
Example Configuration
log_format compression '$remote_addr - $remote_user [$time_local] '
'"$request" $status $bytes_sent '
'"$http_referer" "$http_user_agent" "$gzip_ratio"';
access_log /spool/logs/nginx-access.log compression buffer=32k;

vim game.conf
http {
log_format main
..
server {
location / {
index index.html;
access_log /var/log/access.log main
}
}
}
每个server单独配置日志存,独立存放
/var/log/bbs_access.log main
/var/log/download_access.log mian
虚拟主机
基于IP
不同的IP
基于端口
相同的IP,不同的端口
基于域名
相同的IP,相同的端口,不同的域名
需求:三个站点,分别是www,bbs,blog
解法1,基于IP的虚拟主机
1)创建目录
mkdir -p /var/code/{www,bbs,blog}
2) 创建配置文件
vim /etc/nginx/conf.d/www.conf
server {
server {
lister 172.16.1.7:80;
server_name www.oldboy.com;
location / {
root /var/code/www;
index index.html;
}
}
}
vim /etc/nginx/conf.d/bbs.conf
server {
server {
lister 172.16.1.8:80;
server_name bbs.oldboy.com;
location / {
root /var/code/bbs;
index index.html;
}
}
}
------------------------------------------------------
解法2:基于端口的虚拟主机
vim /etc/nginx/conf.d/www.conf
server {
server {
lister 80;
server_name www.oldboy.com;
location / {
root /var/code/www;
index index.html;
}
}
}
vim /etc/nginx/conf.d/bbs.conf
server {
server {
lister 81;
server_name bbs.oldboy.com;
location / {
root /var/code/bbs;
index index.html;
}
}
}
---------------------------------------------
:%s/www/bbs/
nginx -t #测试
systemctl reload nginx
mkdir /code/log -p #增加日志目录
解法3:基于域名的虚拟主机
vim /etc/nginx/conf.d/www.conf
server {
server {
lister 80;
server_name www.oldboy.com;
access_log /code/log/www.oldboy.com_access.log main;
location / {
root /var/code/www;
index index.html;
}
}
}
vim /etc/nginx/conf.d/bbs.conf
server {
server {
lister 80;
server_name bbs.oldboy.com;
access_log /code/log/bbs.oldboy.com_access.log main;
location / {
root /var/code/bbs;
index index.html;
}
}
}
---------------------------------------------
location
控制访问路径,一个server可以有多个location,
解析PHP方式,1.安装apache 2.FastCGI 9000端口(php-fpm模块)
=
^~
location ~ \.php$ {
proxy_pass http://127.0.0.1;
}
location ~ \.php$ {
root html;
fastcgi_pass http://127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /script$fastcgi_script_name;
include fastcgi_params;
}
location 语法优先排列
| 匹配符 | 匹配规则 | 优先级 |
| = | 精准匹配 | 1 |
| ^~ | 以某个字符串开始 | 2 |
| ~ | 区分大小写的正则匹配 | 3 |
| ~* | 不区分大小写的正则匹配 | 4 |
| !~ | 区分大小写的不匹配规则 | 5 |
| !~* | 不区分大小写的不匹配规则 | 6 |
| / | 通用匹配,任何请求都会匹配到 | 7 |
测试实验

nginx -t 测试语法
systemctl reload nginx


1.日志
先定义日志格式
指定日志路径
2.虚拟主机
基于端口
基于域名
3.location
路径控制










